我在VBA中使用以下代码使用公式将列填充到所选的某个特定单元格(可以通过更改单元格值来更改):
Worksheets("Deelnemersbestand").Range("B2:B"&aantalDeelnemers).Formula="=RAND()"
我也希望清除B列的其余部分。
但是它不起作用。有什么建议么?我是新来的。
答案 0 :(得分:0)
Sub test()
Dim aantalDeelnemers, aantalDeelnemersLegen As Integer
aantalDeelnemers = Worksheets("Interface").Range("aantalDeelnemers").Value
aantalDeelnemersLegen = aantalDeelnemers + 1
Worksheets("Deelnemersbestand").Range("B2:B" & aantalDeelnemers).Formula = "=RAND()"
Worksheets("Deelnemersbestand").Range("B" & aantalDeelnemersLegen &
":B1000").ClearContents
End Sub
答案 1 :(得分:0)
要启用RANDBETWEEN
,在Excel中,您必须转到“工具”>“加载项”并选中“分析工具库”。
Sub Bestand()
Const cWs1 As String = "Interface" ' First Worksheet
Const cWs2 As String = "Deelnemersbestand" ' Second Worksheet
Const cRngName As String = "aantalDeelnemers" ' Cell Range in First Worksheet
Const cColumn As Variant = "B" ' Column Letter/Number
Const cFirstRow As Integer = 2 ' First Row of Data
Const cFormula As String = "=RAND()" '"=RANDBETWEEN(21,50)"
Dim i As Integer
i = Worksheets(cWs1).Range(cRngName).Value
With Worksheets(cWs2)
.Range(.Cells(cFirstRow, cColumn), .Cells(i, cColumn)).Formula = cFormula
.Range(.Cells(i + 1, cColumn), .Cells(.Rows.Count, cColumn)).ClearContents
End With
End Sub