VBA搜索范围

时间:2016-01-22 21:47:48

标签: vba

我试图使用.Find方法搜索一系列单元格,但我似乎无法使其工作。我有单元格A1和A2中的上限和下限数字,只想搜索A列。有没有其他方法可以设置.Range(),以便我可以搜索这些特定的单元格?

 lowNum = Worksheets("Sheet4").Cells(1, 1)
HighNumb = Worksheets("Sheet4").Cells(1, 2)

Row = 2
While Not IsEmpty(Worksheets("Sheet4").Range("A" & Row))
cn = 1
While Not IsEmpty(Worksheets("Sheet4").Cells(Row, cn))
    Set c1 = Worksheets("Sheet2").Range(Cells(lowNum, "A"), Cells(HighNumb,"A")).Find(
        What:=Worksheets("Sheet4").Cells(Row, cn), _
        LookIn:=xlValues)
    If Not c1 Is Nothing Then
        Worksheets("Sheet4").Cells(Row, cn).Delete shift:=xlToLeft
        cn = cn - 1
    End If
    cn = cn + 1
Wend
Row = Row + 1
Wend




End Sub

1 个答案:

答案 0 :(得分:0)

我稍微纠正了你的代码,告诉我它是否有效:

lowNum = Worksheets("Sheet4").Cells(1, 1).Value
HighNumb = Worksheets("Sheet4").Cells(1, 2).Value

LastRow = Worksheets("Sheet4").Cells(Worksheets("Sheet4".Rows.Count, "a").End(xlUp).Row

For Row = 2 To LastRow
    While Not IsEmpty(Worksheets("Sheet4").Range("A" & Row))
        cn = 1
        While Not IsEmpty(Worksheets("Sheet4").Cells(Row, cn))
            Set c1 = Worksheets("Sheet2").Range(Cells(lowNum, "A"), Cells(HighNumb,"A")).Find(What:=Worksheets("Sheet4").Cells(Row, cn).Value, LookIn:=xlValues)
            If Not c1 Is Nothing Then
                Worksheets("Sheet4").Cells(Row, cn).Delete shift:=xlToLeft
                cn = cn - 1
            End If
        cn = cn + 1
        Wend
    Row = Row + 1
    Wend

End Sub