VBA: Delete Columns F onwards Except Last Column

dianeslao

New Member
Joined
Mar 24, 2018
Messages
4
Hello, can you please help share an excel VBA macro that deletes columns F onwards except the last column.
Last Column changes, it can be the column F or AZ.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi & welcome to the board.
How about
Code:
Sub DelCols()

   Dim Usdcols As Long
   
   Usdcols = Cells(1, Columns.Count).End(xlToLeft).Column
   If Usdcols > 5 Then
      Range("F1", Cells(1, Usdcols - 1)).EntireColumn.Delete
   End If
End Sub
 
Upvote 0
hi & welcome to the board.
How about
Code:
sub delcols()

   dim usdcols as long
   
   usdcols = cells(1, columns.count).end(xltoleft).column
   if usdcols > 5 then
      range("f1", cells(1, usdcols - 1)).entirecolumn.delete
   end if
end sub


thankyou!
 
Upvote 0
Hello, thanks for your help. but i noticed that when Column G onwards in my source file is empty the macro deletes Column E and F.
PLEASE help modify. :(
 
Upvote 0
It should be
Code:
If Usdcols > [COLOR=#ff0000]6 [/COLOR]Then
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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