【发布时间】:2014-10-21 17:07:38
【问题描述】:
我有一个宏来检查 sheet1 列 A 中的值是否与 sheet2 中同一列中的值匹配,如果是,它将每个匹配值的相邻单元格从 sheet1 复制到 sheet2 中。以下是我到目前为止的内容,但我在 lastrowadd 行上不断收到“运行时错误 9”,但不知道为什么。任何帮助将不胜感激:)
Sub CopyAdjacent()
Dim i As Long, j As Long, colStatus As Long, lastrowAdd As Long, lastrowRemove As Long
colStatus = 2 'your status column number
lastrowAdd = Sheets(“Sheet1”).Cells(Sheets(“Sheet1”).Rows.Count, 1).End(xlUp).Row
lastrowRemove = Sheets(“Sheet2”).Cells(Sheets(“Sheet2”).Rows.Count, 1).End(xlUp).Row
For i = 1 To lastrowAdd
For j = 1 To lastrowRemove
If Sheets(“Sheet1”).Cells(i, 1).Value = Sheets(“Sheet2”).Cells(j, 1).Value Then
Sheets(“Sheet2”).Cells(j, colStatus).Value = Sheets(“Sheet1”).Cells(i, colStatus).Value
End If
Next j
Next i
End Sub
【问题讨论】:
-
Sheets(“Sheet1”).Cells(Sheets(“Sheet1”).Rows.Count, 1).End(xlUp).Row尝试将其更改为Sheets(“Sheet1”).Rows.Count-1看看会发生什么 -
它仍然给出相同的结果。