Question regarding variables when working with objects

supermom28115

New Member
Joined
May 9, 2022
Messages
43
Office Version
  1. 365
Platform
  1. Windows
I found this thread and wanted to ask about the use of a variable to hold the sheet name.
Is this the way the code should be written when using variables for objects?
I was under the impression that I should be using this code:
VBA Code:
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Sheet 1")
I ask because this is not working in my code and I can not figure out why it will not work. I have tried multiple variations of this code; ie long hand the workbook name within the set code, using worksheet, sheet, sheets, using the index for the sheet. I have even tried changing the variable name in case the ws was inappropriate. I continually get the same ws1= Nothing, object variable or with variable not set error.
So how am I supposed to declare and set a variable correctly for my workbook and worksheets to use in my code?



 
I got that part, my point was that there was no reason to assume Sheet 1 wasn't the name of my sheet.

I didn't assume anything. These things happen. Names like Sheet1 are the way Excel names sheets by default, so putting a space in there may have been an accident. I was just trying to help and it was the example you gave. No need to get huffy or anything.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,215,086
Messages
6,123,038
Members
449,092
Latest member
ikke

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