alternative to Select/activate - to delete all data in a worksheet

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - i am trying to improve my VBA and speed it up by removing all instances of select and activate. one issue i am having is converting the below:

Workbooks("t.xlsx").Activate
sheets("main").Activate
Range("A2:X2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete
ActiveWorkbook.Close SaveChanges:=True

i thought this would work but it doesn't seem to be:

Workbooks("t.xlsx").sheets("main").Range("A2:X2").End(xlDown)).Delete
ActiveWorkbook.Close SaveChanges:=True
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How about
VBA Code:
With Workbooks("t.xlsx").Sheets("main")
   .Range("A2:X" & .Range("A" & Rows.count).End(xlUp).Row).Delete
End With
 
Upvote 0
How about
VBA Code:
With Workbooks("t.xlsx").Sheets("main")
   .Range("A2:X" & .Range("A" & Rows.count).End(xlUp).Row).Delete
End With
would this still work if i had to open the workbook?

With Workbooks.open("t.xlsx").Sheets("main")
.Range("A2:X" & .Range("A" & Rows.count).End(xlUp).Row).Delete
End With
 
Upvote 0
You have to open the workbook to delete the cells.
 
Upvote 0
ok understood so then the below will work and seems to be :)

Workbooks.open(fromPath & "t.xlsx")
With Workbooks("t.xlsx").Sheets("main")
.Range("A2:X" & .Range("A" & Rows.count).End(xlUp).Row).Delete
End With
 
Upvote 0
ok understood so then the below will work and seems to be :)

Workbooks.open(fromPath & "t.xlsx")
With Workbooks("t.xlsx").Sheets("main")
.Range("A2:X" & .Range("A" & Rows.count).End(xlUp).Row).Delete
End With
so its actually deleting my top row. where I want it to delete everything below from A2 to X2 down
 
Upvote 0
That would suggest that col A is empty.
What column will always have data?
 
Upvote 0
Yes but which column will always have data on the last row?
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,289
Members
449,149
Latest member
mwdbActuary

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