Selecting columns in VBA isn't working correctly

CaptainGravyBum

New Member
Joined
Dec 1, 2023
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hello,
I'm hoping this isn't a stupid question, but it's confusing the pants off me.
I recorded a macro to select certain columns and delete them, pretty simple right? should be fine.

Sub Macro1()
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
End Sub

Works ok if I do it myself, but if I run the macro it either selects the entire table or multiple columns. The only thing I can think is that there are some merged cells in the sheet which are overriding which columns are selected.
Does my theory sound correct and how can I get around the problem?

Thanks,
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I think that I've solved my own problem here, I had overcomplicated things so I simply selected all columns and removed the merge cells before deleting the columns.
Columns("A:F").Select
Selection.UnMerge
Easy really.
 
Upvote 0
Solution
Yes, it's the merged cells but you don't have to select anything there:

VBA Code:
Sub Macro1()
Columns("B:B").Delete Shift:=xlToLeft
Columns("E:E").Delete Shift:=xlToLeft
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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