Excel ';运行时错误1004:方法';范围';对象的'_全球';失败';仅适用于xls文件,但不适用于txt或xlsx文件

Excel ';运行时错误1004:方法';范围';对象的'_全球';失败';仅适用于xls文件,但不适用于txt或xlsx文件,excel,find,range,vba,Excel,Find,Range,Vba,我有一段代码,它搜索短语,将范围设置为包含短语的单元格右侧的单元格,一直到最后一个数据列,并使用条件格式设置单元格格式。此代码适用于txt文件和xlsx文件,但短语 Set rngHeaderAs 获取运行时错误1004:在xls文件上运行代码时,对象“\u Global”的方法“Range”失败。“查找”部分中的短语肯定存在,如果我将完全相同的xls文件保存为xlsx文件,代码将运行得非常完美 守则: Sub Color_labreport_horizontal() Cells.Repla

我有一段代码,它搜索短语,将范围设置为包含短语的单元格右侧的单元格,一直到最后一个数据列,并使用条件格式设置单元格格式。此代码适用于txt文件和xlsx文件,但短语

Set rngHeaderAs
获取运行时错误1004:在xls文件上运行代码时,对象“\u Global”的方法“Range”失败。“查找”部分中的短语肯定存在,如果我将完全相同的xls文件保存为xlsx文件,代码将运行得非常完美

守则:

Sub Color_labreport_horizontal()

Cells.Replace What:="n,d.", Replacement:="n.d.", 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 & ") = ""n.d."""
        .FormatConditions(8).Interior.ColorIndex = 33
        .FormatConditions(8).Borders.LineStyle = xlContinuous
        .FormatConditions(8).Borders.Weight = xlThin
    End With
End With
Sub-Color\u labreport\u horizontal()
单元格。替换内容:=“n,d.”,替换:=“n.d.”,查看:=xlPart_
SearchOrder:=xlByRows,MatchCase:=False,SearchFormat:=False_
ReplaceFormat:=False
出错时继续下一步
“因为
设置rngHeaderAs=Range(“A1:ZZ200”)。查找(“As*Arsen*,lookat:=xlPart)”此字符串生成错误
设置rngAs=范围(rngHeaderAs,rngHeaderAs.End(xlToRight))
AsAddress=rngHeaderAs.Address(False,False)
暗淡的Ul1As为双色
Ul1As=8
暗淡的Ul2As为双色
Ul2As=20
暗淡的Ul3As为双色
Ul3As=50
将Ul4As调暗为双精度
Ul4As=600
双色Ul5As
Ul5As=1000
使用ActiveSheet
使用rngAs
.FormatConditions.Add Xexpression,Formula1:=“=和(ISNUMBER(“&AsAddress&”)”&AsAddress&“=”&Ul1As&“;”&AsAddress&“=”&Ul2As&“;”&AsAddress&“=”&Ul3As&“;&AsAddress&“=”&Ul4As&“;&AsAddress&“&Ul5As&”)
.FormatConditions(6).Interior.ColorIndex=7
.FormatConditions(6).Borders.LineStyle=xlContinuous
.FormatConditions(6).Borders.Weight=xlThin
以
使用rngAs

.FormatConditions.Add Xexpression,Formula1:=“=LEFT(&AsAddress&“1)=”Ron Rosenfelt在注释中给了我答案。ZZ200超出了允许的范围,因此我使用CurrentRegion,而不是将范围定义为A1:ZZ200(或其他硬编码的范围):

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

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

您的.xls文件是否与Excel 97兼容?如果是,则范围引用在工作表之外(列只上升到
VI
,您引用列
ZZ
谢谢,Ron。我希望这是解决方案,所以我将ZZ200改为“VI200”,但我仍然得到了错误。当我输入“BZ20”时,它起了作用。显然,我只需要找到允许的最高行/列。或者更好的是:(A1”)。CurrentRegion