來完成這個任務,你必須使用循環。我將與您分享一個工作示例,其中包含允許您爲表格設置參數的變量,同時還顯示了命令For和While命令的一個示例。如果您對代碼的某些特定部分有任何疑問,請不要猶豫。
Sub DataToTable()
Dim actualDataRow As Long
Dim tablesPerRow As Integer
Dim actualRowTable As Integer
Dim actualRow As Integer
Dim initialTableRow As Integer
Dim initialTableColumn As Integer
tablesPerRow = 3
actualRow = 0
actualRowTable = 0
initialTableRow = 2
initialTableColumn = 4
actualDataRow = 1
'Style columns (outside for)
Range(Columns(initialTableColumn), Columns(initialTableColumn + (tablesPerRow * 4))).ColumnWidth = 4.67
While Not IsEmpty(Cells(actualDataRow, 1))
'Style table
Range(Cells(initialTableRow + (actualRow * 4) - 1, initialTableColumn + (actualRowTable * 4) - 1), Cells(initialTableRow + (actualRow * 4) + 1, initialTableColumn + (actualRowTable * 4) + 1)).Borders.LineStyle = xlContinuous
Range(Cells(initialTableRow + (actualRow * 4) - 1, initialTableColumn + (actualRowTable * 4) - 1), Cells(initialTableRow + (actualRow * 4) - 1, initialTableColumn + (actualRowTable * 4) + 1)).Interior.ColorIndex = 27
Range(Cells(initialTableRow + (actualRow * 4) - 1, initialTableColumn + (actualRowTable * 4) - 1), Cells(initialTableRow + (actualRow * 4) + 1, initialTableColumn + (actualRowTable * 4) - 1)).Interior.ColorIndex = 27
'Insert table data
For x = 0 To 1
For y = 0 To 1
Cells(initialTableRow + (actualRow * 4) + x, initialTableColumn + (actualRowTable * 4) + y) = Cells(actualDataRow, 1)
actualDataRow = actualDataRow + 1
Next y
Next x
If actualRowTable >= tablesPerRow - 1 Then
actualRowTable = 0
actualRow = actualRow + 1
Else
actualRowTable = actualRowTable + 1
End If
Wend
End Sub
最好的問候,
安倍
來源
2015-05-06 23:22:51
Abe
沒有時間來寫一個樣本,但我會用兩個'For'循環和選擇範圍的'Cells'方法嘗試。 – FairlyLegit
當您有空時,請您提供樣品嗎?我不熟悉和困惑如何使用循環。謝謝! –
你的號碼是否總是適合右邊的方格(即4的冪數)? – Alex