Vba 取消合并和填充值-如果单元格合并到同一列中
我有一个VBA代码,用于取消合并单元格并填充这些单元格的值 我希望仅当合并的单元格属于同一列时,才具有填充值的功能Vba 取消合并和填充值-如果单元格合并到同一列中,vba,excel,Vba,Excel,我有一个VBA代码,用于取消合并单元格并填充这些单元格的值 我希望仅当合并的单元格属于同一列时,才具有填充值的功能 Sub UnMergeFill() Dim cell As Range, joinedCells As Range For Each cell In ThisWorkbook.ActiveSheet.UsedRange If cell.MergeCells Then Set joinedCells = cell.Merge
Sub UnMergeFill()
Dim cell As Range, joinedCells As Range
For Each cell In ThisWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
' I need an if loop here to check if the Range happens to be in the same column
joinedCells.Value = cell.Value
End If
Next
End Sub
例如,我希望所有单元格都不合并。但只有Test2值需要向下拖动到这些单元格中,因为它们都属于同一列。测试值不应在跨不同列时使用
选项显式
子填充()
暗淡单元格作为范围
将连接的单元格变暗为范围
作为布尔函数的Dim-multicl
对于此工作簿中的每个单元格。ActiveSheet.UsedRange
如果cell.MergeCells那么
“/多列?
如果cell.MergeArea.Columns.Count,那么哪些代码不起作用?你能给出一些屏幕截图的例子吗?一切都很好(除了我在寻找代码的注释部分)。它是在不同列之间取消单元格合并,并在不同列之间复制值。我可以在不同的列之间取消合并,但仅当合并范围属于同一列时才复制值。你能看一下代码上的注释部分吗?
Option Explicit
Sub UnMergeFill()
Dim cell As Range
Dim joinedCells As Range
Dim MultiCol As Boolean
For Each cell In ThisWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
'/ Multiple columns?
If cell.MergeArea.Columns.Count <= 1 Then
MultiCol = False
Else
MultiCol = True
End If
Set joinedCells = cell.MergeArea
cell.MergeCells = False
'/ Only when all the cells are in same column
If Not MultiCol Then
joinedCells.Value = cell.Value
End If
End If
Next
End Sub