0

Firstly, this is my first ever post on stack overflow, so I hope I'm following the correct procedure. I have browsed through dozens of posts on this and other websites but I can't seem to extrapolate solutions for similar cases to mine. I have also tried using debug lines, but I can't pinpoint the problem, probably due to the fact that I'm new to VBA. Here's what I have in short, I hope you can help:

A commandbutton on Sheet1 that raises a Yes/No/Cancel msgbox, I want a mechanism to remember this choice in the UserForms and Modules that follow, so I declared boolNieuweOpdrachtgever as a Public variable, however, in the subsequent form, the Debug line indicates that it doesn't remember its value at all. Here's the code:

Public boolNieuweOpdrachtgever As Boolean
Public Sub nieuw_project_Click()

Dim nieuweOpdrachtgever As Variant

    nieuweOpdrachtgever = MsgBox("Text", vbYesNoCancel)
    Select Case nieuweOpdrachtgever
        Case vbYes
            boolNieuweOpdrachtgever = True
            Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
            nieuweOpdrachtgeverForm.Show
        Case vbNo
            boolNieuweOpdrachtgever = False
            Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
            nieuweOpdrachtForm.Show
        Case Else
            Exit Sub
    End Select

End Sub

For instance in the case of vbYes, it goes through a working form after which it goes into a second one that has an IF statement based on boolNieuweOpdrachtgever. However, by then it has already lost its value. Can you tell me what I am doing wrong?

2
  • Did it get it's value correctly from the Select Case? Commented Jan 3, 2015 at 12:39
  • yeah the debug would suggets so. I have found a workaround to get it working though, I am however still interested in how to take variable values from a (Sheet) module into a UserForm. Commented Jan 3, 2015 at 16:28

3 Answers 3

1

I believe the problem is that the code that is associated with Worksheets is not the same type as a module. Worksheet Code doesn't have the ability to set public or global variables.

I've tested different scenarios and the solution would be in taking all your code inside the Worksheet code, and putting it into a separate Module, then Call the Module from the Worksheet events that trigger your code.

At that point, the module declares the public variable, and it IS accessible as a public variable.

Sheet Code:

Private Sub SomeValue_Change()
    'What would have been your code, now moved to another module
    Call NewModule    
End Sub

Module Code:

Option Explicit
Public tempValue As String

Sub NewModule()
    'Code that was previously in the Worksheet Code
    tempValue = InputBox("Please input the public variable value.","Public Variable")

    'You can test it by calling it from here to simplify the process.
    Call TestValues
End Sub

Other Code: Put this in a different module. Note there is no declaration of the variable at all. Only in the module that contains "NewModule".

Sub TestValues()

    MsgBox("The value from the public variable is :" & tempValue & ".")

End Sub

By declaring the variable from a Module instead of Worksheet-Code, the variable is captured and accessible globally. Doing the same thing from the worksheet code fails.

Sign up to request clarification or add additional context in comments.

1 Comment

Sounds logical, thank you very much. I will try your and Edward Bagby's code later on and post the results.
0

I tried your code and I think I know your problem. when you acquire the variable in your form , say the form nieuweOpdrachtgeverForm, then you need to call the module and the variable, not just the variable. For example: Me.Label1.Caption = Module1.boolNieuweOpdrachtgever

Here is the code I used for the Sub (in Module1) that the button calls:

Public boolNieuweOpdrachtgever As Boolean

Sub nieuw_project_Click()

Dim nieuweOpdrachtgever As Variant

    nieuweOpdrachtgever = MsgBox("Text", vbYesNoCancel)
    Select Case nieuweOpdrachtgever
        Case vbYes
            boolNieuweOpdrachtgever = True
            Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
            nieuweOpdrachtgeverForm.Show
        Case vbNo
            boolNieuweOpdrachtgever = False
            Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
            nieuweOpdrachtForm.Show
        Case Else
            Exit Sub
    End Select

End Sub

Here is the code in the forms, I used to test passing the variable. Note that I added a label called Label1, and placed the resulting value in the label:

Private Sub UserForm_Initialize()

  Me.Label1.Caption = Module1.boolNieuweOpdrachtgever

End Sub

Here are the results on forms after selecting Yes and No:

Form returning false

Form returning true

1 Comment

Thank you for your answer, it indeed sounds reasonable. One follow-up question: how would you refer to the code in Sheet1? Would you use Sheet1 or the name of the sheet? (In your example what should I replace Module1 with?)
0

Sjors - I was wondering if my answer was helpful (I didn't see the check checked), but I'm glad it was. To Answer your last comment, I've added code. THe first bit is the code in Module1, the second is in Sheet1, and the other are from the Forms (per my answer above). I'd recommend you cut and paste the code into your project and step through it (F8) to see how it is working. This should give you a good basic understanding how to make calls between objects.

MODULE1:

Public boolNieuweOpdrachtgever As Boolean

Sub nieuw_project_Click()

Dim nieuweOpdrachtgever As Variant
Dim strFromSheet1 As String

nieuweOpdrachtgever = MsgBox("Text", vbYesNoCancel)
Select Case nieuweOpdrachtgever
    Case vbYes
        boolNieuweOpdrachtgever = True
        Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
        nieuweOpdrachtgeverForm.Show
    Case vbNo
        boolNieuweOpdrachtgever = False
        Debug.Print "In nieuw_project_Click() boolNieuweOpdrachtgever = " & boolNieuweOpdrachtgever
        nieuweOpdrachtForm.Show
    Case Else
        Exit Sub
End Select

Call Sheet1.setString

strFromSheet1 = Sheet1.strPublic

Call Sheet1.Test(strFromSheet1)


End Sub

SHEET1:

Public strPublic As String


Public Sub Test(ByVal strTest As String)

    MsgBox (strTest)

End Sub

Public Sub setString()

    strPublic = "Hello"

End Sub

EXAMPLE OF ONE OF THE FORMS:

Private Sub UserForm_Initialize()

   Me.Label1.Caption = Module1.boolNieuweOpdrachtgever

End Sub

1 Comment

Hi Edward, thank you for the elaborate second response. Sadly, I have been very busy over the weekend and regular studies have started again. I will try your code as soon as possible and post the results here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.