VBA code to merger cells improvment

Mjk7239

New Member
Joined
Sep 29, 2013
Messages
14
All,

Thanks in advance, I have this code that merges and centers cells with equal values, but it seems slow.

any ideas to speed it up would be appreciated.


VBA Code:
Sub Merge_TimeScale()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

ActiveSheet.Range("AD11:HQ11").Select
    
MergeTL_A:

For Each rng In Selection
        
        If rng.Value = rng.Offset(0, 1).Value Then
            Range(rng, rng.Offset(0, 1)).Merge
             GoTo MergeTL_A
        End If
Next

ActiveSheet.Range("AD12:HQ12").Select
    
MergeTL_B:

For Each rng In Selection
        
        If rng.Value = rng.Offset(0, 1).Value Then
            Range(rng, rng.Offset(0, 1)).Merge
             GoTo MergeTL_B
        End If
Next

ActiveSheet.Range("AD13:HQ13").Select
    
MergeTL_C:

For Each rng In Selection
        
        If rng.Value = rng.Offset(0, 1).Value Then
            Range(rng, rng.Offset(0, 1)).Merge
             GoTo MergeTL_C
        End If
Next

ActiveSheet.Range("AD11:HQ13").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
why would you want to merge cells. It will cause all sorts of problems when trying to do calculations, saving, copying, etc !!
Don't have Excel atm, so is UNTESTED
VBA Code:
Sub Merge_TimeScale()
Dim cell As Variant
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each cell In Range("AD11:HQ11, AD12:HQ12, AD13:HQ13")
        If cell.Value = cell.Offset(0, 1).Value Then
            Range(cell, cell.Offset(0, 1)).Merge
        cell.HorizontalAlignment = xlCenter
        cell.VerticalAlignment = xlCenter
        End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,567
Messages
6,120,268
Members
448,953
Latest member
Dutchie_1

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