Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA范围。查找方法错误处理_Vba_Excel - Fatal编程技术网

Excel VBA范围。查找方法错误处理

Excel VBA范围。查找方法错误处理,vba,excel,Vba,Excel,我正在使用range.find方法定位第一行中具有特定值的单元格。当存在匹配时,代码工作正常。当没有匹配时,代码抛出错误,如下面的屏幕截图所示 这种方法需要错误处理是正常的吗?我以为它只会返回Null或者什么都不返回。谢谢你的帮助 使用这个。若您直接想要返回空范围的列号,它将显示错误 Sub findtest() dim c as long dim rng as range Set rng = ActiveSheet.Rows("1:1").Find(What:="John Smith"

我正在使用range.find方法定位第一行中具有特定值的单元格。当存在匹配时,代码工作正常。当没有匹配时,代码抛出错误,如下面的屏幕截图所示

这种方法需要错误处理是正常的吗?我以为它只会返回Null或者什么都不返回。谢谢你的帮助


使用这个。若您直接想要返回空范围的列号,它将显示错误

Sub findtest() 
dim c as long
dim rng as range
   Set rng = ActiveSheet.Rows("1:1").Find(What:="John Smith", _
        After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByColumns, _
        SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    If Not rng Is Nothing Then c = rng.column : Debug.Print c
end sub

1) 请张贴您的代码,而不是它的图片。2) 它不会返回任何内容。3) 分配对象变量时需要使用Set
Set rg=…
非常感谢!新手问题-如何确定哪些是对象变量并需要“set”命令?再次感谢!还有,为什么它在找到匹配项时没有抛出错误?再次感谢!谷歌。对象是工作簿、工作表、范围、图表等。因此,无论是否找到值,您的代码都会出错。在我发布答案时,您更改了问题。我认为对象中不包括数字或字符串变量,包括布尔值(0,1)。