您好我正在编写一个宏来比较excel中不同工作表上的两列.宏如下:
Sub Main()
Application.ScreenUpdating = False
Dim stNow As Date
stNow = Now
Dim arr As Variant
arr = Worksheets("Sheet2").Range("W3:W" & Range("W" & Rows.Count).End(xlUp).Row).Value
Dim varr As Variant
varr = Worksheets("Sheet3").Range("P3:P" & Range("P" & Rows.Count).End(xlUp).Row).Value
Dim x, y, match As Boolean
For Each x In arr
match = False
For Each y In varr
If x = y Then match = True
Next y
If Not match Then
Worksheets("Sheet1").Range("L" & Range("L" & Rows.Count).End(xlUp).Row + 1) = x
End If
Next …Run Code Online (Sandbox Code Playgroud) 我有一个VBA代码,它连接到用户表单
代码通过获取userform中的值来搜索列标题并使用这些标题填充列
我的问题是:我怎样才能避免重复代码?
Dim intBB As Integer
Dim rngBB As Range
intBB = 1
Do While ActiveWorkbook.Worksheets("Sheet1").Cells(1, intBB) <> ""
If ActiveWorkbook.Worksheets("Sheet1").Cells(1, intBB).Value = "Block" Then
With ActiveWorkbook.Worksheets("Sheet1")
Set rngBB = .Range(.Cells(1, intBB), .Cells(1, intBB))
End With
Exit Do
End If
intBB = intBB + 1
Loop
ActiveWorkbook.Worksheets("Sheet1").Range(Cells(2, intBB), Cells(LastRow, intBB)).Value = BlockBox.Value
intBB = 1
Do While ActiveWorkbook.Worksheets("Sheet1").Cells(1, intBB) <> ""
If ActiveWorkbook.Worksheets("Sheet1").Cells(1, intBB).Value = "HPL" Then
With ActiveWorkbook.Worksheets("Sheet1")
Set rngBB = .Range(.Cells(1, intBB), .Cells(1, intBB))
End With
Exit …Run Code Online (Sandbox Code Playgroud)