Question about using variables for worksheets

moradisndat

New Member
Joined
Feb 19, 2021
Messages
15
Office Version
  1. 2007
Platform
  1. Windows
I was hoping to get a confirmation if I am doing this correctly. (Using variable to refer to sheet)

After importing a large CSV file to sheet1, several actions are performed, mostly using "range." (Delete rows with blank cells, formatting, inserting columns for formulas, assigning named ranges, etc)

It looks like this:
---------------------
Dim ws as worksheet
Set ws = ActiveWorkbook.Sheets(Sheet1)

With ws
.Range("E1").EntireColumn.Insert
.Range("A1:G3").Columns.AutoFit
' and so on
End with
-----------------------

Also, could I use ws in place of using Sheets(Sheet1) to import the file?

With ws.QueryTables.Add(Connection:= _
"TEXT;" & path, Destination:=Range("$A$1"))

Kinda new to this so appreciate any help. Thanks in advance.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
If in the below Sheet1 is the sheet name as it appears on the sheet tab then Sheet1 should be in quotes
VBA Code:
Set ws = ActiveWorkbook.Sheets(Sheet1)
The rest looks fine.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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