有没有办法更有效地编写这段VBA代码?
使用for循环还是这样?
并使其更加通用,而不是固定的细胞?
Private Sub CommandButton1_Click()
Range("A31").Formula = "=index(Optional_Processes,1)"
Range("A32").Formula = "=index(Optional_Processes,2)"
Range("A33").Formula = "=index(Optional_Processes,3)"
Range("A34").Formula = "=index(Optional_Processes,4)"
Range("A35").Formula = "=index(Optional_Processes,5)"
Range("A36").Formula = "=index(Optional_Processes,6)"
Range("A37").Formula = "=index(Optional_Processes,7)"
Range("A38").Formula = "=index(Optional_Processes,8)"
Range("A39").Formula = "=index(Optional_Processes,9)"
Range("A40").Formula = "=index(Optional_Processes,10)"
Range("A41").Formula = "=index(Optional_Processes,11)"
Range("A42").Formula = "=index(Optional_Processes,12)"
谢谢!
答案 0 :(得分:4)
在一次点击中执行此操作的另一种方法是:
Private Sub CommandButton1_Click()
Range("A31:A42").Formula = "=index(optional_processes,row()-30)"
End Sub
这不会将1,2,3等作为最后一个参数,但当放在第31行时,它将返回1(row()-30
),依此类推。
答案 1 :(得分:2)
使用For
循环,如下所示:
Dim i As Long
For i = 2 To 12
Range("A" & 30 + i).Formula = "=index(Optional_Processes," & i & ")"
Next i
第二个选项:
Dim CellStart As Range
Set CellStart = Range("A30") ' set the Start Cell anchor
For i = 2 To 12
CellStart.Offset(i).Formula = "=index(Optional_Processes," & i & ")"
Next i