0

I'm trying to obtain the value for Teller from an userform in my module. Here's my userform code:

Private Sub UserForm_Initialize()

Dim LastRow As Long
Dim i As Long
Dim Teller As Long
Dim chkBox As MSForms.CheckBox

Teller = 1
LastRow = Worksheets("Sheet").Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To LastRow
    If Worksheets("Sheet").Cells(i, 1).Value = Worksheets("Sheet").Range("S1").Value Then
        Set chkBox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & Teller)
        chkBox.Caption = Worksheets("Sheet").Cells(i, 9).Value
        chkBox.Left = 5
        chkBox.Top = 25 + ((Teller - 1) * 20)
        Teller = Teller + 1
    End If
Next i    
End Sub

And my module code:

Dim p As Long
Dim x As String
With UserForm
    .Show
    For p = 1 To .Teller
        x= .Controls("CheckBox_" & p).Caption
        MsgBox (x)
    Next p
    End
End With

UserForm.Teller won't give me the value of Teller. How do I get this?

1
  • 1
    teller is declared as private to the userform, so modules will not be able to read it. You can declare teller as public in a module, which will give you the ability to read it from other modules. Commented Jul 21, 2017 at 11:31

1 Answer 1

2

Teller isn't an object or method of the userform, it's a variable you have defined in the userform_initialize module.

What you could do is declare the variable as a public variable in your module and then call it in your userform. You do this by declaring the variable above your subroutine in the module as public as below.

Public Teller As Long

Then you would just use

For p = 1 to teller

in your module

You would need to reset the variable to 0 manually at the end of the code/ when the userform is closed if you want it to reset on each run

Thanks

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

5 Comments

Thanks. I tried it but it seems like the value of Teller is empty in the module, since it gives me no output.
When I put a MsgBox(Teller) at the end of the With Userform it shows a blanco messagebox. Not even a 0 or 1, just nothing.
Try and declare it at the top of the module rather than in the userform, it should work then. My mistake! But make sure to remove it from the Userform Module first
No problem, happy to help Stan!
@user8234201 you should edit and fix your answer so it is accurate. (Maybe show it as public in Stan's module code)

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.