Hi all
Hope you are all well. I am trying to work my way through a new project requirement. I need to remove columns from a sheet prior to sending it out. However, this range is often a variable. I want to use the value in the B2 and B3 cells to form the range to be used in the macro.
In other words, if B2 and B3 are 5 and 11 respectively, columns E to K are to be deleted.
However, I am having trouble getting this part to actually work:
Did I 'dim' it wrongly? Should I be using numbers or should I be using the column names (ie E etc)?
Much thanks in advance
Hope you are all well. I am trying to work my way through a new project requirement. I need to remove columns from a sheet prior to sending it out. However, this range is often a variable. I want to use the value in the B2 and B3 cells to form the range to be used in the macro.
VBA Code:
Sub DeleteColumns()
Dim StartCol As Long
Dim LastCol As Long
StartCol = Sheets("Sheet1").Range("B2").Value
LastCol = Sheets("Sheet1").Range("B3").Value
Sheets("Sheet2").Select
Columns(StartCol : LastCol).EntireColumn.Delete
End Sub
In other words, if B2 and B3 are 5 and 11 respectively, columns E to K are to be deleted.
However, I am having trouble getting this part to actually work:
Code:
Columns(StartCol : LastCol).EntireColumn.Delete
Did I 'dim' it wrongly? Should I be using numbers or should I be using the column names (ie E etc)?
Much thanks in advance