|
合并相同单元格vba 自定义函数
Sub MergeCells_相同单元格(rng, Optional fx = "rc")
Set rng = rng
If fx = "rc" Then
' 合并行方向上的相同单元格
For i = rng.Rows.Count - 1 To 1 Step -1
For j = 1 To rng.Columns.Count
If rng.Cells(i, j).Value2 = rng.Cells(i + 1, j).Value2 Then
rng.Cells(i, j).Resize(2, 1).Merge
End If
Next j
Next i
' 合并列方向上的相同单元格
For j = rng.Columns.Count - 1 To 1 Step -1
For i = rng.Rows.Count To 1 Step -1
tj = (rng.Cells(i, j).MergeArea.Cells(1, 1).Value2 = rng.Cells(i, j + 1).MergeArea.Cells(1, 1).Value2)
If tj Then
Union(rng.Cells(i, j).MergeArea, rng.Cells(i, j + 1).MergeArea).Merge
End If
Next i
Next j
End If
If fx = "r" Then
For i = rng.Rows.Count - 1 To 1 Step -1
For j = 1 To rng.Columns.Count
If rng.Cells(i, j).Value2 = rng.Cells(i + 1, j).Value2 Then
rng.Cells(i, j).Resize(2, 1).Merge
End If
Next j
Next i
End If
If fx = "c" Then
For j = rng.Columns.Count - 1 To 1 Step -1
For i = rng.Rows.Count To 1 Step -1
tj = (rng.Cells(i, j).MergeArea.Cells(1, 1).Value2 = rng.Cells(i, j + 1).MergeArea.Cells(1, 1).Value2)
If tj Then
Union(rng.Cells(i, j).MergeArea, rng.Cells(i, j + 1).MergeArea).Merge
End If
Next i
Next j
End If
End Sub |
评分
-
1
查看全部评分
-
|