VBAmacros(Excel 2010):运行时错误5

我已经做了一个小macros来删除一长串报告中的几个不需要的字符。 我想从string中删除HTML标签,并遇到意外的运行时错误。

我一直有这个奇怪的运行时错误:

strMatter = Mid(str2010, 1, Len(str2010) - 10) 

鉴于类似的事情工作:

 b = Mid(a, 1, Len(a) - 10) 

不同的是,上面的行在“For To Loop”中运行。 按照逻辑,我已经尝试了几件事来解决这个问题,似乎我不允许在循环中减less一个variables。 我该如何解决这个问题?

任何macros的运行前的B列: strPlaceholder

macros之后的B列已经运行: str2010

每一个帮助和/或build议都比欢迎!

编辑:

我已经更改For intRow = 2 To 5000

为了For intRow = 2 To 13

一切正常。 我如何改变脚本以find最后一行?

这是整个小代码:

  Sub WHYYYY() Dim strPlaceholder As String Dim strPhrase As String Dim str2010 As String Dim strMatter As String Dim strStart As String Dim strEnd As String Dim intRow As Integer Dim intChar As Integer Dim intLen As Integer For intRow = 2 To 5000 'Clean Description Column strPlaceholder = Cells(intRow, 2).Value If Not Trim(Len(strPlaceholder)) = 0 Then strPhrase = strPlaceholder str2010 = Mid(strPhrase, 63, Len(strPhrase)) strMatter = Mid(str2010, 1, Len(str2010) - 10) Cells(intRow, 2) = str2010 'Should be = strMatter End If 'Clean Legal Responsible Column strStart = Cells(intRow, 7).Value intLen = Len(strStart) For intChar = 1 To intLen If Not IsNumeric(Mid(strStart, intChar, 1)) Then strEnd = strEnd & Mid(strStart, intChar, 1) End If Cells(intRow, 7) = strEnd Next Cells(intRow, 7) = Mid(strEnd, 3, 50) strEnd = "" 'Clean Business Responsible Column strStart = Cells(intRow, 8).Value intLen = Len(strStart) For intChar = 1 To intLen If Not IsNumeric(Mid(strStart, intChar, 1)) Then strEnd = strEnd & Mid(strStart, intChar, 1) End If Cells(intRow, 8) = strEnd Next Cells(intRow, 8) = Mid(strEnd, 3, 50) strEnd = "" Next End Sub 

我不明白这个问题是否仍然是问题中的问题,


我一直有这个奇怪的运行时错误:

 strMatter = Mid(str2010, 1, Len(str2010) - 10) 

鉴于类似的事情工作:

 b = Mid(a, 1, Len(a) - 10) 

如果它仍然是那么你可以改变如下“战略”

 Dim cell As Range With Worksheets("MySheet") '<~~ replace "MySheet" with your actual sheet name With Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp)).SpecialCells(xlCellTypeConstants, xlTextValues) .Replace What:="*><p>", Replacement:="", LookAt:=xlPart, MatchCase:=False .Replace What:="</p></div>*", Replacement:="", LookAt:=xlPart, MatchCase:=False For Each cell In .Cells ' do other stuff Next cell End With End With