第一次接触VBA,原来Excel有这么有趣的功能。倒腾了一天写了第一个宏,功能是实现了,但代码的可读性实在是差了一些。
VBA的语法比较容易理解的,在写If-else的时候,由于没有循环之间的层级不是特别明显,如果循环体过多,后期修改非常容易破坏分支之间的逻辑关系。此外,excel在通过宏分级的时候,会出现分了级但没有+-号的情况,具体原因有待研究。
八个层次的分级实现:
代码如下:
Sub GroupCells()
Dim myRange As Range
Dim rowCount As Integer, currentRow As Integer
Dim firstRow As Integer, lastRow As Integer
Dim currentRowValue As String
Dim neighborColumnValue As String
Dim findstr As String
Dim totalChapter As Integer, chapter As Integer, level As Integer
Set myRange = Sheet1.Range("A65536")
rowCount = Cells(Rows.Count, myRange.Column).End(xlUp).Row
findstr = "."
firstRow = 1
lastRow = 0
total = 1
For currentRow = 1 To rowCount
currentRowValu