Deleting columns with merged cells in a macro

Joined
Jan 3, 2012
Messages
20
I have a worksheet where columns A:D are merged. I want to delete columns B:D and this works perfect when I manually select column B and then C and finally D.

I have recorded this, but the macro deletes all the 4 columns.

I have also tried to unmerge the columns and this also works fine manually, but again when recorded and done as a macro it doen't work.

Anybody have the explaination and solution to this
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Frank.Egelund.Jensen,

The below macro will first check in the cells in the active worksheet if Range("A1:D1") are merged. If they are the macro will do what you require.

Sample raw data in the active worksheet (sorry, the below screenshot does not show cells/rows A1:D9 merged):


Excel 2007
ABCDE
1A1E1
2A2E2
3A3E3
4A4E4
5A5E5
6A6E6
7A7E7
8A8E8
9A9E9
10A10B10C10D10E10
11
Sheet1


After the macro:


Excel 2007
ABCDE
1A1E1
2A2E2
3A3E3
4A4E4
5A5E5
6A6E6
7A7E7
8A8E8
9A9E9
10A10E10
11
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub UnMergedABCD_DeleteBCD()
' hiker95, 08/15/2013
' http://www.mrexcel.com/forum/excel-questions/720348-deleting-columns-merged-cells-macro.html
With ActiveSheet
  If .Range(.Cells(1, 1), .Cells(1, 4)).MergeCells = True Then
    .Columns("A:D").MergeCells = False
    .Columns("B:D").Delete
  End If
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the UnMergedABCD_DeleteBCD macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,250
Messages
6,171,036
Members
452,374
Latest member
keccles

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