Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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
Vba 在特定工作表中搜索文本字符串_Vba_Excel - Fatal编程技术网

Vba 在特定工作表中搜索文本字符串

Vba 在特定工作表中搜索文本字符串,vba,excel,Vba,Excel,我正在尝试让excel在特定工作表中的特定列中搜索文本字符串,该工作表不是活动工作表。VBA给了我一个错误,说明我不能使用这种选择方法。所以我的问题是,你有没有其他的建议 Worksheets("Parts for renovation").Columns("Q:Q").Select Set cell = Selection.Find(What:="Total transfer price", After:=ActiveCell, LookIn:=xlFormulas, _ Lo

我正在尝试让excel在特定工作表中的特定列中搜索文本字符串,该工作表不是活动工作表。VBA给了我一个错误,说明我不能使用这种选择方法。所以我的问题是,你有没有其他的建议

Worksheets("Parts for renovation").Columns("Q:Q").Select
Set cell = Selection.Find(What:="Total transfer price", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

If cell Is Nothing Then

    Exit Sub

Else

    Worksheets("Internal").Cells(29, 4) = Worksheets("Parts for Renovation").ActiveCell.Offset(0, 4)

End If

无需在此处选择任何内容:

With Worksheets("Parts for renovation").Columns("Q:Q")
    Set cell = .Find(What:="Total transfer price", After:=.Cells(1), LookIn:=xlFormulas, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)    
    If cell Is Nothing Then    
        Exit Sub
    Else
        Worksheets("Internal").Cells(29, 4) = Cell.Offset(0, 4)
    End If
End With

出现错误是因为您在非活动工作表上选择了一个范围。这就是为什么您通常应该避免选择的原因之一

但是,如果您想使代码工作(这是非常不可取的),您可以考虑在选择范围之前选择工作表:

Worksheets("Parts for renovation").Select
Columns("Q:Q").Select
对于建议的部分,尽量避免使用“选择”-

你打字太快了,忘记了以结尾的
;)@谢拉多,谢谢!