【发布时间】:2015-08-03 12:09:38
【问题描述】:
我遇到了将报告处理成有用的结构化 Excel 模型的问题。
我的问题是此报告中的单元格已合并,现在我想取消合并它们以更轻松地处理信息。
我尝试使用宏记录器记录一些东西,但我不确定如何在工作表中的每个单元格上自动执行它。
我想让输出看起来像这样:
这是我录制的部分:
Sub Macro1()
Range("A2:A3").Select
With Selection
.HorizontalAlignment = xlGeneral
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
Range("A2").Select
Selection.AutoFill Destination:=Range("A2:A3")
Range("A2:A3").Select
End Sub
有什么建议可以重写这个宏来自动进行合并和粘贴吗?
感谢您的回复!
更新
我尝试使用选择,但是,我目前面临不知道如何获取下一个单元格的问题:
Sub split()
'
'Dim C As Double
'Dim R As Double
Dim Rng As Range
'select cells
Set Rng = Selection
'C = Rng
'R = 10
For Each cell In Rng
'starts in row 2 and A -> cell 2,1 is the first cell or A2
cell.Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
'Cells(R + 1, C) = Cells(R, C)
If cell.Value = "" Then MsgBox ("Finished splitting and copying!"): End
' If C = 7 Then C = 0: R = R + 2
Next cell
End Sub
【问题讨论】:
-
检查
selection.mergecells= true是否使用顶行的数据创建一个新行 -
好的,我正在尝试一个代码,如果它有效,我会给你:)
-
我刚试了一下,效果很好
-
@Hearner 我在这里遇到错误:
Range("K:S").MergeCells = False ' remove merge我的错误是unable to set the MergeCells property of the Range class -
尝试在
Sheets("Sheet1").range("K:S")或Range(cells(2, 11), cells(2+NbRows, 11+ NbCols))之前添加您的Sheets
标签: vba excel excel-2010