应该只用 1 个 for 循环吧?
- Sub FillTable()
- Dim ws As Worksheet
- Set ws = ActiveSheet '或者指定工作表,例如Set ws = Worksheets("Sheet1")
-
- Dim col1 As Range, col2 As Range, col3 As Range
- Set col1 = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).row)
- Set col2 = ws.Range("B1:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).row)
- Set col3 = ws.Range("C1:C" & ws.Cells(ws.Rows.Count, "C").End(xlUp).row)
-
- Dim row As Long, col As Long
- row = 1
- col = 5
-
- Dim i As Long, j As Long, m As Long
- For i = 1 To col1.Rows.Count '不一定是221, 根据实际长度更改
- ws.Cells(row, col).Value = col1.Cells(i).Value
- col = col + 1
- ws.Cells(row, col).Value = col2.Cells(i).Value
- col = col + 1 '每次填充三列,列数+3
- ws.Cells(row, col).Value = col3.Cells(i).Value
- col = col + 1
- If col > 13 Then '如果超过了9列,换行继续填充
- row = row + 1
- col = 5
- End If
- Next i
-
- End Sub
复制代码 |