我在Internet上发现了很多资源,这些资源几乎可以完成我想做的事情,但不完全是我的工作。我有一个命名范围“ daylist”。对于dayList中的每一天,我想在用户表单上创建一个按钮来运行当天的宏。我能够动态添加按钮,但不知道如何将daycell.text从命名范围传递到按钮,事件处理程序和宏:S这里,我必须创建用户表单的代码:Sub addLabel()ReadingsLauncher.Show vbModelessDim theLabel As ObjectDim labelCounter As LongDim daycell As RangeDim btn As CommandButtonDim btnCaption As StringFor Each daycell In Range("daylist") btnCaption = daycell.Text Set theLabel = ReadingsLauncher.Controls.Add("Forms.Label.1", btnCaption, True) With theLabel .Caption = btnCaption .Left = 10 .Width = 50 .Top = 20 * labelCounter End With Set btn = ReadingsLauncher.Controls.Add("Forms.CommandButton.1", "runButton", True) With btn .Caption = "Run Macro for " & btnCaption .Left = 80 .Width = 80 .Top = 20 * labelCounter ' .OnAction = "btnPressed" End With labelCounter = labelCounter + 1Next daycellEnd Sub为了解决上述问题,我目前提示用户输入他们想运行的日期(例如Day1)并将其传递给宏,然后它可以工作:Sub B45runJoinTransactionAndFMMS()loadDayNumber = InputBox("Please type the day you would like to load:", Title:="Enter Day", Default:="Day1")Call JoinTransactionAndFMMS(loadDayNumber)End SubSub JoinTransactionAndFMMS(loadDayNumber As String)xDayNumber = loadDayNumberSheets(xDayNumber).Activate-Do stuffEnd Sub因此,对于我的每个runButton,它都需要显示daycell.text,并运行一个使用相同文本作为参数的宏,以选择要执行其工作的工作表。任何帮助都是极好的。我见过响应,它们动态地编写了VBA代码来处理宏,但是我相信一定可以通过传递参数来更好地完成它,只是不确定如何做到。提前谢谢了!
3 回答
哆啦的时光机
TA贡献1779条经验 获得超6个赞
捕获单击工作表的示例。将其放在工作表模块中:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' e.g., range(A1:E1) is clicked
If Not Application.Intersect(Target, Range("A1:E1")) Is Nothing Then
MsgBox "You clicked " & Target.Address
End If
End Sub
回首忆惘然
TA贡献1847条经验 获得超11个赞
有用!测试我使用的代码:(Sub intersectCallingMacro() Range("D8").Select 'used for testing only Set target = ActiveCell If Not Application.Intersect(target, Range("A1:M100")) Is Nothing Then Call testIntersect(ActiveCell.Value) End If End Sub Sub testIntersect(dayString As String) Dim xDayString As String xDayString = dayString Sheets(xDayString).Activate End Sub
为代码格式化道歉)将单元值作为参数传递给宏。惊人!
- 3 回答
- 0 关注
- 981 浏览
相关问题推荐
添加回答
举报
0/150
提交
取消