Excel 如果循环不工作,VBA是否简单?

Excel 如果循环不工作,VBA是否简单?,excel,vba,Excel,Vba,不确定发生了什么,但我不能得到一个简单的For-If循环在VBA中完全起作用。以下代码仅粘贴AH=“Changed”的第一个实例,而不粘贴范围内的其他实例。有什么想法吗 Dim x As Integer ' Defining variables to be used Dim b As Integer Dim ClaimsDB As Integer ClaimsDB = Evaluate("CountA(B18:B28)") 'Counting how many rows in the spec

不确定发生了什么,但我不能得到一个简单的For-If循环在VBA中完全起作用。以下代码仅粘贴AH=“Changed”的第一个实例,而不粘贴范围内的其他实例。有什么想法吗

Dim x As Integer ' Defining variables to be used
Dim b As Integer
Dim ClaimsDB As Integer

ClaimsDB = Evaluate("CountA(B18:B28)") 'Counting how many rows in the specified range are empty, in order to only run the loop a specified amount of times

'Copy data that has been marked "changed" in a previous worksheet to a different worksheet

For b = 1 To ClaimsDB 

    If Range("AH" & b + 17).Value = "Changed" Then 'AH(b+17) is marked "Change" if a change has been made - I only want the rows with "Changed" in AH to be copied

        x = Sheets("Change register").Range("B" & b + 17).Value 'Define x as equal to the value in column B, row b+17, for a row with AH = "Changed"

        'Copy row with AH = "Changed" from E:AF
        Sheets("Change register").Range("E" & b + 17).Select 
        Range(Selection, Range("AF" & b + 17)).Select
        Selection.Copy

        'Switch worksheets to product data, and paste the selection in a row corresponding to x+12, starting in column D
        Sheets("Product data").Select
        Sheets("Product data").Cells((x + 12), 4).Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    End If

Next b ' Repeat the loop for the next value of b, until ClaimsDB has been reached
请尝试以下操作:

Option Explicit
Sub CopyChangedRows()
    'Integer is stored as Long anyway, so it is better to use Long
    'also i is usually used as iterator variable :)
    Dim x As Long, i As Long, ws1 As Worksheet, ws2 As Worksheet
    'always hold references to your sheets (when using multiple sheeets) in variable to keep code clean
    Set ws1 = Sheets("Change register")
    Set ws2 = Sheets("Product data")

    'Copy data that has been marked "changed" in a previous worksheet to a different worksheet
    For i = 18 To 28
        'loop will skip empty cells, as they value will be different from "Changed"
        If ws1.Range("AH" & i).Value = "Changed" Then
            x = ws1.Range("B" & i).Value

            'Copy row with AH = "Changed" from E:AF
            ws1.Range(ws1.Range("E" & i), ws1.Range("AF" & i)).Copy

            'Switch worksheets to product data, and paste the selection in a row corresponding to x+12, starting in column D
            'whole copied range will be pasted starting in specified range
            ws2.Range("D" & x + 12).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        End If
    Next
End Sub

仍然不起作用,不幸的是,仅仅用“Change”复制第一行只是尝试将b=1更改为10,但仍然不起作用-对身体本身有什么建议吗?看起来它没有在bWell周围正常循环。。。。很多建议。。。切勿使用
选择
选择
!在VBA代码中也使用显式选项。但是不知道你想做什么,我不能给你任何具体的答案。谢谢你,迈克尔!我添加了显式选项。你能建议一种不使用select/selection编写代码的方法吗?我还是VBA新手,不知道有什么好的解决办法。@arobNYC我对你的代码进行了一点重构,试试看:)在代码中还添加了一些注释。如您所见,无
Select
s或
Selection
s。