2016-05-13 22 views
1

我已搜查,發現了許多有利於例如類似職位的外側細胞搜索範圍Sheet1中針對範圍在Sheet2中複製到Sheet取決於價值

Look for values from sheet A in sheet B, and then do function in corresponding sheet B cellFor each Loop Will Not Work Search for Value On one Sheet and Change Value on another Sheet

雖然這些解決我的目標的某些方面,他們不是那麼做。 我有3張表1-3,我想在A列和B列的Sheet1-2中進行搜索和匹配,如果在B列中找到匹配項或未找到匹配項,則將A列中的值複製到Sheet3中。

這是我迄今使用Office 2016

Public Sub SeekFindCopyTo() 

Dim lastRow1 As Long 
Dim lastRow2 As Long 
Dim tempVal As String 

lastRow1 = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row 
lastRow2 = Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Row 

For sRow = 4 To lastRow1 
    Debug.Print ("sRow is " & sRow) 
    tempVal = Sheets("Sheet1").Cells(sRow, "B").Text 

    For tRow = 4 To lastRow2 
     Debug.Print ("tRow is " & tRow) 
     TestVal = Sheets("Sheet2").Cells(tRow, "B") 
     Operations = Sheets("Sheet2").Cells(tRow, "A") 

     If Sheets("SAP_XMATTERS").Cells(tRow, "B") = tempVal Then 
      Operations = Sheets("Sheet2").Cells(tRow, "A") 
      Debug.Print ("If = True tempVal is " & tempVal) 
      Debug.Print ("If = True TestVal is " & TestVal) 
      Debug.Print ("If = True Operaitons is " & Operations) 
      If Operations = "REMOVE" Then 
       Sheets("Sheet2").Range("A" & tRow).EntireRow.Copy 
       Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Insert xlcutcell 
       'Sheets("Sheet2").Rows(tRow).Delete 
      Else 
       'Sheets("Sheet2").Rows(tRow).Delete 
      End If 
     End If 
    Next tRow 
Next sRow 
End Sub 

代碼工作不夠好,但美中不足的是,我要尋找一個在B中的比賽:表B之間1 & 2,如果比賽我想要檢查A:A中的相鄰單元格,如果它是REMOVE,則將字符串REMOVE刪除,然後將整行復制到sheet3。這裏是我還想知道的問題,如果在整個行復制到sheet3時,如果在Betsheet 2 & 1與相鄰單元格中的字符串PROCESS之間沒有匹配。我可以在不同的潛艇中做任何一種選擇,但不能一次完成。

你的幫助無異,即使它是沿着線的理解:「你不能這樣做」 ;-)

TIA

鮑勃

+0

快速註釋 - 總是用表格限定您的範圍。幾乎在任何地方都可以這樣做,但檢查你的'lastRow1 = ...'和'lastRow2 = ...',在那裏有'Rows.Count',把你想要計算的工作表放在那裏。否則,*** *** lastRow1'和'lastRow2'將使用活動工作表的'Rows.Count'。 (你也可以在'If操作=「刪除」...'循環中)執行此操作。 – BruceWayne

+0

示例數據可能有所幫助。 – findwindow

回答

0

共使用重新寫.Find訣竅。

Sub SeekFindCopy() 
    Dim sourceValue As Variant 
    Dim resultOfSeek As Range 
    Dim targetRange As Range 

    LastRow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row 
    Set targetRange = Sheets("Sheet2").Range("B:B") 

    For sourceRow = 4 To LastRow 
    Debug.Print ("sRow is " & sRow) 

    sourceValue = Sheets("Sheet1").Range("B" & sRow).Value 

    Set resultOfSeek = targetRange.Find(what:=sourceValue, After:=targetRange(1)) 
     'Debug.Print (sourceValue) 
    Operations = Sheets("Sheet1").Cells(sRow, "A") 
    If resultOfSeek Is Nothing Then 
      'Debug.Print ("Operations is " & Operations) 
     If Operations = "PROCESS" Then 
      Sheets("Sheet1").Range("A" & sRow).EntireRow.Copy 
      Sheets("UpLoad").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Insert xlcutcell 
      'Sheets("Sheet1").Rows(tRow).Delete 
     End If 
    Else 
      'Debug.Print ("Operations is " & Operations) 
     If Operations = "REMOVE" Then 
      Sheets("Sheet1").Range("A" & sRow).EntireRow.Copy 
      Sheets("UpLoad").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Insert xlcutcell 
      'Sheets("Sheet1").Rows(tRow).Delete 
     End If 
    End If 
    Next sourceRow 
End Sub