我有一个设置为输出如下数据:
我想创建一个循环,
这里是我遇到的麻烦的代码....
Sub CopyPasteDex()
Dim wksDest As Worksheet
Dim wksSource As Worksheet
Dim Rngsource As Range
Dim NextRow As Long
Dim LastRow As Long
Dim LastCol As Long
Dim c As Long
Application.ScreenUpdating = False
Set wksSource = Worksheets("Sheet1")
Set wksDest = Worksheets("Sheet2")
With wksDest
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
With wksSource
LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column
For c = 2 To LastCol Step 7
LastRow = .Cells(.Rows.Count, c).End(xlUp).Row
Set Rngsource = .Range(.Cells(3, c), .Cells(LastRow, c + 3))
Rngsource.Copy
wksDest.Range("A:A").PasteSpecial.xlPasteValues
NextRow = NextRow + Rngsource.Rows.Count
Next c
End With
Application.ScreenUpdating = True
End Sub
这似乎为我工作。
Sub CopyPasteDex()
Dim wksDest As Worksheet
Dim wksSource As Worksheet
Dim Rngsource As Range
Dim NextRow As Long
Dim LastRow As Long
Dim LastCol As Long
Dim c As Long
Application.ScreenUpdating = False
Set wksSource = Worksheets("Sheet1")
Set wksDest = Worksheets("Sheet2")
With wksDest
NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
With wksSource
LastCol = .Cells(3, .Columns.Count).End(xlToLeft).Column
For c = 2 To LastCol Step 8 ' make sure that the step is changed to 8 here.
LastRow = .Cells(.Rows.Count, c).End(xlUp).Row
Set Rngsource = .Range(.Cells(3, c), .Cells(LastRow, c + 3))
Rngsource.copy
wksDest.Cells(NextRow, 1).PasteSpecial xlPasteValues ' Note that I've changed .range to .cells and changed the destination row to NextRow
NextRow = NextRow + Rngsource.Rows.Count
Next c
End With
Application.ScreenUpdating = True
End Sub