Error passing variables to a userform

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,590
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have variables fix_type and fix_name declared publically as string.

Code:
Public fix_type As String
Public fix_name As String

When I run my code and get to this point of opening my userform, both variables have values.

Code:
...
    Case 1
        fix_type = "NDB"
        valid_freq fixpropcancel
        Stop
        Coordinates.Show
    Case 2
...

I get an error (object required) in my Userform Initialization mode (lines highlighted red) when I try to assign these variable's values to their respective labels on the userform. Both "fx_type" and fx_name" are valid lable names in my userform. Looking to have my error revealed.

Rich (BB code):
Private Sub UserForm_Initialize()
    Caption = "FIX COORDINATES"
    fx_type.Caption = fix_type.Value
    fx_name.Caption = fix_name.Value
    btn_accept.Enabled = False
    lat_dd.Value = "00.000000"
    long_dd.Value = "00.000000"
    lat_dm_d.Value = "00"
    lat_dm_min.Value = "00"
    long_dm_d.Value = "00"
    lon_dm_min.Value = "00"
    lat_dms_d.Value = "00"
    lat_dms_min.Value = "00"
    lat_dms_sec.Value = "00.000"
    long_dms_d.Value = "00"
    lon_dms_min.Value = "00"
    long_dms_sec.Value = "00.000"
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
But variables are not controls therefore, they should not carry .Value:

fx_type.Caption = fix_type
fx_name.Caption = fix_name
 
Upvote 0
Ah right. Sometimes the obvious eludes me. Thank you Dante for the reminder.
But something's still not right. The values of those variables isn't being passed into the label control.
 
Upvote 0
Where do you have the variables declared?

If you put them in the module. Works.


In Module:
VBA Code:
Option Explicit

Public fix_type As String
Public fix_name As String

Sub llamada()
  Dim n
  n = 1
  Select Case n
    Case 1
        fix_type = "NDB"
        Coordinates.Show
    Case 2
    
  End Select
End Sub

In userform:
VBA Code:
Private Sub UserForm_Initialize()

    fx_type.Caption = fix_type
    fx_name.Caption = fix_name

End Sub


And that works.
 
Upvote 0
Solution
Does it matter in which module of a project you put Public declarations?
The module doesn't matter. I tested it and it works if the public declarations are in another module.

😇
 
Upvote 0
I actually found the source of my issue. I had forgotten to remove the original Dim declarations of those variables in the module. I think they over rode the public declaration.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,258
Members
452,901
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top