在不同的工作表中查找值

时间:2017-07-12 09:32:11

标签: excel vba excel-vba for-loop if-statement

我尝试编写一些代码,例如,选择工作表2中单元格A2中的值,将其与工作表1中的范围A:A进行比较,如果找到相同的值,则从Sheet 1中的该行复制一些值并将它们粘贴到Sheet 2中。然后它查看Sheet 2中的单元格A3并重复该过程。然后是表2中的单元格A4等。

我从这里失去了。有什么想法吗?

Sub Last_Week()

Dim Sheet2 As Worksheet
Dim Sheet1 As Worksheet

Set Sheet2 = ActiveSheet
Set Sheet1 = ActiveSheet.Previous

Sheet1.Activate

Dim i As Integer
For i = 1 To 1500
    Columns("A:A").Select
    Set cell = Selection.Find(What:=(ACTIVATE WS AND SELECT THE VALUE IN COLUMN A AND ROW I)

    If cell Is Nothing Then
'do it something

    Else
'do it another thing
    End If

Next i

End Sub

1 个答案:

答案 0 :(得分:1)

试试这个:

Sub Last_Week()


Dim Sheet1 As Worksheet
Set Sheet1 = Worksheets("Sheet1")

Dim Sheet2 As Worksheet
Set Sheet2 = Worksheets("Sheet2")

Dim i As Integer
For i = 1 To 1500
    'search cell A & i from Sheet2 in column A in Sheet1
    Set cell = Sheet1.Columns("A:A").Find(Sheet2.Cells(i, 1).Value, LookIn:=xlValues)

    If cell Is Nothing Then
    'do it something
    Else
    'do it another thing
    End If

Next i

End Sub