VBA HELP - "If Sheet has no Meged Cells Do Nothing, If Merged Cells then Run Code" Excel 2013

magnum5az

New Member
Joined
Jun 21, 2017
Messages
16
Hello All,

I am working on a piece of code that will look at a specific sheet and if there are merged cells present run a code to remove all merged cells and move a header, if no merged cells exist on the sheet then do nothing.

Here is what I've come up with but need the "Case" or "If/Then" code

Code:
Sub ReOrgData()

    Dim Sourcesht As Worksheet
    
    Set Sourcesht = Sheets("AR Aging-Combined Property")
    
    
    Sourcesht.Cells.Select
    Selection.UnMerge
    Range("B2:B4").Select
    Selection.Cut
    Range("A2").Select
    ActiveSheet.Paste
    Rows("7:7").Select
    Selection.Delete Shift:=xlUp
    Application.CutCopyMode = False



End Sub

Thanks in advance for any help on this.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hello All,

I am working on a piece of code that will look at a specific sheet and if there are merged cells present run a code to remove all merged cells and move a header, if no merged cells exist on the sheet then do nothing.

Here is what I've come up with but need the "Case" or "If/Then" code

Code:
Sub ReOrgData()

    Dim Sourcesht As Worksheet
    
    Set Sourcesht = Sheets("AR Aging-Combined Property")
    
    
    Sourcesht.Cells.Select
    Selection.UnMerge
    Range("B2:B4").Select
    Selection.Cut
    Range("A2").Select
    ActiveSheet.Paste
    Rows("7:7").Select
    Selection.Delete Shift:=xlUp
    Application.CutCopyMode = False



End Sub

Thanks in advance for any help on this.

You don't really need an If ... Then statement. Just run the code to unmerge and do the work and it will take care of it. If the cells are merged they will unmerge. If they are not merged, vba will ignore the command and move on.
 
Upvote 0
You don't really need an If ... Then statement. Just run the code to unmerge and do the work and it will take care of it. If the cells are merged they will unmerge. If they are not merged, vba will ignore the command and move on.


But they do need an If..Then statement...

"if there are merged cells present then run a code to remove all merged cells and move a header"

So, if there are no merged cells, then he doesn't want to move the header.

Without an If..Then, the header will be moved no matter what.
 
Upvote 0
Untested, but this should work for you:

Code:
Sub ReOrgData()
Dim c As Range
Dim mCells As Boolean
Dim Sourcesht As Worksheet

Set Sourcesht = Sheets("AR Aging-Combined Property")

For Each c In Sourcesht.UsedRange
    If c.MergeCells Then
        mCells = True
        GoTo ExitFor
    End If
Next c

ExitFor:
If mCells Then
    Sourcesht.Cells.UnMerge
    Sourcesht.Range("B2:B4").Cut
    Sourcesht.Range("A2").Select
    Sourcesht.Paste
    Sourcesht.Rows("7:7").Delete Shift:=xlUp
    Application.CutCopyMode = False
End If
End Sub
 
Upvote 0
But they do need an If..Then statement...

"if there are merged cells present then run a code to remove all merged cells and move a header"

So, if there are no merged cells, then he doesn't want to move the header.

Without an If..Then, the header will be moved no matter what.

You can test if there is at least one merged cells (could be more, lots more) like this...

If IsNull(Sourcesht.Cells.MergeCells) Then

Note: The above line of code assumes at least one cell on the worksheet is not merged (which seems like a reasonanble assumption to me).
 
Last edited:
Upvote 0
Worked like a charm! Thanks for this!

Untested, but this should work for you:

Code:
Sub ReOrgData()
Dim c As Range
Dim mCells As Boolean
Dim Sourcesht As Worksheet

Set Sourcesht = Sheets("AR Aging-Combined Property")

For Each c In Sourcesht.UsedRange
    If c.MergeCells Then
        mCells = True
        GoTo ExitFor
    End If
Next c

ExitFor:
If mCells Then
    Sourcesht.Cells.UnMerge
    Sourcesht.Range("B2:B4").Cut
    Sourcesht.Range("A2").Select
    Sourcesht.Paste
    Sourcesht.Rows("7:7").Delete Shift:=xlUp
    Application.CutCopyMode = False
End If
End Sub
 
Last edited:
Upvote 0
Worked like a charm! Thanks for this!
Code:
Sub ReOrgData()
Dim c As Range
Dim mCells As Boolean
Dim Sourcesht As Worksheet

Set Sourcesht = Sheets("AR Aging-Combined Property")

For Each c In Sourcesht.UsedRange
    If c.MergeCells Then
        mCells = True
        GoTo ExitFor
    End If
Next c

ExitFor:
If mCells Then
    Sourcesht.Cells.UnMerge
    Sourcesht.Range("B2:B4").Cut
    Sourcesht.Range("A2").Select
    Sourcesht.Paste
    Sourcesht.Rows("7:7").Delete Shift:=xlUp
    Application.CutCopyMode = False
End If
End Sub
All of the code above can be replaced by this...
Code:
Sub ReOrgData()
  With Sheets("AR Aging-Combined Property")
    If IsNull(.Cells.MergeCells) Then
      .Cells.UnMerge
      .Range("B2:B4").Cut .Range("A2")
      .Rows("7:7").Delete Shift:=xlUp
    End If
  End With
End Sub
 
Upvote 0
Rick,

Shouldn't it be:

Rich (BB code):
If Not IsNull(.Cells.MergeCells) Then
 
Upvote 0
Rick,

Shouldn't it be:

Rich (BB code):
If Not IsNull(.Cells.MergeCells) Then
No, it is correct as originally written. Cells.MergeCells will return False if no merged cells exist, True if all cells are involved in a merge (a highly, highly unlikely scenario) and Null if the sheet contains a mixture of merged and non-merged cells. Since the mixture of merged and non-merged cells is what we are after (I completely discount a True result as such a sheet would be ridiculous), we need to test if Cells.MergeCells returns Null... IsNull does that (it will return True when MergeCells is Null).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
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