我尝试编写代码,在一个范围内插入值有3列和基于值数组的行。我需要将1到1000的值插入到我的范围中1,2,3下一行,4,5,6下一行,7,8,9下一行直到1000。 请有人帮助我,我试图在stackoverflow中找到但没有结果匹配
答案 0 :(得分:0)
如果你是关于Excel;
如果第一行是1:
Col1 = (ROW() - 1) * 3 + 1
,
Col2 = (Row() - 1) * 3 + 2
,
Col3 = (ROW() - 1) * 3 + 3
其他:@fr =第一行的行号:
Col1 = (ROW() - @fr) * 3 + 1
,
Col2 = (ROW() - @fr) * 3 + 2
,
Col3 = (ROW() - @fr) * 3 + 3
更好的方法是使用这个:(@ fr = 1,如果第一行是1)
=(ROW() - @fr) * 3 + COLUMN()
在VBA中添加:
Public Function myFunc(row as integer, col as integer, Optional fr As Integer = 1) As Integer
myFunc = (row - fr) * 3 + Col + 1
End Function
然后使用它。 对你:
Sub test2()
Dim i As Integer, row As Integer, col As Integer
a = 1
For i = 1 To 1000
row = (i - 1) \ 3 + 1
col = (i - 1) mod 3 + 1
Cells(row, col) = myfunc(row, col)
Next i
End Sub
答案 1 :(得分:0)
试试这个:
Sub test2()
Dim i As Integer, r As Integer
r = 1
For i = 1 To 1000
If i Mod 3 = 0 Then
Cells(r, 3) = i: r = r + 1
Else
Cells(r, i Mod 3) = i
End If
Next i
End Sub