在VBA中使用替代函数运行时1004错误

任何想法为什么这个代码产生运行时1004错误?

Dim outputHtml As String, tbody As String outputHtml = Right(htmlCode, Len(htmlCode) - InStr(htmlCode, "<div class=""b-campaign-stat-data b-campaign-stat-data_type_custom"">") + 1) tbodyStart = InStr(outputHtml, "<tbody") tbodyEnd = InStr(outputHtml, "</tbody>") tbody = Mid(outputHtml, tbodyStart, tbodyEnd + 8 - tbodyStart) outputHtml = WorksheetFunction.Substitute(outputHtml, tbody, "g") 

这是绊倒它的最后一行。 我已经尝试了Application.Substitute以及…

你还没有声明你的tbodyEndvariables。 添加到您的代码:

 Dim tbodyEnd As String 

请注意,您可以启用“Option Explicit”以避免将来出现类似问题。 有两种方法可以做到这一点:

  1. Option Explicit添加到模块的顶部,或者
  2. 转到“工具”>“选项”,然后在“编辑器”选项卡上选中“需要variables声明”。

当你没有声明一个variables的时候,做这两者中的任何一个都会导致更有意义的警告/错误。 它将为您提供variables的名称,这是有帮助的。