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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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