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 ...
随机推荐
- uva11383 转化为 二分图匹配
给定一个n*n矩阵,每个格子里都有一个正整数w(i,j).你的任务是给每行确定一个整数row(i),没列也确定一个正整数col(i),使得对于任意格子(i,j),w(i,j) <= row(i) ...
- 关于在ASP.NET中使用JavaScript的建议
一个很恼人的情况,就是当你使用JS在一个ASP,NET应用程序中引用一个在模板页初始化的服务器控件的时候: 比如,我们在模板页有一个TextBox的服务器控件,而且我们想要去获取他的Text:如果你使 ...
- Zookeeper使用实例——分布式共享锁
前一讲中我们知道,Zookeeper通过维护一个分布式目录数据结构,实现分布式协调服务.本文主要介绍利用Zookeeper有序目录的创建和删除,实现分布式共享锁. 举个例子,性能管理系统中,告警规则只 ...
- VMWare中桥接、NAT、Host-only
1.概述 2.bridged(桥接模式) 3.NAT(网络地址转换模式) 4.host-only(主机模式) 5.replicate physical network connection state ...
- DBus学习网站
http://blog.csdn.net/thonrbirdxb/article/details/11482007 DBus的基本资料可以参考 DBus学习笔记(博客园) http://dotnet. ...
- [转载]LinkButton跳转页面及传递参数
在DataList中使用LinkButton按钮(LinkButtonDelete),该按钮用于链接跳转到删除页面.在模板中双击该按钮,跳转到.cs页面.问题是我们如何获得该条信息的ID,如果不知道I ...
- Js中String转int
Js中String转int 方案一代码: Number(str) 方案二代码: //parseInt 方法都有两个参数, 第一个参数就是要转换的对象, 第二个参数是进制基数, 可以是 2, 8, 10 ...
- android 实践项目三
android 实践项目三 本周我主要完成的任务是将代码进行整合,然后实现百度地图的定位与搜索功能.在这次实现的 图形界面如下: 在本周的工作中主要的实现出来定位与收索的功能,在地图中能实现了定位,显 ...
- linux内核分析 第一周
计算机是如何工作的 冯·诺依曼理论的要点是: 数字计算机的数制采用二进制:计算机应该按照程序顺序执行. 冯·诺依曼体系结构 根据冯·诺依曼体系结构构成的计算机,必须具有如下功能:把需要的程序和数据送至 ...
- Android移植学习笔记
1.一头雾水不知道用什么编译环境,不知道用什么下载软件 编译软件:Ubuntu12.04(编译主机系统),JDK(Java) 下载软件: 模拟器: qemu