Excel VBA:创建一个嵌套循环并为范围内的每个值保存输出

Excel VBA: create a nested loop and save output for each value in range

我正在尝试创建一个嵌套循环,以将范围(“E2:E6”)中的值输入到单元格(“B3”)中,并将范围(“F2:F6”)中的值输入单元格(“B2” ).然后将Range("I2:j2")的结果记录到Sheet2.

这个答案 () 对首先解决问题很有帮助,但我一直被困在如何粘贴 Range("I2: j2") 到 Sheet2

非常感谢任何帮助!

    Sub Nested_Loop()
'
'
'
    
gg = 1

    Dim myRange As Range
    Dim myRange2 As Range
    Dim i As Long, j As Long, h As Long
    
    Worksheets("Sheet1").Activate
    Set myRange = Range("E2:E6")
    Set myRange2 = Range("F2:F6")
    For h = 1 To myRange2.Rows.Count
            For i = 1 To myRange.Rows.Count
                For j = 1 To myRange.Columns.Count
                myRange.Cells(i, j).Select
        Selection.Copy
        Range("B3").Select
        ActiveSheet.Paste
            myRange2.Cells(h, j).Select
        Selection.Copy
        Range("B2").Select
        ActiveSheet.Paste
        Range("I2:j2").Select
        Application.CutCopyMode = False
        Selection.Copy
    Worksheets("Sheet2").Activate
    Worksheets("Sheet2").Cells(i + 1, j + gg).Select     'I want to paste all 25 values (5 possible inputs for each variable(2)). Currently the loop only prints 5 results and then pastes over itself
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
      Next j
      Worksheets("Sheet1").Activate
   Next i
Worksheets("Sheet1").Activate

Next h

End Sub 

尽量避免 .Select & .Activate。而是正确地声明 wb、ws 和 rng,我们也可以不用 copy/paste(当然,如果您只对值感兴趣)。有关指导,请参阅:How to avoid using Select in Excel VBA。使用 rng1.value = rng2.value。快多了。

我认为您对需要多少个循环有点困惑。我认为您只需要一个用于 myRange 和一个嵌套的用于 myRange2(都在行上)。

最后,尽量使用“合乎情理”的变量名;这有助于您的用户(例如 counter 而不是 gg)。

下面的代码应该可以正常工作。 (也许是一个很好的挑战,看看你是否可以简单地在代码中包含显然在 ws.Range("I2:J2") 中进行的计算......)

Sub Nested_Loop()

    Dim wb As Workbook
    Dim ws As Worksheet, ws2 As Worksheet
    
    Dim myRange As Range, myRange2 As Range, destRange As Range
    
    Dim i As Long, j As Long, counter As Long
    
    Set wb = ActiveWorkbook
    
    Set ws = wb.Sheets("Sheet1")
    Set ws2 = wb.Sheets("Sheet2")
    
    Set myRange = ws.Range("E2:E6")
    Set myRange2 = ws.Range("F2:F6")
    
    'set destination range; "B2" as start inferred from original code
    Set destRange = ws2.Range("B2").Resize(25, 2) 'i.e. .Resize(5*5=25rows,1+1=2cols)
    
    counter = 0
    
    'loop first range = 1 to 5
    For i = 1 To myRange.Rows.Count
        
        'nested -> 5*5
        For j = 1 To myRange2.Rows.Count
        
            'use .value = .value instead of copy/paste
            ws.Range("B3").Value = myRange.Cells(i).Value
            ws.Range("B2").Value = myRange2.Cells(j).Value
            
            'with .value = .value we don't need to leave the activeworksheet
            Range(destRange.Cells(1 + counter, 1), destRange.Cells(1 + counter, 2)).Value = ws.Range("I2:J2").Value
            
            'increment counter to go to next row in destRange
            counter = counter + 1
            
        Next j

    Next i

End Sub

一些适合您的项目:

dim ws1 as worksheet
set ws1 = thisWorkbook.Sheets(1)
dim ws2 as worksheet
set ws2 = thisWorkbook.Sheets(2)
dim rowNum as long
for rowNum = 1 to 5
    dim colNum as long
    for colNum = 1 to 5
        With ws1
            .Cells(rowNum,colNum).Copy .Cells(2,2)
            .Range(.Cells(2,9),.Cells(2,9+1)).Copy ws2.Range(ws2.Cells(rowNum,colNum*2),ws2.Cells(rowNum,colNum*2+1)
        End With
    next colNum
next rowNum
  • 使用有意义的变量;现在养成习惯,这样你就不会在以后纠正 A、J、K、I 等
  • 确定你所有的范围;请注意 ws2 上的目的地比我的 With ws1
  • 上的目的地复杂得多
  • 我对粘贴目标范围内的列号使用了乘法,这将防止覆盖数据