Merging matching cells' contents rather than the formula

antiplank

New Member
Joined
Mar 25, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to search through a set of cells, find matches and merge the cells that match adjacent ones. What I have so far works fine but I suppose I may be going about this the wrong way. I want to search through the displayed value. In my case, it's a series of dates that are formatted to show only the month. I'd like to go through and merge all matching months out of them. So for May, I'd like it to find all twenty workdays, match the month, and then merge those cells. The cells contain simple formulas like this =WORKDAY(AH4,1) that build out a work day calendar. I'm likely on the wrong path, here, since I seem to have a habit of that but any help would be appreciated.

VBA Code:
Option Explicit
Sub Merge_Same_rngs()
Application.DisplayAlerts = False

Dim rng As Range
Mergerngs:
For Each rng In Sheets("Sheet1").Range("J3:ZZ3")
If rng.Value = rng.Offset(0, 1).Value And rng.Value <> "" Then
Range(rng, rng.Offset(0, 1)).Merge
Range(rng, rng.Offset(0, 1)).HorizontalAlignment = xlCenter
Range(rng, rng.Offset(0, 1)).VerticalAlignment = xlCenter
GoTo Mergerngs
End If
Next

End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hello,

I'm trying to search through a set of cells, find matches and merge the cells that match adjacent ones. What I have so far works fine but I suppose I may be going about this the wrong way. I want to search through the displayed value. In my case, it's a series of dates that are formatted to show only the month. I'd like to go through and merge all matching months out of them. So for May, I'd like it to find all twenty workdays, match the month, and then merge those cells. The cells contain simple formulas like this =WORKDAY(AH4,1) that build out a work day calendar. I'm likely on the wrong path, here, since I seem to have a habit of that but any help would be appreciated.

VBA Code:
Option Explicit
Sub Merge_Same_rngs()
Application.DisplayAlerts = False

Dim rng As Range
Mergerngs:
For Each rng In Sheets("Sheet1").Range("J3:ZZ3")
If rng.Value = rng.Offset(0, 1).Value And rng.Value <> "" Then
Range(rng, rng.Offset(0, 1)).Merge
Range(rng, rng.Offset(0, 1)).HorizontalAlignment = xlCenter
Range(rng, rng.Offset(0, 1)).VerticalAlignment = xlCenter
GoTo Mergerngs
End If
Next

End Sub

I just realized my subject makes no sense because I wrote that first and then realized what I was really asking for, sorry about that.
 
Upvote 0
Ok, just figured it out, I was using the .Value property, not .Text.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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