如何使用设置范围
How to use set ranges
我正在寻找一种方法来改进一种更好的方法来编写一段代码,该代码将查看 "Second List" 中是否有来自 "First List" 的客户端并将数据复制到一个名为 "Found".
的 sheet
事情是这样的:
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
这是我实际代码的简化版本(它更大并且包含许多循环的早期退出以及按字母顺序组织),但现在我主要担心使用“.select" 函数占用了我大部分的处理时间。
我知道我已经看到有另一种方法使用
Set rng = Range(Cells(2, prtGtId),Cells(Cells(Rows.Count, GtId2).End(xlUp).row, prtGtId))
For Each Cell in rng
Code
Next Cell
或类似的东西,但我似乎找不到比 "It's much faster this way" 更进一步的详细教程。
考虑到我需要在将它们复制到 "Found" 时保留行中的格式,有没有办法改变它以使其更快?
让我举一个简短的例子,说明您可以使用您提供的代码执行哪些操作。而不是 Rows(row2).Select 你也可以写 Set CurrentRow = Rows(row2)
当然,您必须事先将 CurrentRow 声明为一个范围(Dim CurrentRow as Range)
还建议在使用 Set CurrentRow = Nothing 完成后释放 CurrentRow 变量
您唯一需要知道的是,您在 Set 语句中放在等号后面的内容应该生成一个范围对象。
大多数时候使用 .Select 的代码行都可以重写为不是 select 项目,而是例如将其放入变量中。这通常会加快代码速度,并且您不必在代码完成后重置 selection。
希望对您有所帮助
也许你可以用第一个列表中的所有 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
Activate 和 select 模拟用户击键,即使您将 Application.ScreenUpdating 设置为 false,您也不需要真正的 select 对象。您通常应该避免使用这些方法(请参阅此处关于为什么以及何时使用 select:http://dailydoseofexcel.com/archives/2004/04/27/beginning-vba-select-and-activate/ 的有趣文章)。声明变量(设置 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
你看到的几行少了,没有多了select。另请参阅 "with" 语句(此处用作示例)的工作原理,这可能非常有用。
(我假设你在 sheet "First List" 激活的情况下启动你的宏,这就是我添加 Sheets("First List") 的原因)以这种方式编程也可以避免这种错误(这样你就可以在没有启动宏的情况下启动宏担心 sheet 激活)
我正在寻找一种方法来改进一种更好的方法来编写一段代码,该代码将查看 "Second List" 中是否有来自 "First List" 的客户端并将数据复制到一个名为 "Found".
的 sheet事情是这样的:
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
这是我实际代码的简化版本(它更大并且包含许多循环的早期退出以及按字母顺序组织),但现在我主要担心使用“.select" 函数占用了我大部分的处理时间。
我知道我已经看到有另一种方法使用
Set rng = Range(Cells(2, prtGtId),Cells(Cells(Rows.Count, GtId2).End(xlUp).row, prtGtId))
For Each Cell in rng
Code
Next Cell
或类似的东西,但我似乎找不到比 "It's much faster this way" 更进一步的详细教程。
考虑到我需要在将它们复制到 "Found" 时保留行中的格式,有没有办法改变它以使其更快?
让我举一个简短的例子,说明您可以使用您提供的代码执行哪些操作。而不是 Rows(row2).Select 你也可以写 Set CurrentRow = Rows(row2) 当然,您必须事先将 CurrentRow 声明为一个范围(Dim CurrentRow as Range) 还建议在使用 Set CurrentRow = Nothing 完成后释放 CurrentRow 变量 您唯一需要知道的是,您在 Set 语句中放在等号后面的内容应该生成一个范围对象。 大多数时候使用 .Select 的代码行都可以重写为不是 select 项目,而是例如将其放入变量中。这通常会加快代码速度,并且您不必在代码完成后重置 selection。 希望对您有所帮助
也许你可以用第一个列表中的所有 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
Activate 和 select 模拟用户击键,即使您将 Application.ScreenUpdating 设置为 false,您也不需要真正的 select 对象。您通常应该避免使用这些方法(请参阅此处关于为什么以及何时使用 select:http://dailydoseofexcel.com/archives/2004/04/27/beginning-vba-select-and-activate/ 的有趣文章)。声明变量(设置 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
你看到的几行少了,没有多了select。另请参阅 "with" 语句(此处用作示例)的工作原理,这可能非常有用。 (我假设你在 sheet "First List" 激活的情况下启动你的宏,这就是我添加 Sheets("First List") 的原因)以这种方式编程也可以避免这种错误(这样你就可以在没有启动宏的情况下启动宏担心 sheet 激活)