我的excel电子表格中有J到X列,其中包含以下值:
J K L M N O P Q R S T U V W X
1 1 1 1 1 2 2 2 2 2 2 2 3 3 3
其中值是变量本身并且可以更改。例如,值也可以是:
J K L M N O P Q R S T U V W X
1 2 2 2 2 2 2 2 3 3 3 3 3 3 3
我正在尝试编写一个vba,它将所有单元格(向右)合并为值1,2,3等。任何帮助都将不胜感激。
示例1的结果将是:列J到N将合并为1.列O到U将合并为2.列V到X将合并为3.见下文
J O V
1 2 3
谢谢。
我在其他地方找到了答案。如果有人有兴趣,我会留在这里
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim rngMerge As Range, cell As Range
Set rngMerge = Range("J5:X5") 'Set the range limits here
MergeAgain:
For Each cell In rngMerge
If cell.Value = cell.Offset(0, 1).Value And IsEmpty(cell) = False Then
Range(cell, cell.Offset(0, 1)).Merge
GoTo MergeAgain
End If
Next
答案 0 :(得分:0)
以下是我回答问题的方法。如果有人有兴趣。
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim rngMerge As Range, cell As Range
Set rngMerge = Range("J5:X5") 'Set the range limits here
MergeAgain:
For Each cell In rngMerge
If cell.Value = cell.Offset(0, 1).Value And IsEmpty(cell) = False Then
Range(cell, cell.Offset(0, 1)).Merge
GoTo MergeAgain
End If
Next