Dim letters As Integer ‘声明模块级变量,此变量计算字母个数
Dim space As Integer ‘空格个数
Dim digit As Integer ‘数字个数
Dim others As Integer ‘其他字符个数
Function auto_level() As Integer
Dim space_num As Integer
Dim str1 As String
Dim begin_line As Integer
Dim end_line As Integer
Dim mid_line As Integer
begin_line = 0
end_line = 0
mid_line = 0
SumRows = Sheet1.UsedRange.Rows.Count
‘find the "echo"
For i = 1 To SumRows
str1 = Sheet1.Cells(i, 1)
str2 = Split(str1, " ")
j = UBound(str2)
For jj = 0 To j
If str2(jj) = "" Then
‘ do nothing
Else
‘MsgBox "comment of str2 is :" & str2(jj) & " jj is " & jj & " row " & i
If str2(jj) = "echo" Then
If begin_line = 0 Then
mid_line = begin_line
begin_line = i + 1
Else
If end_line = 0 Then
end_line = i - 1
Else
begin_line = end_line + 2
end_line = i - 1
End If
‘MsgBox "line number is " & begin_line & end_line & " i = " & i
Call recursion_group(begin_line + 1, end_line - 1)
End If
Exit For
End If
Exit For
End If
Next
‘MsgBox str2(1) & " len of str2 is: " & j & " row " & i
Next
‘start recursion group the rows (i,j)
str1 = Sheet1.Cells(14, 1)
space_num = get_space_num(str1)
‘MsgBox "space_num is :" & space_num & "SumRows is " & SumRows
End Function
Sub auto_leve()
Call auto_level
End Sub
Function recursion_group(begin_lin As Integer, end_lin As Integer)
Dim begin_line As Integer
Dim end_line As Integer
Dim space_num1 As Integer
Dim space_num2 As Integer
Dim str1 As String
Dim i As Integer
begin_line = begin_lin
end_line = end_lin
str1 = ""
str1 = Sheet1.Cells(begin_line, 1)
space_num1 = get_space_num(str1)
For i = begin_line + 1 To end_line
str1 = Sheet1.Cells(i, 1)
space_num2 = get_space_num(str1)
If space_num1 = space_num2 Then
If i - begin_line > 1 Then
Call create_group1(begin_line + 1, i)
Call recursion_group(begin_line + 1, i - 1)
If i < end_line Then
Call recursion_group(i + 1, end_line)
Exit For
End If
Else
begin_line = i
End If
End If
‘MsgBox str2(1) & " len of str2 is: " & j & " row " & i
Next
End Function
Function create_group1(begin_line As Integer, end_line As Integer)
‘Rows("13:33").Select
Rows(begin_line & ":" & end_line).Select
Selection.Rows.Group
With ActiveSheet.Outline
.AutomaticStyles = False
.SummaryRow = xlAbove
.SummaryColumn = xlRight
End With
End Function
Function un_create_group1(begin_line As Integer, end_line As Integer)
‘Rows("13:33").Select
Rows(begin_line & ":" & end_line).Select
Selection.Rows.Ungroup
End Function
Sub create_group()
‘
‘ create_group Macro
‘
‘ Keyboard Shortcut: Ctrl+p
‘
Rows("13:33").Select
Selection.Rows.Group
With ActiveSheet.Outline
.AutomaticStyles = False
.SummaryRow = xlAbove
.SummaryColumn = xlRight
End With
End Sub
Sub un_group()
‘
‘ un_group Macro
‘
Selection.Rows.Ungroup
Selection.Rows.Ungroup
End Sub
Function get_space_num(inputstr As String) As Integer
Dim space_num As Integer
space_num = 0
str1 = inputstr
lenth = Len(str1)
‘MsgBox str1 & " Len of str1 is: " & lenth
For i = 1 To lenth
str2 = Mid(str1, i, 1)
If str2 <> " " Then
‘MsgBox str2
Exit For
Else
space_num = space_num + 1
End If
Next
get_space_num = space_num
End Function
Private Sub Command11_Click()
Dim s As String
s = InputBox("请输入字符串")
Call jisuan(s)
‘以下代码用来显示统计出的结果值
Print "字符串【" & s & "】包含:"
Print "英文字母数量="; letters
Print "空格数量="; space
Print "数字数量="; digit
Print "其他字符数量="; others
End Sub
Private Sub jisuan(inputstr As String) ‘InputStr变量存储输入的字符串
Dim i As Integer ‘循环控制变量,整型
Dim CaseStr As String ‘此变量保存储所截取的字符
letters = 0 ‘初始化为0
space = 0
digit = 0
others = 0
For i = 1 To Len(inputstr) ‘开始分别统计个数
CaseStr = Mid(inputstr, i, 1) ‘取得某个字符
Select Case CaseStr
Case "a" To "z", "A" To "Z" ‘如果字符是英文字母
letters = letters + 1
Case " " ‘如果字符是空格
space = space + 1
Case 0 To 9 ‘如果字符是数字
digit = digit + 1
Case Else ‘如果字符是其他字母
others = others + 1
End Select
Next
End Sub