'运行时错误1004:对象'_Global'的方法'范围'失败'只有xls文件,但不包含txt或xlsx文件

我有这段代码search一个短语,将范围设置为单元格右边的单元格,包含到最后一个数据列的单元格,并使用条件格式设置单元格。 这段代码对于txt文件和xlsx文件都能正常工作,但是这个短语

Set rngHeaderAs 

得到运行时错误1004:当我在xls文件上运行代码时,对象'_Global'的方法'Range'失败。 查找部分中的短语肯定存在,如果我将xls文件保存为xlsx文件,则代码将完美运行。

代码:

 Sub Color_labreport_horizontal() Cells.Replace What:="n,d.", Replacement:="nd", lookat:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False On Error Resume Next 'As Set rngHeaderAs = Range("A1:ZZ200").Find("As*Arsen*", lookat:=xlPart) 'This string generates the error Set rngAs = Range(rngHeaderAs, rngHeaderAs.End(xlToRight)) AsAddress = rngHeaderAs.Address(False, False) Dim Ul1As As Double Ul1As = 8 Dim Ul2As As Double Ul2As = 20 Dim Ul3As As Double Ul3As = 50 Dim Ul4As As Double Ul4As = 600 Dim Ul5As As Double Ul5As = 1000 With ActiveSheet With rngAs .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & "<" & Ul1As & ")" .FormatConditions(1).Interior.ColorIndex = 33 .FormatConditions(1).Borders.LineStyle = xlContinuous .FormatConditions(1).Borders.Weight = xlThin End With With rngAs .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul1As & ";" & AsAddress & "<" & Ul2As & ")" .FormatConditions(2).Interior.ColorIndex = 4 .FormatConditions(2).Borders.LineStyle = xlContinuous .FormatConditions(2).Borders.Weight = xlThin End With With rngAs .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul2As & ";" & AsAddress & "<" & Ul3As & ")" .FormatConditions(3).Interior.ColorIndex = 6 .FormatConditions(3).Borders.LineStyle = xlContinuous .FormatConditions(3).Borders.Weight = xlThin End With With rngAs .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul3As & ";" & AsAddress & "<" & Ul4As & ")" .FormatConditions(4).Interior.ColorIndex = 45 .FormatConditions(4).Borders.LineStyle = xlContinuous .FormatConditions(4).Borders.Weight = xlThin End With With rngAs .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul4As & ";" & AsAddress & "<" & Ul5As & ")" .FormatConditions(5).Borders.LineStyle = xlContinuous .FormatConditions(5).Borders.Weight = xlThin .FormatConditions(5).Interior.ColorIndex = 3 End With With rngAs .FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(" & AsAddress & ");" & AsAddress & ">=" & Ul5As & ")" .FormatConditions(6).Interior.ColorIndex = 7 .FormatConditions(6).Borders.LineStyle = xlContinuous .FormatConditions(6).Borders.Weight = xlThin End With With rngAs .FormatConditions.Add xlExpression, Formula1:="=LEFT(" & AsAddress & ";1)=""<""" .FormatConditions(7).Interior.ColorIndex = 33 .FormatConditions(7).Borders.LineStyle = xlContinuous .FormatConditions(7).Borders.Weight = xlThin End With With rngAs .FormatConditions.Add xlExpression, Formula1:="=(" & AsAddress & ") = ""nd""" .FormatConditions(8).Interior.ColorIndex = 33 .FormatConditions(8).Borders.LineStyle = xlContinuous .FormatConditions(8).Borders.Weight = xlThin End With End With 

任何想法,为什么只有当我在xls文件上运行时才会发生这种情况?

Ron Rosenfelt在评论中给了我答案。 ZZ200超出了允许的范围,所以我没有将范围定义为A1:ZZ200(或其他一些硬编码范围),而是使用了CurrentRegion:

  Set rngHeaderAs = Range("A1").CurrentRegion.Find("As*Arsen*", lookat:=xlPart) 

以确保我覆盖了我想要的范围而不超出允许的范围。 现在,代码也可以在Excel 97兼容文件中完美运行。