我正在尝试在工作表上简化CommandButton
的宏。
我有很多按钮,应该根据细胞数据改变。
如果我逐行写
CommandButton25.Caption = Range("C29").Value
CommandButton26.Caption = Range("C30").Value
或
ActiveSheet.CommandButton27.Caption = Range("c31").Value
有效。但是,如果我尝试使用像
这样的东西For i = 28 To 40
ActiveSheet("CommandButton" & i).Caption = Range("c31").Value
Next i
问题很可能是(CommandButton
& i
)。
我在用户表单上有Label
的类似宏。
For i = 1 To 40
Desk("Label" & i).Caption = ActiveCell(4 + i, 3)
Next i
完美无缺。
如果只有40行,我可能会留下它,但用户表格和其他表格上有更多按钮。
答案 0 :(得分:0)
我认为你应该循环形状,这就是你的问题,我的意思是:
rw1 <- c("File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3", "File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3", "File1", "File1", "File1", "File2", "File2", "File2", "File3", "File3", "File3")
rw2 <- c("0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.01", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.02", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03", "0.03")
rw3 <- c("Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final", "Time", "Size", "Final")
rw4 <- c(123, 456, 789, 312, 645, 978, 741, 852, 963, 369, 258, 147, 753, 498, 951, 753, 915, 438, 978, 741, 852, 963, 369, 258, 147, 753, 498)
rw5 <- c("01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12", "01/01/12")
rw6 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3)
rw7 <- c("Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Iteration", "Release", "Release", "Release", "Release", "Release", "Release", "Release", "Release", "Release")
rw8 <- c("None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "None", "Cannot Connect to Database", "None", "None", "None", "None", "None", "None", "None", "None")
Testdf = data.frame(rw1, rw2, rw3, rw4, rw5, rw6, rw7, rw8)
colnames(Testdf) <- c("FileName", "Version", "Category", "Value", "Date", "Number", "Build", "Error")
如果这不起作用,那么我认为您应该将代码粘贴到空白的Excel工作表上,然后将单元格拆分为:
然后你可以将整个事情拉下来,将D列的内容复制到记事本中并将其粘贴为代码,问题就解决了。不是很优雅,但它会工作:) 祝你好运!
答案 1 :(得分:0)
您可以遍历UserForms中指定的“控件”对象,如下所示:
For i 28 To 40
myUserForm.Controls("CommandButton" & i).Caption = Range("C31").Value
Next i
您可以使用以下内容遍历工作表上的命令按钮:
Sub cb_loop()
Dim i As Byte
For i = 28 To 48
Sheets("sheet1").Shapes("CommandButton" & i).DrawingObject.Object.Caption = Range("C31").Value
Next i
End Sub