Copying cell data into a userform label using a module

sadsfan

Board Regular
Joined
Apr 30, 2003
Messages
217
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a Spreadsheet for writing school reports, there are a number of subjects e.g. Maths and English. I have a userform that lists pupils names, as I need the same names for each subject I have set up a module that will copy the names from a sheet to the labels, and my code is:

VBA Code:
Sub Names()
LblName1.Caption = Sheets("Data").Range("B2").Value
LblName2.Caption = Sheets("Data").Range("B3").Value
End Sub

I have tried to call it in my Userform with the following code:
VBA Code:
Private Sub Userform_Initialize()
Call Module1.Names
End Sub

But when I run it, it says 'Run-time error 424 Object required. Where am I going wrong?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi,
if you have Controls for your userform in a code that resides outside your forms code page, you will need to qualify them to your userform which you can do either by specifying the UserForm in your code

VBA Code:
UserForm1.LblName1.Caption = Sheets("Data").Range("B2").Value
    UserForm1.LblName2.Caption = Sheets("Data").Range("B3").Value

Or by passing a copy of the UserForm object as an argument to your procedure

VBA Code:
Sub Student_Names(ByVal Form As Object)
    Form.LblName1.Caption = Sheets("Data").Range("B2").Value
    Form.LblName2.Caption = Sheets("Data").Range("B3").Value
End Sub

note that I modified your procedure Names to Student_Names - as it may (or may not) conflict with VBA Names object.

you would then call as follows

VBA Code:
Private Sub Userform_Initialize()
    Student_Names Me
End Sub

Hope Helpful

Dave
 
Upvote 0
Thanks Dave that worked a treat. If I had some labels on a multipage, e.g. one called English would I put: Form.English.LblName1.Caption ...
 
Upvote 0
Thanks Dave that worked a treat. If I had some labels on a multipage, e.g. one called English would I put: Form.English.LblName1.Caption ...

Form.English.Caption should work

Dave
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,916
Members
448,533
Latest member
thietbibeboiwasaco

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