Public Sub SmartIndenterProcedure()
Dim OneComp As VBComponent
Dim StartLine As Long, EndLine As Long
Dim LineIndex As Long, LineNo As Long, LineCount
Dim StartCol As Long, EndCol As Long
Dim LineText As String
Dim ProcName As String, KeyWord As String
Dim IndentLevel As Integer, IsAfterUnderLine As Boolean
Dim IndentThisLine As Boolean, BackThisLine As Boolean
Dim IndentNextLine As Boolean, BackNextLine As Boolean For Each OneComp In ActiveWorkbook.VBProject.VBComponents LineCount = OneComp.CodeModule.CountOfLines
For LineNo = 1 To LineCount ProcName = OneComp.CodeModule.ProcOfLine(LineNo, vbext_pk_Proc) ProcLineCount = OneComp.CodeModule.ProcCountLines(ProcName, vbext_pk_Proc)
LineNo = LineNo + ProcLineCount - 1 StartLine = OneComp.CodeModule.ProcStartLine(ProcName, vbext_pk_Proc)
EndLine = OneComp.CodeModule.ProcCountLines(ProcName, vbext_pk_Proc) + StartLine '循环每一行,删除行首缩进
For LineIndex = StartLine To EndLine
LineText = OneComp.CodeModule.Lines(LineIndex, 1)
Do Until Left(LineText, 1) <> " "
OneComp.CodeModule.ReplaceLine LineIndex, Mid(LineText, 2)
LineText = OneComp.CodeModule.Lines(LineIndex, 1)
Loop
Next LineIndex IndentLevel = 0
For LineIndex = StartLine To EndLine
LineText = OneComp.CodeModule.Lines(LineIndex, 1)
KeyWord = Left(LineText, IIf(InStr(LineText, " ") = 0, Len(LineText), InStr(LineText, " ") - 1)) Select Case KeyWord
Case "Do", "For", "Private", "Select", "Sub", "While", "With", "Function", "Type"
IndentNextLine = True
Case "If"
If Right(LineText, 4) = "Then" Then IndentNextLine = True
Case "Loop", "Next", "End"
BackThisLine = True
Case "Case", "Else", "ElseIf"
BackThisLine = True
IndentNextLine = True
Case "Public", "Private"
If Split(LineText, " ")(1) = "Sub" Or Split(LineText, " ")(1) = "Function" Then
IndentNextLine = True
End If
End Select '判断续行问题
If Right(LineText, 2) = " _" And IsAfterUnderLine = False Then
IndentNextLine = True
IsAfterUnderLine = True
ElseIf Right(LineText, 2) <> " _" And IsAfterUnderLine Then
BackNextLine = True
IsAfterUnderLine = False
End If '处理本行的缩进级别
If IndentThisLine Then
IndentLevel = IndentLevel + 1
IndentThisLine = False
End If
If BackThisLine Then
IndentLevel = IndentLevel - 1
BackThisLine = False
End If On Error GoTo ErrHandler
OneComp.CodeModule.ReplaceLine LineIndex, Space$(IndentLevel * 4) & LineText
On Error GoTo 0 If IndentNextLine Then
IndentLevel = IndentLevel + 1
IndentNextLine = False
End If
If BackNextLine Then
IndentLevel = IndentLevel - 1
BackNextLine = False
End If Next LineIndex Next LineNo Next OneComp Set OneComp = Nothing Exit Sub
ErrHandler:
If IndentLevel < 0 Then IndentLevel = 0
Resume Next
End Sub

  

20170719xlVBASmartIndent的更多相关文章

  1. Silverlight之控件应用总结(二)(4)

    [置顶] Silverlight之控件应用总结(二)(4) 分类: 技术2012-04-03 22:12 846人阅读 评论(0) 收藏 举报 silverlightradiobuttonhyperl ...

随机推荐

  1. 聊聊WKWebView

    聊一聊WKWebView 前言 由于之前一直在用UIWebView,所以对于WKWebView只是停留在知道,了解的状态,并未深入的去研究.前天一个项目要求支持iOS8以上,要加入一个web界面.在习 ...

  2. jQuery ajax 请求HttpServlet返回[HTTP/1.1 405 Method not allowed]

    1.问题使用jQuery的ajax请求 Servlet 时,返回没有进入ajax的success回调函数,浏览器控制台显示 [HTTP/1.1 405 Method not allowed]. 2.解 ...

  3. Python 运算符与基本数据类型

    一.运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 二.基本数据类型 1.空(None) 表示该值是一个空对象,空值是Python里一个特殊的值,用None表示 ...

  4. 在thinkphp里面执行原生的sql语句

    在thinkphp里面执行原生的sql语句 怎样在thinkphp里面执行原生的sql语句? $Model = new Model();//或者 $Model = D(); 或者 $Model = M ...

  5. php header utf8 插入header("Content-type: text/html; charset=utf-8");

    PHP文件插入header("Content-type: text/html; charset=utf-8"); 相当于页面里面的<meta http-equiv=" ...

  6. 【运维技术】Nexus私服安装配置常用问题

    maven私服安装配置 软件安装及基本配置 安装配置 # 安装jdk,参考其他教程 mkdir -p /app/nexus2 # 创建目录 wget https://download.sonatype ...

  7. 【翻唱】【你的名字MAD】爱你等于爱自己

    http://video.yingtu.co/0/8f606e9b-9694-4d35-a0bf-730391a3ee12.mp4 [你的名字MAD]爱你等于爱自己-原唱翻唱 http://video ...

  8. 01: socket模块

    网络编程其他篇 目录: 1.1 socket理论部分 1.2 socket处理单个连接 和 同时接受多个连接 1.3 socket实现远程执行命令,下载文件 1.4 通过socket实现简单的ssh ...

  9. shell:遍历目录和子目录的所有文件及匹配文件内容到日志

    过滤文件内网 #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] ...

  10. (转)Spring Boot(一)

    (二期)4.springboot的综合讲解 [课程四]springbo...概念.xmind64.5KB [课程四]spring装配方式.xmind0.2MB [课程四预习]spri...解读.xmi ...