Public sub in a userform? Why is it not possible, or is it?

finaljustice

Board Regular
Joined
Oct 6, 2010
Messages
175
Hi there,

I am having a simple issue, I am not being able to call a sub that is in another userform.
On userform1 I have this sub I use to load information to the userform.
(It was before declared as Private, but I thought if I declared it as Public It would solve the problem)

Code:
Public Sub loadresults()  'simply reload results and is located is in the userform1.
UserForm1.TextBox1 = Format(ThisWorkbook.Sheets("LD").Range("W28"), "Standard")
UserForm1.TextBox2 = Format(ThisWorkbook.Sheets("LD").Range("P412"), "Standard") 
UserForm1.TextBox3 = Format(ThisWorkbook.Sheets("LD").Range("J15"), "Standard") 
UserForm1.TextBox4 = Format(ThisWorkbook.Sheets("LD").Range("J18"), "Standard") 
UserForm1.TextBox5 = Format(ThisWorkbook.Sheets("LD").Range("I22"), "Standard") 
UserForm1.TextBox6 = Format(ThisWorkbook.Sheets("LD").Range("J22"), "Standard") 
End Sub

Then on another userform2, I have a command button which called 2 subs like so:

Code:
Private Sub CommandButton1_Click() 'this is in userform2    EditMP 'this code is in userform2 and calls fine
    loadresults 'this code is in userform1 and gives me an error.
End Sub

Só I remembered that I used this mechanism when the code was on a module, therefore I removed the "loadresults" from userform1 and pasted it in module 1 and declared it public. Like so:

Code:
Public Sub loadresults()  'simply reload results, saved on a module not a userform.
UserForm1.TextBox1 = Format(ThisWorkbook.Sheets("LD").Range("W28"), "Standard") 'Custo para Lingotamento
UserForm1.TextBox2 = Format(ThisWorkbook.Sheets("LD").Range("P412"), "Standard") 'Balanço térmico
UserForm1.TextBox3 = Format(ThisWorkbook.Sheets("LD").Range("J15"), "Standard") '% ressopro P
UserForm1.TextBox4 = Format(ThisWorkbook.Sheets("LD").Range("J18"), "Standard") 'indice de projeçao
UserForm1.TextBox5 = Format(ThisWorkbook.Sheets("LD").Range("I22"), "Standard") 'rendimento Fe do Conversor Prelimiar
UserForm1.TextBox6 = Format(ThisWorkbook.Sheets("LD").Range("J22"), "Standard") 'rendimento Fe do Conversor Real
End Sub

It now works...
I would like to know why is it that I can't have a Public sub inside the userform folder?

Thanks for your time.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Not sure why you have the code in the userform module but have you tried calling it like this.
Code:
UserForm1.loadresults
 
Upvote 0
Not sure why you have the code in the userform module but have you tried calling it like this.
Code:
UserForm1.loadresults


Cool that works! Basically you are informing where the code is correct?

Do you think it is good practice to put all the code inside a modules instead of userform modules?

Thanks for your attention!
 
Upvote 0
It really depends what you are trying to do.

In this case I might put the code in UserForm2's module, either within the button's click event or as a separate sub.
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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