VBA - Merged columns

ChrisFoster

Board Regular
Joined
Jun 21, 2019
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a number of reports that are created by my CRM system. Some of the reports were built by a developer who liked to merge data across numerous columns - for instance column A:B = First Name, C:D = Surname.

Each report could have merged data across various different columns, so it's not always consistent.

The data I need is always stored in the left hand column, so where column A:B = First Name - I could just delete column B and this unmerges the cells and leaves me with the data I need but now just in one column, which would be A.

Is there any VBA code that I can use to find and delete the blank columns within a merged range? My thinking is I could maybe use code to unmerge the whole worksheet, then delete any blank columns - But I'm unsure of the best way forward. Any help would be appreciated.

Regards,

Chris
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Would be something like this:

VBA Code:
    If Range("A1").MergeCells = True Then
        Range("B1").EntireColumn.Delete
    End If

Always test on a copy of data
 
Upvote 0
Would be something like this:

VBA Code:
    If Range("A1").MergeCells = True Then
        Range("B1").EntireColumn.Delete
    End If

Always test on a copy of data
This will work if the merged columns are always A:B - but the merged data across various different columns, so it's not always consistent.
 
Upvote 0
Maybe :
VBA Code:
Sub v()
Dim cols%, c%
Cells.MergeCells = False
cols = Range([A1], ActiveSheet.UsedRange).Columns.Count
For c = cols To 1 Step -1
    If Application.CountA(Columns(c)) = 0 Then Columns(c).Delete
Next
End Sub
 
Upvote 0
So would you like to check all the way across row 1 and check if it is merged, and if so delete the adjacent row?
 
Upvote 0
Maybe :
VBA Code:
Sub v()
Dim cols%, c%
Cells.MergeCells = False
cols = Range([A1], ActiveSheet.UsedRange).Columns.Count
For c = cols To 1 Step -1
    If Application.CountA(Columns(c)) = 0 Then Columns(c).Delete
Next
End Sub

Works perfectly, thank-you.
 
Upvote 0

Forum statistics

Threads
1,214,618
Messages
6,120,544
Members
448,970
Latest member
kennimack

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