20170719xlVBASmartIndent
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的更多相关文章
- Silverlight之控件应用总结(二)(4)
[置顶] Silverlight之控件应用总结(二)(4) 分类: 技术2012-04-03 22:12 846人阅读 评论(0) 收藏 举报 silverlightradiobuttonhyperl ...
随机推荐
- linux常用命令:traceroute 命令
通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...
- 20145316许心远《网络对抗》EXP8 Web基础
实验后回答问题 什么是表单 来自百度百科的官方定义:表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法. ...
- Xcode10.x适配的部分问题
因为我们项目是一个Workspace,由若干个库组成(组件化比较碎),又涉及到海外和国内(存在很多差异性),整个项目的编译是由每个库的脚本(每个库生成会支持32位和64位,每次编译前会清除历史缓存), ...
- UVA756 Biorhythms
UVA756 Biorhythms crt crt裸题 因为模数已知所以有些值能直接求 #include<iostream> #include<cstdio> using na ...
- 使用maven开发MR
1.安装mvn(可以省略,因为eclipse自带maven插件) 1.1. 上传安装包到linux 1.2. 解压maven安装包 mkdir /usr/maven -bin.tar.gz -C /u ...
- QT学习资源
http://www.qter.org/portal.php?mod=view&aid=26
- SPOJ - POLYNOM Polynomial(数论乱搞)题解
题意 :给你n个数,问你是否存在一个多项式(最多三次方)满足f(i)= xi. 思路:讲一个神奇的思路: x3 - (x - 1)3 = 3x2 - 3x + 1 x2 - (x - 1)2 = 2x ...
- 【第十三章】 springboot + lombok
lombok作用:消除模板代码. getter.setter.构造器.toString().equals() 便捷的生成比较复杂的代码,例如一个POJO要转化成构建器模式的形式,只需要一个注解. 注意 ...
- NYOJ 116 士兵杀敌(二)(二叉索引树)
http://acm.nyist.net/JudgeOnline/problem.php?pid=116 题意: 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的 ...
- mvc ---- ajax 提交过来的Json格式如何处理(解析)
前台传过来的不是一个对象,而是一个Json字符串怎么办 ? 如: {","contents":"<p>lsdfjlsdjflsdf</p> ...