我想使用 VBA 比较 Excel 中的 2 到 1 列..
我已经使用此代码实现了 2 到 2 列
Sub LIST1_LIST2()
Dim list1 As Range
Dim LIST2 As Range
Set list1 = Range("B3:C181")
Set LIST2 = Range("G3:H729")
For Each row1 In list1.Rows
For Each row2 In LIST2.Rows
If (row1.Cells(1) = row2.Cells(1) And row1.Cells(2) = row2.Cells(2)) Then
row1.Cells(1).Offset(0, 2) = row1.Cells(1)
row1.Cells(2).Offset(0, 2) = row1.Cells(2)
Exit For
End If
Next row2
Next row1
End Sub
使用一个简单的 Do until 循环:
Option Explicit
Public Sub Example()
Dim B As Range, _
C As Range, _
D As Range, _
F As Range, _
G As Range
Dim i%, x% ' Dim as long
Set B = Columns(2)
Set C = Columns(3)
Set D = Columns(4)
Set F = Columns(6)
Set G = Columns(7)
i = 2
x = 2
Do Until IsEmpty(B.Cells(i))
Debug.Print B.Cells(i).Value & ", " & _
C.Cells(i).Value ' Print on Immed Win
Do Until IsEmpty(F.Cells(x))
DoEvents ' For testing
If F.Cells(x).Value = B.Cells(i).Value & ", " & _
C.Cells(i).Value Then
Debug.Print F.Cells(i).Value = B.Cells(i).Value & ", " & _
C.Cells(i).Value ' Print on Immed Win
G.Cells(x) = D.Cells(i)
x = 2 ' Reset Loop
Exit Do
End If
x = x + 1
Loop
i = i + 1
Loop
End Sub
其他信息
DoEvents对于简单的事情最有用,比如允许用户在进程开始后取消它,例如搜索文件。对于长时间运行的进程,通过使用 Timer 或将任务委托给 ActiveX EXE 组件来更好地完成处理器。在后一种情况下,任务可以继续完全独立于您的应用程序,并且操作系统采用多任务处理的情况和时间切片。
Debug.Print Immediate Window用于调试和评估表达式、执行语句、打印变量值等。它允许您在调试期间输入要由开发语言计算或执行的表达式。要显示“立即”窗口,请打开一个项目进行编辑,然后从“调试”菜单中选择“Windows”并选择“立即”,或按 CTRL+ALT+I。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句