I have been at this for a while and it's driving me batty. It's probably much simpler than I'm making it.
To get this out of the way: I HAVE to do this. I HAVE to merge these cells. I don't like it, I don't want to, I know merging cells is always a hassle and should be avoided, but I do not have a choice.
I got a great bit of code that works fine to merge cells in multiple columns when the values in each COLUMN are the same:
This accomplishes the following:
Starting data (note that I have all data sorted in all the applicable columns before starting [each column sorted respectively, e.g. column A sorted, THEN column B, THEN column C, etc.]):
Merged Data (after code):
However, I only want to merge same values in each column if they fall within the rows of the merged area of the column to the left (and the first column is A). Here is what I'm trying to do:
Desired Result:
Note that there are values in columns B (and C) that might be the same, but I don't want to merge every value in each column regardless, I only want to merge values in column B where they are the same BUT ALSO fall within the merged area of one value in column A, and in C where they fall within the merged value of B, etc.
I would love to be able to do all this by beginning with the overall range (e.g. "for range A3:C5000, do this"), but it SEEMS like the only way to accomplish this is to do A first, THEN do B, but CHECK the merged area/cell count of A, THEN do C, but CHECK the merged area/cell count of column B, etc. But every experiment I try is confounded by this Excel merged cell nonsense, like when I check the cell address or merged area or merged cell count to the left, it doesn't quite work out because of how Excel handles the address or cell count of where I am in the loop and what is to the left. Plus all of those checks fail if it's the first column, because there are no cells to the left of A, etc.
Does anyone have opinion/help of a relatively efficient way of reliably doing this?
Thanks for taking the time
To get this out of the way: I HAVE to do this. I HAVE to merge these cells. I don't like it, I don't want to, I know merging cells is always a hassle and should be avoided, but I do not have a choice.
I got a great bit of code that works fine to merge cells in multiple columns when the values in each COLUMN are the same:
VBA Code:
Application.DisplayAlerts = False
Mergecells:
For Each cell In Range("A3:C50")
If cell.Value = cell.Offset(1, 0).Value And Not IsEmpty(cell) Then
Range(cell, cell.Offset(1, 0)).Merge
cell.VerticalAlignment = xlCenter
cell.HorizontalAlignment = xlCenter
GoTo Mergecells
End If
Next
Application.DisplayAlerts = True
This accomplishes the following:
Starting data (note that I have all data sorted in all the applicable columns before starting [each column sorted respectively, e.g. column A sorted, THEN column B, THEN column C, etc.]):
Merged Data (after code):
However, I only want to merge same values in each column if they fall within the rows of the merged area of the column to the left (and the first column is A). Here is what I'm trying to do:
Desired Result:
Note that there are values in columns B (and C) that might be the same, but I don't want to merge every value in each column regardless, I only want to merge values in column B where they are the same BUT ALSO fall within the merged area of one value in column A, and in C where they fall within the merged value of B, etc.
I would love to be able to do all this by beginning with the overall range (e.g. "for range A3:C5000, do this"), but it SEEMS like the only way to accomplish this is to do A first, THEN do B, but CHECK the merged area/cell count of A, THEN do C, but CHECK the merged area/cell count of column B, etc. But every experiment I try is confounded by this Excel merged cell nonsense, like when I check the cell address or merged area or merged cell count to the left, it doesn't quite work out because of how Excel handles the address or cell count of where I am in the loop and what is to the left. Plus all of those checks fail if it's the first column, because there are no cells to the left of A, etc.
Does anyone have opinion/help of a relatively efficient way of reliably doing this?
Thanks for taking the time