Excel 中增加缩进量 使用宏实现
最近工作中需要处理,根据Heading列的层级关系,对Name列增加缩进量。
实现了如下宏:
Sub InsertIndent()
'
' macro
'
'
Dim rows_count, heading, name, comma_count, min_comma_count, i
Set heading = ActiveSheet.UsedRange.Find(what:="Heading")
If heading Is Nothing Then
MsgBox ("No Exist Heading Column, Please Export Heading Column")
Exit Sub
End If
Set name = ActiveSheet.UsedRange.Find(what:="Name")
If name Is Nothing Then
MsgBox ("No Exist Name Column, Please Export Name Column")
Exit Sub
End If
min_comma_count = UBound(Split(Cells(heading.Row + 1, heading.Column), "."))
If min_comma_count < 1 Then
MsgBox ("Heading Column Format Error")
Exit Sub
End If
rows_count = ActiveSheet.UsedRange.Rows.Count
For i = 1 To rows_count
comma_count = UBound(Split(Cells(heading.Row + i, heading.Column), "."))
If comma_count - min_comma_count >= 0 Then
Range(Cells(name.Row + i, name.Column).Address).IndentLevel = comma_count - min_comma_count
End If
Next
MsgBox ("Name Column Insert Indent Finished.")
End Sub
表单样式
| Heading | Name |
| 1.6.2.3 | Excel导入测试 |
| 1.6.2.3.1 | 场景模式 |
| 1.6.2.3.2 | 场景模式产品框架 |