0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ExcelVBA 自动写入值的方法

Posted at

这个函数能够将Date表里面存储的数据,用随机抽取的方式,转写到新的表里面
如果稍加修改,将ThisWorkbook.Worksheets("DATE").Cells(ret, "H")部分换掉改成ret,
则可以变成获取iMax到iMin范围内的随机数

Sub AUTO_INPUT()
    Dim iMax As Double
    Dim iMin As Double
    Dim arrcols As Variant
    iMax = 5
    iMin = 2
    arrcols = Array("B", "C")
    For i = 4 To 10005
        'ret = Format(Fix((iMax - iMin + 1) * Rnd + iMin), "0")
        For Each j In arrcols
            ret = Format(Fix((iMax - iMin + 1) * Rnd + iMin), "0")
            Cells(i, j) = ThisWorkbook.Worksheets("DATE").Cells(ret, "H")
        Next j
    Next i
End Sub

iMax 是取得的最大值(包含)
iMin 是取得的最小值(包含)
arrcols 是写入的列名称,这里写的内容西会在后面的 For Each里面被转换出来
这里用数组的好处是,可以随意指定列,并且会通过一次执行全部执行到
上面例子里,B,C列将被追加数据
for i 为需要写入数据的行号
Format 用于控制最后的输出格式,这里是使用的是“0”,也可以是“0.00”,“00.00”或者任意的位数的格式
ThisWorkbook.Worksheets("DATE") 里面的DATE是放存储需要的数据表的表名称,可以随意修改
Cells(ret, "H")里面的ret,是前面获得的随机数,H是Date表里存放随机获取数据的所在列

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?