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 ...
随机推荐
- Java设计模式应用——工厂模式
工厂模式有三种:简单工厂.工厂方法.抽象工厂 一. 抽象工厂 1. 一个可以生产多种产品的工厂: 2. 不改变工厂无法生产新的产品. package com.coshaho.learn.factory ...
- Rapid 2D-to-3D conversion——快速2D到3D转换
https://blog.csdn.net/qq_33445835/article/details/80143598 目前想做一个关于2D转3D的项目,由于国内资料比较少而且大部分都是基于国外的研究 ...
- CentOS7防火墙之firewalld
今天在centos7上装mysql8,装好了之后发现主机的navicat始终连不上centos中的mysql 搜索发现是防火墙的问题,已查看iptables,嗯?没有了这个防火墙,原来centos换防 ...
- Unable to load the Wrapper's native library because none of the following files及解决方法
在有几个应用中,在启动的时候发现下列警告: The version of the script (3.5.29) doesn't match the version of this Wrapper ( ...
- MIME协议(详解范例)
转载一:http://blog.csdn.net/bripengandre/article/details/2192982 转载二:http://blog.csdn.net/flfna/article ...
- Java序列化流-ObjectOutputStream、ObjectInputStream
Java对象流的基本概念: 实例代码: 实体类User: import java.io.Serializable; /** * @author zsh * @company wlgzs * @crea ...
- 垒骰子|2015年蓝桥杯B组题解析第九题-fishers
垒骰子 赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体. 经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥! 我们先来规范一下骰子:1 的 ...
- Harmonic Number (调和级数+欧拉常数)题解
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
- json字符窜转对象
第一种方法: 注意引用:using System.Runtime.Serialization.Json; using System.IO; static void Main(string[] args ...
- 入门dp总结
写这篇博文主要是为了归纳总结一下dp的有关问题(不定期更新,暑假应该会更的快一些) 会大概讲一下思路,不会事无巨细地讲 另一篇是平时做过的一些dp题,这篇博客里面提到的题都有题解放在那边:https: ...