Public variable in more macros

Rataplan

New Member
Joined
Jan 20, 2025
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Good morning.
I would like to point out that I am new to programming with VBA and in general I am not a professional programmer.
Let's get to the question: I am creating an excel worksheet to record and monitor maintenance interventions. I would like to create a macro, with different Forms, to populate the excel Worksheet and analyze the data. My need is to pass the value of the "Scelta" variable to the "Userform1" macro to use it as the 'rowsource' property of the "cbo_Ricerca" combobox so that I am listed with the available search ranges, which I have created on the sheet with names, as you can see in the files that I am attaching.
Thank you for your patience and availability.
 

Attachments

  • Userform1_Code_1.JPG
    Userform1_Code_1.JPG
    115.3 KB · Views: 8
  • Userform1_Code_2.JPG
    Userform1_Code_2.JPG
    122 KB · Views: 8
  • Userform1_Code_3.JPG
    Userform1_Code_3.JPG
    127.6 KB · Views: 5
  • Userform1_Code_4.JPG
    Userform1_Code_4.JPG
    104.7 KB · Views: 6
  • Userform1_Form.JPG
    Userform1_Form.JPG
    185.7 KB · Views: 7
  • Userform2_Code.JPG
    Userform2_Code.JPG
    38.6 KB · Views: 7
  • Userform2_Form.JPG
    Userform2_Form.JPG
    112.9 KB · Views: 7
  • Userform3_Code.JPG
    Userform3_Code.JPG
    39.8 KB · Views: 5
  • Userform3_Form.JPG
    Userform3_Form.JPG
    87.1 KB · Views: 6
  • Userform4_Code.JPG
    Userform4_Code.JPG
    71.9 KB · Views: 5
  • Userform4_Form.JPG
    Userform4_Form.JPG
    100.1 KB · Views: 4
  • Userform5_Code.JPG
    Userform5_Code.JPG
    52.2 KB · Views: 3
  • Userform5_Form.JPG
    Userform5_Form.JPG
    63.9 KB · Views: 8

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Here is the minimum code needed in the userform so you can pass it the scelta range and have the combobox get filled with its contents:
VBA Code:
Option Explicit

Public SceltaRng As Range  'This is the public variable that holds the scelta range

'We are not using userform_Initialize, as that runs too soon, rather we define an initialize routine we call explicitly
Public Sub Initialize()
    'Set the list to the content of scelta
    If Not SceltaRng Is Nothing Then
        cbo_Ricerca.List = SceltaRng.Value
    End If
End Sub
The code that loads the form needs to change to:
VBA Code:
Sub ShowaForm()
    With UserForm1
        Set .SceltaRng = ThisWorkbook.Names("Scelta").RefersToRange
        .Initialize
        .Show
    End With
End Sub
 
Upvote 0
Here is the minimum code needed in the userform so you can pass it the scelta range and have the combobox get filled with its contents:
VBA Code:
Option Explicit

Public SceltaRng As Range  'This is the public variable that holds the scelta range

'We are not using userform_Initialize, as that runs too soon, rather we define an initialize routine we call explicitly
Public Sub Initialize()
    'Set the list to the content of scelta
    If Not SceltaRng Is Nothing Then
        cbo_Ricerca.List = SceltaRng.Value
    End If
End Sub
The code that loads the form needs to change to:
VBA Code:
Sub ShowaForm()
    With UserForm1
        Set .SceltaRng = ThisWorkbook.Names("Scelta").RefersToRange
        .Initialize
        .Show
    End With
End Sub
Thanks, @jkpieterse for your prompt reply.
as I explained to you I am quite newbie to excel macros.
I would ask you if you can tell me where to insert the code you suggested in the code files I published.
I tried to insert it, but maybe I am in the wrong file where to copy it.
Guido
 
Upvote 0
Well, the first code block goes into each userform module. Make sure you check if the form as a sub called Userform_Initialize. Move that code to my Initialize sub as shown above. For the second block you must look in your own code for code like "userform1.Show" and replace that with the code inside the sub ShowaForm I posted above.
It would help if you post the actual code rather than screen-shots, using the little code buttons above the message editor.
 
Upvote 0

Attachments

  • Screenshot 2025-01-21 124904.png
    Screenshot 2025-01-21 124904.png
    97 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,734
Members
453,369
Latest member
juliewar

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