VBE 中的 ReplaceLine 方法仅替换部分行
ReplaceLine method in VBE only replacing part of line
在回答 问题时,我认为编写一个 VBE 宏会很有趣,它会自动替换看起来像
的行
DimAll a, b, c, d As Integer
来自
Dim a As Integer, b As Integer, c As Integer, d As Integer
在我的初稿中,我只想修改选定的一行。在建立了适当的引用以获取 VBE 对象模型(参见 http://www.cpearson.com/excel/vbe.aspx )并进行了一些尝试后,我想出了:
Function ExpandDim(codeLine As String) As String
Dim fragments As Variant
Dim i As Long, n As Long, myType As String
Dim last As Variant
Dim expanded As String
If UCase(codeLine) Like "*DIMALL*AS*" Then
codeLine = Replace(codeLine, "dimall", "Dim", , , vbTextCompare)
fragments = Split(codeLine, ",")
n = UBound(fragments)
last = Split(Trim(fragments(n)))
myType = last(UBound(last))
For i = 0 To n - 1 'excludes last fragment
expanded = expanded & IIf(i = 0, "", ",") & fragments(i) & " As " & myType
Next i
expanded = expanded & IIf(n > 0, ",", "") & fragments(n)
ExpandDim = expanded
Else
ExpandDim = codeLine
End If
End Function
Sub DimAll()
Dim myVBE As VBE
Dim startLine As Long, startCol As Long
Dim endLine As Long, endCol As Long
Dim myLine As String
Set myVBE = Application.VBE
myVBE.ActiveCodePane.GetSelection startLine, startCol, endLine, endCol
myLine = myVBE.ActiveCodePane.CodeModule.Lines(startLine, 1)
Debug.Print ExpandDim(myLine)
myVBE.ActiveCodePane.CodeModule.ReplaceLine startLine, ExpandDim(myLine)
End Sub
在我的另一个代码模块中:
Sub test()
DimAll a, b, c, d As Integer
Debug.Print TypeName(a)
Debug.Print TypeName(b)
Debug.Print TypeName(c)
Debug.Print TypeName(d)
End Sub
这是奇怪的部分。当我突出显示以 DimAll a 开头的行并调用我笨拙命名的子 DimAll 时,立即 window 我看到
Dim a As Integer, b As Integer, c As Integer, d As Integer
符合预期,但在代码模块本身中,该行更改为
Dim a, b, c, d As Integer
DimAll 已替换为 Dim -- 但该行的其余部分未修改。我怀疑逗号混淆了 ReplaceLine 方法。关于如何解决此问题的任何想法?
当我 运行 使用调试器时,myLine
会在两次调用之间更改值。 DimAll
在第二次通过时变为 Dim
。
这是因为在 ExpandDim Function
.
中输入主要 If
条件后,您将替换 codeLine
的值
在该函数中创建一个新变量,您应该没问题...或者传递它 ByVal
,您就可以了:
Function ExpandDim(ByVal codeLine As String) As String
在回答
DimAll a, b, c, d As Integer
来自
Dim a As Integer, b As Integer, c As Integer, d As Integer
在我的初稿中,我只想修改选定的一行。在建立了适当的引用以获取 VBE 对象模型(参见 http://www.cpearson.com/excel/vbe.aspx )并进行了一些尝试后,我想出了:
Function ExpandDim(codeLine As String) As String
Dim fragments As Variant
Dim i As Long, n As Long, myType As String
Dim last As Variant
Dim expanded As String
If UCase(codeLine) Like "*DIMALL*AS*" Then
codeLine = Replace(codeLine, "dimall", "Dim", , , vbTextCompare)
fragments = Split(codeLine, ",")
n = UBound(fragments)
last = Split(Trim(fragments(n)))
myType = last(UBound(last))
For i = 0 To n - 1 'excludes last fragment
expanded = expanded & IIf(i = 0, "", ",") & fragments(i) & " As " & myType
Next i
expanded = expanded & IIf(n > 0, ",", "") & fragments(n)
ExpandDim = expanded
Else
ExpandDim = codeLine
End If
End Function
Sub DimAll()
Dim myVBE As VBE
Dim startLine As Long, startCol As Long
Dim endLine As Long, endCol As Long
Dim myLine As String
Set myVBE = Application.VBE
myVBE.ActiveCodePane.GetSelection startLine, startCol, endLine, endCol
myLine = myVBE.ActiveCodePane.CodeModule.Lines(startLine, 1)
Debug.Print ExpandDim(myLine)
myVBE.ActiveCodePane.CodeModule.ReplaceLine startLine, ExpandDim(myLine)
End Sub
在我的另一个代码模块中:
Sub test()
DimAll a, b, c, d As Integer
Debug.Print TypeName(a)
Debug.Print TypeName(b)
Debug.Print TypeName(c)
Debug.Print TypeName(d)
End Sub
这是奇怪的部分。当我突出显示以 DimAll a 开头的行并调用我笨拙命名的子 DimAll 时,立即 window 我看到
Dim a As Integer, b As Integer, c As Integer, d As Integer
符合预期,但在代码模块本身中,该行更改为
Dim a, b, c, d As Integer
DimAll 已替换为 Dim -- 但该行的其余部分未修改。我怀疑逗号混淆了 ReplaceLine 方法。关于如何解决此问题的任何想法?
当我 运行 使用调试器时,myLine
会在两次调用之间更改值。 DimAll
在第二次通过时变为 Dim
。
这是因为在 ExpandDim Function
.
If
条件后,您将替换 codeLine
的值
在该函数中创建一个新变量,您应该没问题...或者传递它 ByVal
,您就可以了:
Function ExpandDim(ByVal codeLine As String) As String