我的宏比较并将数据粘贴到指定的列。我希望它在下一个空列中比较和粘贴数据

时间:2017-11-15 15:32:28

标签: vba excel-vba excel

[The sheet from where the data is to be copied, columnB2:B]

As columnB is filled I want the data in Sheet1 ColumnB to be copied to the next empty column in Sheet 3

我的宏比较并将数据粘贴到指定的列。我希望它在下一个空列中比较和粘贴数据。在“If FR<> 0然后w2.Range(”B“& FR).Value = c.Offset(0,1)”“行中我希望“B”是空的,但我无法做到。

Sub UpdateW2()

    Dim w1 As Worksheet, w2 As Worksheet
    Dim c As Range, FR As Long
    Dim emptyColumn As Long

    Application.ScreenUpdating = False

    Set w1 = Sheet1
    Set w2 = Sheet2

    emptyColumn = w2.Cells(2, w2.Columns.Count).End(xlToLeft).Column
    If emptyColumn > 1 Then
        emptyColumn = emptyColumn + 1
    End If

    For Each c In w1.Range("B2", w1.Range("A" & Rows.Count).End(xlUp))
        FR = 0
        On Error Resume Next
        FR = Application.Match(c, w2.Columns("A"), 0)
        On Error GoTo 0
        If FR <> 0 Then w2.Range("B" & FR).Value = c.Offset(0, 1)
    Next c

    Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

 emptyColumn = w2.Cells(2, w2.Columns.Count).End(xlToLeft).Column
 If emptyColumn > 1 Then
       emptyColumn = emptyColumn + 1
 End If

这将返回第2行中没有右边条目的列。你想要的(我认为)是第一个根本没有任何数据的专栏。

 emptycolumn = w2.usedrange.columns.count +1