|
楼主 |
发表于 2009-8-12 18:00
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
'转换代码到 HTML
'撰写:老朽
'网址:http://Club.ExcelHome.net
'日期:2009-8-12 下午 06:00:09
Private Sub ConvertCode(ByRef Txt, ByVal Procedure As Boolean )
Dim Rng As Range , Ar As Variant , i As Long , lStart As Long
Dim lEnd As Long , Tmp As String , J As Long
Dim Tmp2 As String , CountCont As Long
With Sheet1
Set Rng = .Range("A2").Offset(, Abs(Procedure))
Set Rng = .Range(Rng, .Range("A65536").Offset(, Abs(Procedure)).End(xlUp))
Ar = Rng.Value
Set Rng = Nothing
End With
i = 1
Txt = Application.Substitute(Txt, " _" & vbNewLine, Chr(35) & Chr(35) & Chr(64) & Chr(64) & Chr(37) & Chr(37))
'Chr(35) & Chr(35) & Chr(64) & Chr(64) & Chr(37) & Chr(37)= "#" & "#" & "@" & "@" & "%" & "%"
'Chr$(39):注解关键字 '
'Rem 也是关键字
'判断是否为注解并将注解标注为绿色
If InStr(1, Txt, Chr$(39), 1) > 0 Or InStr(1, Txt, "Rem ", 1) > 0 Then
While i > 0
If InStr(1, Txt, "Rem ", 1) > 0 Then
lStart = InStr(i, Txt, "Rem ", 0)
Else
lStart = InStr(i, Txt, Chr$(39), 0)
End If
If lStart > 0 Then
If Not IsString(Txt, lStart) Then
If InStr(lStart, Txt, vbNewLine, 0) = 0 Then
lEnd = Len(Txt)
Else
lEnd = InStr(lStart, Txt, vbNewLine, 0) - 1
End If
Tmp = Mid$(Txt, lStart, lEnd - lStart + 1)
Txt = Application.Replace(Txt, lStart, Len(Tmp), HTMLColor(GREEN, Tmp))
i = InStr(lStart + Len(HTMLColor(GREEN, Tmp)) - Len(Tmp) + 1, Txt, vbNewLine, 0)
Else
i = lStart + 1
End If
Else
i = 0
End If
Wend
End If
For i = LBound (Ar) To UBound (Ar) '关键字为蓝色
On Error GoTo err_h
If InStr(1, Txt, Ar(i, 1), 1) > 0 Then
Tmp = CStr (Ar(i, 1))
lStart = 1
For J = 1 To (Len(Txt) - Len(Application.Substitute(Txt, Tmp, ""))) / Len(Tmp)
If lStart > 0 Then lStart = InStr(lStart, Txt, Tmp, 0)
If lStart > 0 Then
If Not LineHasComment(Txt, lStart) And _
IsWordComplete(Mid$(Txt, lStart - 1 + Abs(lStart = 1), _
Len(Tmp) + 2 - Abs(lStart = 1)), Tmp) And _
Not IsString(Txt, lStart) Then
Txt = Application.Substitute(Txt, Tmp, HTMLColor(RED, Tmp), J)
End If
lStart = InStr(lStart + Len(HTMLColor(RED, Tmp)) - Len(Tmp) + 1, Txt, Tmp, 0)
End If
Next J
End If
Next i
Txt = Application.Substitute(Txt, Chr(35) & Chr(35) & Chr(64) & Chr(64) & Chr(37) & Chr(37), " _" & vbNewLine)
'Chr(35) & Chr(35) & Chr(64) & Chr(64) & Chr(37) & Chr(37)= "#" & "#" & "@" & "@" & "%" & "%"
Txt = Application.Substitute(Txt, " ", " ")
Txt = Application.Substitute(Txt, vbNewLine, "<" & "br" & ">")
Exit Sub
err_h:
End Sub |
|