Merging cells and changing colour

markh9999

New Member
Joined
Aug 21, 2007
Messages
49
I need some code that looks at a range of cells and if the string "abcd" appears somewhere in the cell text then I need to merge it with the cell below and change the background colour.

Can someone start me off please?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try like this

Code:
Sub mrg()
Dim c As Range
Application.DisplayAlerts = False
For Each c In Range("A1:D10")
    If c.Value Like "*abcd*" Then
        c.Interior.ColorIndex = 6
        c.Resize(2).Merge
    End If
Next c
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Thank you Peter - so simple when done by an expert !!

Also, how do I now combine the text from the original 2 cells to form the text for the merged cells?
 
Upvote 0
Try

Code:
Sub mrg()
Dim c As Range
Application.DisplayAlerts = False
For Each c In Range("A1:D10")
    If c.Value Like "*abcd*" Then
        c.Value = c.Value & Chr(10) & c.Offset(1).Value
        c.Interior.ColorIndex = 6
        With c.Resize(2)
            .Merge
            .WrapText = True
        End With
    End If
Next c
Application.DisplayAlerts = True
End Sub
 
Upvote 0
Thank you again ... and one final (hopefuly) question:

I'm struggling with the syntax on the following.

Once I've merged the cells using the code below, I want to find and replace some repeated text within the merged cell.

e.g the text *abc* occurs 5 times (* is wildcard) and needs removing.

Can you help?
 
Upvote 0
Try

Code:
Sub mrg()
Dim c As Range
Application.DisplayAlerts = False
For Each c In Range("A1:D10")
    If c.Value Like "*abcd*" Then
        c.Value = c.Value & Chr(10) & c.Offset(1).Value
        c.Interior.ColorIndex = 6
        With c.Resize(2)
            .Merge
            .WrapText = True
        End With
        c.Value = Replace(c.Value, "abc", vbNullString)
    End If
Next c
Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,711
Members
452,939
Latest member
WCrawford

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