Selecting columns in VBA isn't working correctly

CaptainGravyBum

Board Regular
Joined
Dec 1, 2023
Messages
77
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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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