How To Delete Column S to Last Column?

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
781
Office Version
  1. 2016
Platform
  1. Windows
I have this code and its not working, can anyone explain to me why?

Code:
sub test()
Dim lastCol As Long

with activesheet
    lastCol = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
    Columns(17 & ":" & lastCol).Delete Shift:=xlToLeft
end with

or

Code:
sub test()
Dim lastCol As Long

with activesheet
    lastCol = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
    Range(0, 17 & ":" & lastCol).Delete Shift:=xlToLeft
end with

the only alternative i can think of is change lastCol to a string and use

Code:
range("T:" & lastCol)
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Are you trying to delete col S onwards, col Q on wards or col T onwards?
 
Upvote 0
Try...

Code:
Range(Cells(1, "S"), Cells(1, Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column)).EntireColumn.Delete
 
Upvote 0
Try...

Code:
Range(Cells(1, "S"), Cells(1, Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column)).EntireColumn.Delete

that worked as intended.
could you explain how this method works as opposed to rowcount method for finding last row?
 
Upvote 0
Either as Mark has shown, or
Code:
Sub test()
Dim lastCol As Long

    lastCol = Range("A1").SpecialCells(xlCellTypeLastCell).Column
    Range("T1", Cells(1, lastCol)).EntireColumn.Delete Shift:=xlToLeft
End Sub
 
Upvote 0
Either as Mark has shown, or
Code:
Sub test()
Dim lastCol As Long

    lastCol = Range("A1").SpecialCells(xlCellTypeLastCell).Column
    Range("T1", Cells(1, lastCol)).EntireColumn.Delete Shift:=xlToLeft
End Sub

This works as intended and i see what the difference was, thank you
 
Upvote 0
that worked as intended.
could you explain how this method works as opposed to rowcount method for finding last row?

Find works backwards (due to xlprevious) from the first cell in a zig-zag pattern until it finds the last cell so in practice it works backwards from the last cell on the sheet(or a range if set) until it finds the last cell.

Rows.count (in your case because you are dealing with columns it would be Columns.Count) works backwards from the last cell in the specified row or column.

So the difference is with .Count you need the specific Row/Column and it only finds the last cell in that specific Row/Column.

The Find method when used with Cells finds the last Row/Column no matter which Row/Column is longest.

I never use SpecialCells(xlCellTypeLastCell) as (because it uses the usedrange) it has a nasty habit of retaining a memory and also picks up formats as well as values.
 
Upvote 0

Forum statistics

Threads
1,215,828
Messages
6,127,125
Members
449,361
Latest member
VBquery757

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