Saturday 8 October 2016

Indent Vba Code:

Easiest way to indent or tweak what to indent with the below code. Below code indents each line of code where ever it finds the key words. Ofcourse the code doesn't indent the code 100%.

codeFile.txt: File contains the Vba code
Result.txt: File will contain the indented code.


Sub IndentVba()
Dim tabChar As Long
tabChar = 0
Open "C:\Users\jack\Desktop\codeFile.txt" For Input As #1
Open "C:\Users\jack\Desktop\Result.txt" For Output As #2

 r = 1
 Do Until EOF(1)
 Line Input #1, a
 If Trim(a) Like "For *" Or Trim(a) Like "Do *" Or Trim(a) Like "While *" Then
 tabChar = tabChar + 1
 Debug.Print Application.WorksheetFunction.Rept(vbTab, tabChar) & Trim(a)
 Print #2, Application.WorksheetFunction.Rept(vbTab, tabChar) & Trim(a)
 ElseIf Trim(a) Like "Next*" Or Trim(a) Like "Loop*" Then
 Debug.Print Application.WorksheetFunction.Rept(vbTab, tabChar) & Trim(a)
 Print #2, Application.WorksheetFunction.Rept(vbTab, tabChar) & Trim(a)
 If tabChar > 0 Then tabChar = tabChar - 1
 ElseIf Trim(a) Like "End*" Then
 tabChar = 0
 Else
 Debug.Print Application.WorksheetFunction.Rept(vbTab, tabChar) & Trim(a)
 Print #2, Application.WorksheetFunction.Rept(vbTab, tabChar) & Trim(a)
 End If
 Loop
 Close #2
 Close #1
End Sub

No comments:

Post a Comment