简体   繁体   English

Excel VBA-如何解决搜索错误

[英]Excel VBA - how to account for errors from searching

Sheets("Table").Select
Cells.Find(What:="Cat", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Offset(1, 1).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(rowoffset:=0, columnoffset:=-1).Select
Selection.Resize(Selection.Rows.Count + 0, Selection.Columns.Count + 10).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("Table").Select
Cells.Find(What:="Bat", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Offset(1, 1).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(rowoffset:=0, columnoffset:=-1).Select
Selection.Resize(Selection.Rows.Count + 0, Selection.Columns.Count + 10).Select
Selection.Copy
Sheets("Sheet1").Select
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

I'm very new to excel and trying to account for errors that will occur when one of the words I'm searching for are not on the table. 我对excel非常陌生,它试图解决当我要搜索的单词之一不在桌子上时将发生的错误。 I'm not sure how to format it to work, but basically for the first search, if it errors then go to the next search without doing any of selection, copy, and paste part (same for the second search). 我不确定如何格式化它,但是基本上对于第一个搜索,如果它出错,则直接进行下一个搜索,而不进行选择,复制和粘贴(与第二个搜索相同)。

Create a Range variable to assign your find function to, then use an If statement to determine if it exists or not. 创建一个Range变量来分配您的find函数,然后使用If语句确定它是否存在。 If no, move to the next one. 如果否,请移至下一个。

Dim fRange As Range

Sheets("Table").Select
Set fRange = Cells.Find(What:="Cat", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Offset(1, 1)

If Not fRange Is Nothing Then

fRange.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(rowoffset:=0, columnoffset:=-1).Select
Selection.Resize(Selection.Rows.Count + 0, Selection.Columns.Count + 10).Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

End If


Sheets("Table").Select
Set fRange = Cells.Find(What:="Bat", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Offset(1, 1)

If Not fRange Is Nothing Then

fRange.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Offset(rowoffset:=0, columnoffset:=-1).Select
Selection.Resize(Selection.Rows.Count + 0, Selection.Columns.Count + 10).Select
Selection.Copy
Sheets("Sheet1").Select
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM