表选择的问题

时间:2015-02-15 11:19:45

标签: excel vba excel-vba

我正在尝试选择表格的内容(范围X11:Xnn):

                                        KEYWORD2
  X11            X12          ...          X1N
  X21            X22          ...          X2N
  ...            ...          ...          ...
  XN1            XN2          ...          XNN      
KEYWORD1                

No Important Things…            

所以,我想只选择范围X11:XNN搜索2个关键字,然后只选择Xii。

我正在尝试这样做:

Sub Macro3()
Cells.Find(What:="KEYWORD1", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

Selection.Offset(-1, 0).Select 'I don't want the KeyWord1 appears

Cells.Find(What:="KEYWORD2", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

Selection.Offset(1, 0).Select 'I don't want the KeyWord2 appears

Range(??, ??).Select   
End Sub

1 个答案:

答案 0 :(得分:0)

您的主要问题是您没有返回关键字的位置,以便您以后可以使用它们。让我们捕获这些结果,我们可以抵消它们,我们称之为范围。

Sub Macro3()
    Dim rngKeywordOneLocation As Range
    Dim rngKeywordTwoLocation As Range

    Set rngKeywordOneLocation = Cells.Find(What:="KEYWORD1", After:=ActiveCell, LookIn:=xlFormulas _
            , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)

    Set rngKeywordTwoLocation = Cells.Find(What:="KEYWORD2", After:=ActiveCell, LookIn:=xlFormulas _
            , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False)

    Range(rngKeywordOneLocation.Offset(1, 0), rngKeywordTwoLocation.Offset(-1, 0)).Select

End Sub

另外,您应该小心use of .select