如果特定列之后存在数据,则映射所有列

时间:2018-08-28 06:56:29

标签: excel vba excel-vba

我正在合并所有工作表并映射以前的合并状态,

我有一个在合并之前可以正常工作的代码,现在我需要一个代码来查找从先前合并表到当前合并表的状态。

-这是上一个合并表 This is previous consolidated sheet

-当前合并表 enter image description here

我想映射数据是否在前一张工作表的C列之后从D列开始的标题到当前工作表的地方。我可以用我的代码映射D列,但我还需要映射其余的非空白列。

这是我目前的代码

Sub FMEA_Consolidate()
    Application.ScreenUpdating = False
    Dim SheetName As String
    SheetName = Format(Date, "dd-mmm-yy") 

    ''''''''''''''''''''''''''''''''''''''''Rename Last consolidated as Old if exist'''''''''''''''''''''''''''''''''''
    Dim ws As Worksheet
    For Each ws In Sheets
        If InStr(1, ws.Name, "latest", vbTextCompare) <> 0 Then
            Sheets("Consolidated").Activate
            Range("D1") = ws.Name
            Range("D1").Interior.Color = 65535
            ws.Name = "old"
        End If
    Next

    ''''''''''''''''''''''''''''''''''''''''If Last Old exist then Map previous status'''''''''''''''''''''''''''''
    Dim Lastws As Worksheet
    For Each Lastws In Sheets
        If InStr(1, Lastws.Name, "old", vbTextCompare) <> 0 Then      
            Sheets("Consolidated").Activate
            Range("D2").Select

            Selection.FormulaArray = _
                "=IFERROR(INDEX(Old!C[-1],MATCH((RC[-2]&RC[-3]),(Old!C[-2]&Old!C[-3]),0),0),0)"

            Range("D2").AutoFill Destination:=Range("D2:D" & Range("A" & Rows.Count).End(xlUp).Row)

            ActiveSheet.Range("D2:D" & Range("D2:D" & Rows.Count).End(xlDown).Row).Copy
            Range("D2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            Application.CutCopyMode = False
            Sheets("old").Delete
        End If
    Next

    ActiveSheet.Name = "Latest Consolidated " & SheetName
    Range("D2").Select
End Sub

任何帮助都将不胜感激。

提前谢谢。

交叉帖子:https://chandoo.org/forum/threads/map-all-columns-if-data-exist-after-particular-column.39604/

交叉帖子:https://www.excelforum.com/showthread.php?t=1243321&p=4964474#post4964474

0 个答案:

没有答案