2017-11-23 8 views
1

変数名をPublicとすると、変数がsubsの間にEnd Subを渡した場合、変数はその値を保持できます。しかし、私のpromelmは、私はuserformのcommandnbuttonからその値を取得する変数を持っている、私はuserformのサブ変数からこの変数を設定しようとするたびにコンパイルエラーを取得します。私は数ヶ月のリストボックスを持っており、月を選択するとコマンドボタンを押すとマクロが始まります。私は、変数Hの値がEns Subを渡したときにサブシステム間を移動することを知っています。他のサブシステムでもこの変数値を使用する必要があり、動的である必要があります。UserFormからModuleへのVBA CommandButtonの値変数の使用

これは、ユーザーフォームのサブです:

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 
+0

ユーザーフォームがアンロードされると、使用される変数はすべて消去され、値を取得できなくなります。変数をモジュールに入れてみてください。 – newacc2240

+0

@ newacc2240しかし変数はすでにモジュールに入っていて、それでもまだ動いていません。 –

+1

あなたのパブリック変数は、標準モジュールで宣言しなければなりません。ユーザーフォームの背後にあるコードファイルでは宣言しないでください。 –

答えて

1

私の最初の答えは正しい道にありました。 monthname = listofmonths.valueの行があります。これは、コンパイラがどのlistofmonthsか分からないため問題があります。その後、残りはuserformname.propertyを使用してuserformのプロパティとして扱われなければなりませんので、これを調整してから、どの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