【问题标题】:My macro compares and pastes data to the specified column. I want it to compare and paste the data in the next empty column我的宏比较数据并将其粘贴到指定的列。我希望它在下一个空列中比较和粘贴数据
【发布时间】:2017-11-15 23:56:27
【问题描述】:

[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 Sheet3

我的宏比较数据并将其粘贴到指定的列。我希望它比较数据并将其粘贴到下一个空列中。在“如果 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

【问题讨论】:

  • 欢迎来到SO,我找不到明确的问题,你能修改你的帖子并定义一个要解决的问题吗?请阅读>How to Ask
  • "我希望它比较数据并将其粘贴到下一个空列中" "我希望 "B" 为 emptyColumn 但我做不到那个”。您是否要粘贴到下一个空列中?或者B列,不知道你想要什么
  • 另外,我注意到在您的提问行中的代码中,它缺少End If 声明。也许这就是您的问题所在?
  • @Maldred 我希望它把它粘贴到下一个空列中。
  • @Maldred 我的问题在于“If FR 0 Then w2.Range("B" & FR).Value = c.Offset(0, 1)”这一行。我希望将“B”写为 emptyColumn

标签: vba excel


【解决方案1】:
 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

【讨论】:

  • 我的问题在于“If FR 0 Then w2.Range("B" & FR).Value = c.Offset(0, 1)”这一行。我希望将“B”写为 emptyColumn
  • "If FR 0 Then w2.cells(FR,emptycolumn).Value = c.Offset(0, 1)"
  • 如果这解决了您的问题,请将其标记为已回答
猜你喜欢
  • 2022-07-21
  • 1970-01-01
  • 2017-11-20
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多