2012-04-06 50 views
2

我想比較兩個不同工作表中2列的值,如果值相等,則第一個工作表中相應的單元格將在相應的單元格中取相同的值在第二張紙上。所以我使用了一個循環函數,但問題是我無法在第一個工作表中的函數中定義和使用第二個工作表。在存儲在工作表1中的VB函數中使用工作表2

我會很感激指導我解決這個問題。

你會發現以下的功能我想使用:

Sub Test_V01() 
    Dim i As Integer 
    Dim j As Integer 

    Dim Bouira As Excel.Worksheet 

    For j = 3 To 100 
     For i = 3 To 120 
      If Cells(j, 3) = Bouira!Cells(i, 30) Then 
       Cells(j, 12) = Bouira!Cells(i, 31) 
      End If 
     Next i     
    Next j 

End Sub 

回答

2
  1. 分別替換Cells(j, 3)Cells(j, 12)Me.Cells(j, 3)Me.Cells(j, 12),以確保始終使用該片中的細胞,而不是細胞來自活動工作表。

  2. Bouira!Cells替換爲Bouira.Cells。在表中沒有收集其元素的文字值爲Cells

    Set Bouira = ThisWorkbook.Worksheets("Sheet name") 
    
+0

非常感謝,因爲我有自己的想法 – user1317100 2012-04-06 11:31:58

+0

+1進入循環前

  • 分配參考Bouira努力實際解釋它! – 2012-04-06 12:26:55

  • 1

    這將設置引用到工作表Bouira

    Sub Test_V01() 
        Dim i As Integer, j As Integer, Bouira As Worksheet 
    
        Set Bouira = Worksheets("Bouira") 
    
        For j = 3 To 100 
         For i = 3 To 120 
          If Cells(j, 3) = Bouira.Cells(i, 30) Then 
           Cells(j, 12) = Bouira.Cells(i, 31) 
          End If 
         Next i 
        Next j 
    
    End Sub 
    
    +0

    非常感謝您的幫助,它完美運作 – user1317100 2012-04-06 10:16:16

    相關問題