您好,我目前正在使用一个宏,该宏可以为我自动设置表格格式,并且将所有单元格集中对齐,除了第一个选定列中的单元格。
我想知道是否有一种方法可以对此进行调整,以便第一个选定的列仅在包含文本而不是在包含数字的情况下才向左对齐
代码如下:
Sub Test_align_left()
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Columns(1).Select
On Error Resume Next
With Selection
.SpecialCells(xlCellTypeConstants, xlTextValues).HorizontalAlignment = xlLeft
.SpecialCells(xlCellTypeFormulas, xlTextValues).HorizontalAlignment = xlLeft
End With
End Sub
预先感谢
托马斯
答案 0 :(得分:2)
如果您是说如果文本是左对齐,或者如果是数字则居中,那么这是避免循环遍历每个单元格的方法。
Sub x()
On Error Resume Next
With Columns(1)
.SpecialCells(xlCellTypeConstants, xlTextValues).HorizontalAlignment = xlLeft
.SpecialCells(xlCellTypeConstants, xlNumbers).HorizontalAlignment = xlCenter
.SpecialCells(xlCellTypeFormulas, xlNumbers).HorizontalAlignment = xlCenter
.SpecialCells(xlCellTypeFormulas, xlTextValues).HorizontalAlignment = xlLeft
End With
End Sub
答案 1 :(得分:1)
如果只想保留第一列,可以执行以下操作:
Sub Test_align_left()
'Test_align_left Macro
With Selection.offset(0,1).resize(,Selection.columns.count-1)
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub