【问题标题】:Using VBA CommandButton Value Variable From Userform to Module使用从用户窗体到模块的 VBA 命令按钮值变量
【发布时间】:2018-05-07 00:52:44
【问题描述】:

我知道,如果我们将一个变量命名为Public,那么如果他在 subs 之间传递了End Sub,则该变量可以保持其值。但是我的问题是我有一个从用户表单中的 commandnbutton 获取其值的变量,并且每次尝试从用户表单子中设置此变量时都会出现编译错误。我有一个月份的列表框,选择月份后,您按下命令按钮并启动宏。我知道如果 H 传递了Ens Sub,变量的值将在 subs 之间移动,我也需要在其他 subs 中使用这个变量值并且我需要它是动态的,有人知道什么可以帮助我吗?

这是用户表单子:

 Public MainWB  As Workbook
    Public VisualWB As Workbook
    Public VisualWS As Worksheet
    Public VacationWS As Worksheet
    Public HealingWS As Worksheet
    Public IllnessWS As Worksheet
    Public Lists As Worksheet
    Public MonthCol As Long
    Public MonthName As String
    Public MonthBefore As Long
    Public ColumnSpace As Long
    Public LR As Long
    Public LC As Long
    Public myExtension As String
    Public SecondPath As String
    Public Table As Range
    Public Names As Range

    Private Sub CommandButton1_Click()

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Application.Calculation = xlManual


    Call initialize

    'Here I have a lot of Code

    Unload UserForm1
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    Application.Calculation = xlAutomatic
    End Sub

这是模块子:

Option Explicit
Sub initialize()
'The variables will keep their value on all the subs

MonthName = ListOfMonths.Value
Set MainWB = ThisWorkbook
Set VacationWS = MainWB.Worksheets(1)
Set HealingWS = MainWB.Worksheets(2)
Set IllnessWS = MainWB.Worksheets(3)
Set Lists = MainWB.Worksheets("Lists")
Set VisualWS = MainWB.Worksheets("Visual")

With VisualWS
    .Range("L1:W1").Find(MonthName, , xlValues, xlWhole).Activate
End With

MonthCol = ActiveCell.Column
MonthBefore = MonthCol - 1
End Sub

【问题讨论】:

  • 卸载用户表单时,使用的变量全部被擦除,因此您无法获取值。尝试将变量放入模块中。
  • @newacc2240 但是变量已经在一个模块中,它仍然不是wotk。
  • 您的公共变量必须在标准模块中声明,而不是在用户表单后面的代码文件中。
  • 如果我的回答正确,请标记为已接受,以便其他人知道问题已解决。祝你有美好的一天,祝你编码好运!
  • @LucasRaphaelPianegonda 对不起,您的回答无效。

标签: vba excel userform


【解决方案1】:

我最初的答案是正确的。有 monthname=listofmonths.value 的那一行是有问题的,因为编译器不知道 listofmonths 是什么。然后必须使用 userformname.property 将其余部分作为用户表单的属性进行处理,因此请对其进行调整,然后请澄清 listofmonths 是什么以及我们从哪里获得。如果没有问题行,它现在应该可以编译了。

Option Explicit
Sub initialize()
'The variables will keep their value on all the subs

'UserForm1.MonthName = Listofmonths.Value ' this is a issue since listofmonths is not defined
Set UserForm1.MainWB = ThisWorkbook
Set UserForm1.VacationWS = UserForm1.MainWB.Worksheets(1)
Set UserForm1.HealingWS = UserForm1.MainWB.Worksheets(2)
Set UserForm1.IllnessWS = UserForm1.MainWB.Worksheets(3)
Set UserForm1.Lists = UserForm1.MainWB.Worksheets("Lists")
Set UserForm1.VisualWS = UserForm1.MainWB.Worksheets("Visual")

With UserForm1.VisualWS
    .Range("L1:W1").Find(UserForm1.MonthName, , xlValues, xlWhole).Activate
End With

UserForm1.MonthCol = ActiveCell.Column
UserForm1.MonthBefore = UserForm1.MonthCol - 1
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多