我想在Excel中用1填充从1到n(比如n = 2000)的列。无论如何都要用excel编程才能得到这个?
例如:4行和10000列。第一列2000列在第一行中有1列,下一页2000在第二行中有1列,依此类推。
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0
P.S:我需要这个来解决多类分类问题。
答案 0 :(得分:2)
在设置参数之后,一个简单的for / next循环,每行的偏移量有一些数学就足够了。
Sub fill_cols()
Dim r As Long, cs As Long, rs As Long, lc As Long
rs = 4
cs = 2000
lc = 10000
With ActiveSheet
If lc > Columns.Count Then
For r = 1 To rs
.Cells(1, r).Resize(lc, 1) = 0
.Cells(1, r).Offset((r - 1) * cs, 0).Resize(cs, 1) = 1
Next r
Else
For r = 1 To rs
.Cells(r, 1).Resize(1, lc) = 0
.Cells(r, 1).Offset(0, (r - 1) * cs).Resize(1, cs) = 1
Next r
End If
End With
End Sub
如果请求的列超出工作表中的最大列数,我已添加了列到行的转置,反之亦然。