Set Worksheet name from Variable?

kpm30519

New Member
Joined
Nov 9, 2018
Messages
22
Hi All--

I have a userform that pulls data from 4 different worksheets. I set specific labels to these data values.
Each of the 4 worksheets is set up exactly the same, so Range("C:C") on one is the same on another, so I want to use a single module to run the code 4 times. Sounds simple...

Here's the module:
Code:
Sub LastDateEntered()
Label19.Caption = "Last Date Entered"
    ReturnLastDate = Ws.Range("C:C"). _
       End(xlDown).Offset(0, 0).value
           LastDraw1 = Ws.Range("D:D"). _
       End(xlDown).Offset(0, 0).value
           LastDraw2 = Ws.Range("E:E"). _
       End(xlDown).Offset(0, 0).value
           LastDraw3 = Ws.Range("F:F"). _
       End(xlDown).Offset(0, 0).value
           LastDraw4 = Ws.Range("G:G"). _
       End(xlDown).Offset(0, 0).value
           LastDraw5 = Ws.Range("H:H"). _
       End(xlDown).Offset(0, 0).value
    
    
Label1.Caption = ReturnLastDate
Label20.Caption = Last1
Label21.Caption = Last2
Label22.Caption = Last3
Label23.Caption = Last4
Label24.Caption = Last5
When it runs, I get Run Time error 424, Object Required, with the line ReturnLastDate highlighted.

When I run this directly from the userform it works great. It pulls the last values from the respective columns and displays them in the Labels.

On the userform I have this:

Code:
Dim Ws As Worksheet
Set Ws = Worksheets("WSName5")

'A whole bunch of stuff happens here, not related.

Label19.Caption = "Last Date Entered"


    ReturnLastDate = Ws.Range("C:C"). _
       End(xlDown).Offset(0, 0).value
           LastDraw1 = Ws.Range("D:D"). _
       End(xlDown).Offset(0, 0).value
           LastDraw2 = Ws.Range("E:E"). _
       End(xlDown).Offset(0, 0).value
           LastDraw3 = Ws.Range("F:F"). _
       End(xlDown).Offset(0, 0).value
           LastDraw4 = Ws.Range("G:G"). _
       End(xlDown).Offset(0, 0).value
           LastDraw5 = Ws.Range("H:H"). _
       End(xlDown).Offset(0, 0).value
    
    
Label1.Caption = ReturnLastDate
Label20.Caption = Last1
Label21.Caption = Last2
Label22.Caption = Last3
Label23.Caption = Last4
Label24.Caption = Last5

I declare the worksheet name on the userform, then call the module. And, the module does not work.

I am stumped. Also, if there is a better way of doing this, I'd appreciate the help.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Add this to your sub...
Code:
Dim Ws As Worksheet
Set Ws = Worksheets("WSName5")
HTH. Dave

I have four sheets. Each sheet has a name. I need to run the module for each of the four sheets, and the sheet is selected by the user. If I name the sheet inside the module, It's no better than typing it out four times, I need four different sheet names.
 
Upvote 0
If you change the module title to
Code:
Sub LastDateEntered(Ws As Worksheet)
and call it like
Code:
Call LastDateEntered(Ws)
The worksheet variable will be passed to the module
 
Upvote 0
Thank You Fluff!
I hate being a newbie at VBA...

Next question, I'd really like to be able to set the variable Ws by a combobox, but
Code:
Set Ws = WorkSheets(ComboBox.value)
doesn't seem to work. Any ideas?
 
Upvote 0
In that case you don't have a sheet with the same name as the combo value.
Check spelling & also check for leading/trailing spaces.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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