Cells(1,1).Value
is "A". Cells(1,2).Value
is "---".
Cells(2,1).Value
is empty. Cells(3,2).Value
is empty.
Cells(3,1).Value
is "B". Cells(3,2).Value
is empty.
Just because you merge the cells above, doesn't change that the cell is in Row 3. Only the top-left cell in a Merged Range has a value. (Although, there are ways around that.)
You can check if a cell is merged using Range.MergeCells
. You can get the value of a Merged Cell using Range.MergeArea
For example:
Cells(1,1).MergeArea.Cells(1,1)
is "A". Cells(2,2).MergeArea.Cells(1,1)
is "---". Cells(1,1).MergeCells
is True
.
Cells(2,1).MergeArea.Cells(1,1)
is "A". Cells(2,2).MergeArea.Cells(1,1)
is "---". Cells(2,1).MergeCells
is True
.
Cells(3,1).MergeArea.Cells(1,1)
is "B". Cells(3,2).MergeArea.Cells(1,1)
is "---". Cells(3,1).MergeCells
is False
.
If you are using a loop to run through the cells, consider using a While
or Do
instead of a For
, since you can increment by different numbers for each iteration:
Dim rowCurrent AS Long
rowCurrent = 1
While Cells(rowCurrent,1).Value <> ""
If Cells(rowCurrent,2).MergeArea.Cells(1,1).Value = "---" Then
Select Case Cells(rowCurrent,1).MergeArea.Cells(1,1).Value
Case "A"
'concatenate something
Case "B"
'concatenate something else
Case "C"
'concatenate a third thing
Case Else
'throw an error?
End Select
End If
'Skip the rest of the merged cell
rowCurrent = rowCurrent + Cells(rowCurrent,1).MergeArea.Rows.Count
Wend