Vba 如何使用设定范围
我正在寻找一种更好的方法来改进,编写一段代码,查看“第二个列表”中是否有来自“第一个列表”的客户端,并将数据复制到名为“find”的工作表中 事情是这样的:Vba 如何使用设定范围,vba,excel,select,range,Vba,Excel,Select,Range,我正在寻找一种更好的方法来改进,编写一段代码,查看“第二个列表”中是否有来自“第一个列表”的客户端,并将数据复制到名为“find”的工作表中 事情是这样的: Dim row As Long, row2 As Long, found as Long Dim ID As String, prtGtId as String, GtId2 as String Application.ScreenUpdating = False prtGtId = "B" GtId2 = "D" row = 2 row2
Dim row As Long, row2 As Long, found as Long
Dim ID As String, prtGtId as String, GtId2 as String
Application.ScreenUpdating = False
prtGtId = "B"
GtId2 = "D"
row = 2
row2 = 2
found = 0
Do While row <= Cells(Rows.Count, prtGtId).End(xlUp).row
ID = Cells(row, prtGtId)
Sheets("Second List").Select
Do While row2 <= Cells(Rows.Count, GtId2).End(xlUp).row
If (ID = Cells(row2, GtId2)) Then
Rows(row2).Select
Selection.Copy
Sheets("Found").Select
Rows(2).Select
Selection.Insert Shift:=xlDown
Sheets("First List").Select
Rows(row).Select
Selection.Copy
Sheets("Found").Select
Rows(2).Select
Selection.Insert Shift:=xlDown
Sheets("Second List").Select
found = found + 1
End If
row2 = row2 + 1
Loop
Sheets("First List").Select
row = row + 1
row2 = 2
Loop
Sheets("Blank").Select
Cells(2, 3) = found
Application.ScreenUpdating = True
或者类似的东西,但我似乎找不到比“这种方式快得多”更详细的教程
考虑到我需要在将行复制到“find”时保留行中的格式,是否有办法更改此格式以加快速度?让我给您举一个简短的示例,说明您可以在提供的代码中执行哪些操作。而不是行(第2行)。选择此选项还可以写入Set CurrentRow=Rows(第2行) 当然,您必须事先将CurrentRow声明为一个范围(Dim CurrentRow as range) 当您使用Set CurrentRow=Nothing完成CurrentRow变量时,最好释放它 您需要知道的唯一一件事是,在Set语句中放在等号后面的内容应该生成一个range对象。 大多数使用.Select的代码时间行可以重写为不选择项,而是将其放入变量中。这通常会加快代码的速度,并且在完成代码后不必重置选择。
我希望这会有所帮助让我给你一个简短的例子,说明你在提供的代码中可以做些什么。而不是行(第2行)。选择此选项还可以写入Set CurrentRow=Rows(第2行) 当然,您必须事先将CurrentRow声明为一个范围(Dim CurrentRow as range) 当您使用Set CurrentRow=Nothing完成CurrentRow变量时,最好释放它 您需要知道的唯一一件事是,在Set语句中放在等号后面的内容应该生成一个range对象。 大多数使用.Select的代码时间行可以重写为不选择项,而是将其放入变量中。这通常会加快代码的速度,并且在完成代码后不必重置选择。
我希望这有帮助也许您可以使用第一个列表中的所有ID创建一个数组,然后检查是否在第二个列表中找到相同的ID,并使用双ID的行号创建一个数组 使用最后一个数组,您可以对所需的所有行进行一次大选择,然后一次复制所有行
我不知道这是否会加快速度,但也许你可以试试。也许你可以用第一个列表中的所有ID创建一个数组,然后检查是否在第二个列表中找到相同的ID,并用双ID的行号创建一个数组 使用最后一个数组,您可以对所需的所有行进行一次大选择,然后一次复制所有行
我不知道这是否会加快速度,但也许你可以试试。我会完全删除Select语句。试试这个,不需要范围
If (ID = Cells(row2, GtId2)) Then
Sheets("Second List").Rows(row2).Copy
Sheets("Found").Rows(2).Insert Shift:=xlDown
Sheets("First List").Rows(rw).Copy
Sheets("Found").Rows(2).Insert Shift:=xlDown
found = found + 1
End If
我将完全删除Select语句。试试这个,不需要范围
If (ID = Cells(row2, GtId2)) Then
Sheets("Second List").Rows(row2).Copy
Sheets("Found").Rows(2).Insert Shift:=xlDown
Sheets("First List").Rows(rw).Copy
Sheets("Found").Rows(2).Insert Shift:=xlDown
found = found + 1
End If
激活并选择“模拟用户击键”,即使将Application.ScreenUpdate设置为false,也不需要真正选择对象。通常应避免使用这些方法(请参阅此处一篇关于选择原因和时间的有趣文章:)。声明变量(设置rng…)或直接处理对象
Sub test()
Dim row As Long, row2 As Long, found As Long
Dim ID As String, prtGtId As String, GtId2 As String
Application.ScreenUpdating = False
prtGtId = "B"
GtId2 = "D"
row = 2
row2 = 2
found = 0
Do While row <= Sheets("First List").Cells(Rows.Count, prtGtId).End(xlUp).row
ID = Sheets("First List").Cells(row, prtGtId)
' Sheets("Second List").Select
With Sheets("Second List")
Do While row2 <= .Cells(Rows.Count, GtId2).End(xlUp).row
If (ID = .Cells(row2, GtId2)) Then
.Rows(row2).Copy
Sheets("Found").Rows(2).Insert Shift:=xlDown
Sheets("First List").Rows(row).Copy
Sheets("Found").Rows(2).Insert Shift:=xlDown
found = found + 1
End If
row2 = row2 + 1
Loop
End With
' Sheets("First List").Select
row = row + 1
row2 = 2
Loop
Sheets("Blank").Cells(2, 3) = found
Application.ScreenUpdating = True
End Sub
子测试()
暗行等长,行2等长,发现等长
Dim ID作为字符串,prtGtId作为字符串,GtId2作为字符串
Application.ScreenUpdating=False
prtGtId=“B”
GtId2=“D”
行=2
行2=2
找到=0
当行激活并选择“模拟用户击键”时,即使将Application.ScreenUpdate设置为false,也不需要真正选择对象。通常应避免使用这些方法(请参阅此处一篇关于选择原因和时间的有趣文章:)。声明变量(设置rng…)或直接处理对象
Sub test()
Dim row As Long, row2 As Long, found As Long
Dim ID As String, prtGtId As String, GtId2 As String
Application.ScreenUpdating = False
prtGtId = "B"
GtId2 = "D"
row = 2
row2 = 2
found = 0
Do While row <= Sheets("First List").Cells(Rows.Count, prtGtId).End(xlUp).row
ID = Sheets("First List").Cells(row, prtGtId)
' Sheets("Second List").Select
With Sheets("Second List")
Do While row2 <= .Cells(Rows.Count, GtId2).End(xlUp).row
If (ID = .Cells(row2, GtId2)) Then
.Rows(row2).Copy
Sheets("Found").Rows(2).Insert Shift:=xlDown
Sheets("First List").Rows(row).Copy
Sheets("Found").Rows(2).Insert Shift:=xlDown
found = found + 1
End If
row2 = row2 + 1
Loop
End With
' Sheets("First List").Select
row = row + 1
row2 = 2
Loop
Sheets("Blank").Cells(2, 3) = found
Application.ScreenUpdating = True
End Sub
子测试()
暗行等长,行2等长,发现等长
Dim ID作为字符串,prtGtId作为字符串,GtId2作为字符串
Application.ScreenUpdating=False
prtGtId=“B”
GtId2=“D”
行=2
行2=2
找到=0
请不要对MS Office或VBA使用[macros]。请不要将[宏]用于MS Office或VBA。