|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
各位老师大神你们好:
如何实现user form1用户界面下按钮代码优化,各按钮共用一段代码,每个CommandButton_Click()都指向这段代码谢谢!
以下为原始代码其中2个按钮,表里还有好多不一一列出见附件,按钮如果超出50个或100个的话每个都是写同样的代码,就是每个按钮 If CommandButton10.Caption = arr(x, 2)红字体要对应所指定的按钮名称太烦,按钮多了容易出错谢谢!
Private Sub CommandButton10_Click()
Dim s As String, x As Long, arr
arr = Sheet1.Range("a1").CurrentRegion
For x = 2 To UBound(arr)
If CommandButton10.Caption = arr(x, 2) Then
s = arr(x, 3)
End If
Next
ActiveCell.Value = ActiveCell.Value & s
End Sub
Private Sub CommandButton11_Click()
Dim s As String, x As Long, arr
arr = Sheet1.Range("a1").CurrentRegion
For x = 2 To UBound(arr)
If CommandButton11.Caption = arr(x, 2) Then
s = arr(x, 3)
End If
Next
ActiveCell.Value = ActiveCell.Value & s
End Sub
|
|