0
我在創建多個/嵌套循環時遇到問題。我可以編寫一段代碼,並重復17次,每次都稍作修改,但我試圖避免這種情況。Excel中的多個循環VBA
我的代碼看起來像下面這樣,其他一切都很好,除了最後添加'循環所需的部分。所以基本上循環應該包括「AJ2,AK2,...,AZ2」和「1,2,...,17」。
任何幫助將不勝感激。這幾個星期我一直在努力。
Sub Test()
Worksheets("SheetName").Activate
Range("A1").CurrentRegion.Copy
Worksheets("NewSheet").Activate
Range("A1").PasteSpecial
Range("A1").PasteSpecial xlPasteColumnWidths
Dim c As Range
Dim col1 As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet
Dim lastrow As Long
Set Source = ActiveWorkbook.Worksheets("SheetName")
Set Target = ActiveWorkbook.Worksheets("NewSheet")
Set col1 = Source.Range("SheetName[Col]")
j = 2
For Each c In col1
If c >= Range("AJ2") Then 'loop needed with "AJ2"
Source.Range("A" & c.Row & ":BN" & c.Row).Copy
Target.Range("A" & Range("E65536").End(xlUp).Row + 1).PasteSpecial xlPasteValues
Target.Range("BO" & Rows.Count).End(xlUp).Value = DateAdd("m", 1, Range("BO2").Value) 'loop needed with 1
j = Range("A65536").End(xlUp).Row + 1
End If
Next c
For Each c In col1
If c >= Range("AK2") Then 'loop needed with "AK2"
Source.Range("A" & c.Row & ":BN" & c.Row).Copy
Target.Range("A" & Range("E65536").End(xlUp).Row + 1).PasteSpecial xlPasteValues
Target.Range("BO" & Rows.Count).End(xlUp).Value = DateAdd("m", 2, Range("BO2").Value) 'loop needed with 2
j = Range("A65536").End(xlUp).Row + 1
End If
Next c
End Sub
編寫一個帶'Range(「AJ2」)'&c的*函數*。作爲*參數* – Bathsheba