在stringparsing器中编译由vba'Left'修剪函数引起的错误

我试图创build一个VBA函数来逐行parsingstring文本到Excel表中。 我希望函数只打印每行中两个字符之间的子string。 例如,html标签。 在debugging时, Right()函数做我想要的。 但是Left()函数甚至不能编译。 错误消息是非常小的帮助,“无效的过程调用或参数”。 到底是怎么回事? 我根本没有改变那行代码,它只是在工作。

解释什么即时通讯尝试完成,可以说我正在parsingHTML文本。 我只想查看“<”和“>”标签内的文字。 但我也试图编码,所以我可以用它来parsing任何两个所需的字符之间的子string。

所以这样的文字:

 <html> <head> </head> <body> </body> </html> 

将打印:

 html head /head body /body /html 

这只是工作。 不知道为什么我得到这个错误。 仅用于Left()修剪。

 Sub ParseTextFromFile() Dim myFileName As String Dim myLine As String Dim FileNum As Long Dim n As Integer n = 1 myFileName = "C:\Users\user\Desktop\myfile.TXT" FileNum = FreeFile Close FileNum Open myFileName For Input As FileNum Do While Not EOF(FileNum) Line Input #FileNum, myLine 'Debug.Print TrimString(CStr(myLine)) Cells(n, 1).Value = TrimString(CStr(myLine)) n = n + 1 Loop MsgBox "" & n - 1 & " items added." End Sub 'parses a string between two chars/string key values Function TrimString(s As String) As String Dim indexOfKey As Integer Dim firstTrim As String Dim secondTrim As String Dim textLength As Integer Dim key1 As String Dim key2 As String firstTrim = "" secondTrim = "" key1 = CStr(Cells(5, 8)) 'H5 = "<" key2 = CStr(Cells(6, 8)) 'H6 = ">" 'remove everything to the left of the first appearance of the key char/string indexOfKey = InStr(1, s, key1) firstTrim = Right(s, Len(s) - indexOfKey) 'remove everything to the right of the second appearance of the key char/string indexOfKey = InStr(1, firstTrim, key2) secondTrim = Left(firstTrim, indexOfKey - 1) 'this line is throwing the error TrimString = secondTrim End Function