作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个宏,我想在其中为消息框创建自定义按钮,并且我想触发一个看起来像 Msgbox 的表单,其中包含“YTD”、“特定月份”等选项。这是我创建的以下表格:
表单内的代码如下:
Option Explicit
Public InputMsg As String
Public Sub CommandButton1_Click()
InputMsg = "YTD"
End Sub
Public Sub CommandButton2_Click()
InputMsg = "Specific Months"
我想将这些输入值添加到我在下面提到的模块代码中:
Sub Chts_Functions_UB()
'CallingForms
On Error Resume Next
Application.DisplayAlerts = False
Set Wb = ThisWorkbook
Set WsCharts = Wb.Sheets("Trend Charts")
Set UBMainChart = WsCharts.ChartObjects("UBMainChart")
Set UBMonthlyYTDSht = Wb.Worksheets("UM - Monthly & YTD")
Set FPFAChart = WsCharts.ChartObjects("FP_FA_YTD Chart")
Set FPBPChart = WsCharts.ChartObjects("FP_BP_YTD Chart")
Set FPRMDChart = WsCharts.ChartObjects("FP_RMD_YTD Chart")
Set FPMonthlyYTDSht = Wb.Worksheets("FP - Monthly & YTD")
YearValue = WsCharts.Range("A1").Value
'btnFunctionName = WsCharts.Shapes(Application.Caller).Name
WsCharts.Range("F2").Value = btnFunctionName
Dim Crows As Long, Ccols As Long
Dim NamedRng As Variant
'****Here I would like to get the Input from the Form (YTD or Specific Months Button)***
Crows = UBMonthlyYTDSht.Range("A" & Rows.Count).End(xlUp).Row
Ccols = UBMonthlyYTDSht.Cells(1, Columns.Count).End(xlToLeft).Column
On Error GoTo 0
感谢你的帮助!!
最佳答案
除非绝对必要,否则避免使用公共(public)变量。将相关值作为参数传递,如下所示。
您的表格代码
Private Sub CommandButton1_Click()
Chts_Functions_UB "YTD"
End Sub
Private Sub CommandButton2_Click()
Chts_Functions_UB "Specific Months"
End Sub
您的模块代码
Sub Chts_Functions_UB(ChartType As String)
'
' Chts_Functions_UB code here
'
End Sub
还要避免使用
On Error Resume Next
.明智地使用它并使用适当的错误处理。例如
Sub Chts_Functions_UB(ChartType As String)
On Error GoTo Whoa
Application.DisplayAlerts = False
'
' Chts_Functions_UB code here
'
LetsContinue:
Application.DisplayAlerts = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
备注 :
LetsContinue
和
Whoa
是我喜欢使用的名字。你可以给他们起你喜欢的名字。
关于excel - 如何从宏触发表单并将表单中的输入获取到 VBA 模块代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70061055/
我是一名优秀的程序员,十分优秀!