【发布时间】:2014-08-15 06:15:50
【问题描述】:
我正在为工作编写 excel 宏,但遇到了麻烦。在这种情况下,有两个工作表,“BU”和“TOPS 信息”。使用宏时,它应该在“BU”的每一行中搜索“TOPS 信息”中找到的值,然后转到“TOPS 信息”的下一行并重复该过程。如果找到正确的匹配项,则应该是复制一个单元格并将其粘贴到“TOPS 信息”中。
代码如下:
Sub QIM()
Dim j As Integer
Dim k As Integer
Dim i As Integer
Dim l As Integer
Dim m As Integer
Dim searchArray(1 To 3) As String
j = 0
k = 1
'WARNING: Temporary Sheet Names
lastRowTOPS = Worksheets("TOPS Information").Cells(Rows.Count, "A").End(xlUp).Row
lastRowBU = Worksheets("BU").Cells(Rows.Count, "A").End(xlUp).Row
'Cycle through BU rows
For j = lastRowTOPS To 1 Step -1
'Cycle through searchArray for each BU row
For k = lastRowBU To 1 Step -1
'//////////////////////////////////////
x = Sheets("BU").Range("B" & k).Value
y = Range("C" & j).Value
If StrComp(x, y) = 1 Then
Sheets("BU").Range("C" & k).Copy
Range("H" & j).PasteSpecial
End If
'//////////////////////////////////////
Next k
Next j
End Sub
这个宏显然只有在当时选择了“TOPS 信息”的情况下才有效。任何和所有的帮助将不胜感激。谢谢!
【问题讨论】:
-
[office.microsoft.com/en-us/excel-help/… 可能就是你要找的东西
-
链接断开。 vlookup
-
工作表
BUB 列中是否会有重复值?您的代码最终将复制最上面的“发现”。如果 col B 数据是唯一的,那么Exit For以减少执行时间。您应该考虑使用 2 个工作表变量和 2 个范围变量。