【问题标题】:How to update range for test in loop that is changing range如何在改变范围的循环中更新测试范围
【发布时间】:2018-02-28 17:43:45
【问题描述】:

我希望我的循环测试正在寻找值的单元格。问题是我的范围正在改变,因为循环正在添加行。如何更新我的设定范围?

WorkRng1WorkRng2 是用输入框设置的公共变量

到目前为止,这是我的代码:

Dim foundRange As Range
Dim manualRng As Range
Dim LastRow As Long
matchCounter = 0
On Error Resume Next
Application.ScreenUpdating = False
    For Each Rng2 In WorkRng2
        If Rng2.Interior.Color = VBA.RGB(255, 0, 0) And Rng2.Value > 0 Then
                blkRow.Copy
            LastRow = Range(WorkRng1.Column & Rows.Count).End(xlUp).Row 'tests for last filled row in range 1
            Set foundRange = WorkRng1.Find(Rng2.Offset(-1, 0).Value, LookIn:=xlValues)
            If foundRange Is Nothing Then
            Application.ScreenUpdating = True
                MsgBox "Cannot find where to put " & Rng2, vbInformation, "OOPS!"
                Set manualRng = Application.InputBox("Please select the cell where you would like to add " & Rng2, "LOCATION PLEASE", Type:=8)
                manualRng.Select
                Selection.EntireRow.Insert Shift:=xlDown
                    Application.CutCopyMode = False
            Application.ScreenUpdating = False
                Else
            WorkRng1.Find(Rng2.Offset(-1, 0).Value, LookIn:=xlValues).Select
                ActiveCell.Offset(1, 0).Select
                Selection.EntireRow.Insert Shift:=xlDown
                    Application.CutCopyMode = False
            End If

我想我会添加一个lastRow 测试,但我坚持使用范围的语法。任何帮助将不胜感激。

【问题讨论】:

  • 使用计数器向后循环。

标签: excel loops range vba


【解决方案1】:

对于那些感兴趣的人,我通过将找到的范围设置为整个列来解决这个问题。这是我的工作代码:

Dim matchCounter As Integer
Dim foundRange As Range
Dim manualRng As Range
Dim lastRow As Long
matchCounter = 0
On Error Resume Next
Application.ScreenUpdating = False
    For Each Rng2 In WorkRng2
        If Rng2.Interior.Color = VBA.RGB(255, 0, 0) And Rng2.Value > 0 Then
                blkRow.Copy
            lastRow = WorkRng1.SpecialCells(xlCellTypeLastCell).Row 'tests for last filled row in range 1
            Set foundRange = Range(Cells(1, WorkRng1.Column), Cells(lastRow, WorkRng1.Column)).Find(Rng2.Offset(-1, 0).Value, LookIn:=xlValues)
            If foundRange Is Nothing Then
            Application.ScreenUpdating = True
                MsgBox "Cannot find where to put " & Rng2, vbInformation, "OOPS!"
                Set manualRng = Application.InputBox("Please select the cell where you would like to add " & Rng2, "LOCATION PLEASE", Type:=8)
                blkRow.Copy
                manualRng.Select
                Selection.EntireRow.Insert Shift:=xlDown
                    Application.CutCopyMode = False
            Application.ScreenUpdating = False
                Else
            WorkRng1.Find(Rng2.Offset(-1, 0).Value, LookIn:=xlValues).Select
                ActiveCell.Offset(1, 0).Select
                Selection.EntireRow.Insert Shift:=xlDown
                    Application.CutCopyMode = False
            End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多