2014-01-08 65 views
0

我主要想找到工作簿A中的單元格在工作簿B中的哪個單元格。工作簿B中每個工作表單元格都是紅色的,所以找到的單元格應該變得沒有填充(如此代碼的底部所示)。問題在於我的第二個循環,我不太確定如何使用工作簿A中複製的單元格搜索工作簿B,或者如果有更高效的方法來完成此操作。如何從一個工作表中的不同工作表中查找行?

Sub Macro7() 
Dim jcolumn As Range 
wsht = InputBox("Enter Name of bucket: ") 
Workbooks("A").Worksheets(wsht).Activate 
    For Each cell In Columns("J:J") 
     cell.Copy 
     Workbooks("B").Worksheets(wsht).Activate 
     Columns("J:J").Select 
     For Each icell In Columns("J:J") 
       jcolumn.Find(cell).Select 
       With Selection.Interior 
       .Pattern = xlNone 
       .TintAndShade = 0 
       .PatternTintAndShade = 0 
       End With 
     Next 
    Next 

End Sub 

回答

0

這是我會怎麼做:

Sub YourRoutine() 

Application.ScreenUpdating = False 

Dim wb1 As Workbook, wb2 As Workbook 
Dim ws1 As Worksheet, ws2 As Worksheet 
Dim myFileName As String 

myFileName = "C:\Users\Bernard\Desktop\test.xlsx" 'Put your full extension & filename here 


Set wb1 = ThisWorkbook 
Set ws1 = ActiveSheet 
Set wb2 = Workbooks.Open(myFileName) 
Set ws2 = wb2.ActiveSheet 

'Set-up your parameters 
yourCol1 = 1 'Change 1 to the column in which your data is located in "ws1" 
yourCol2 = 1 'Change 1 to the column in which your data is located in "ws1" 
firstWs1 = 1 'Change 1 to the row in which the first data is located in "ws1" 
firstWs2 = 1 'Change 1 to the row in which the first data is located in "ws2" 

'Locate last row of ws1 
wb1.Activate 
ws1.Activate 
lastWs1 = Cells(Rows.Count, yourCol1).End(xlUp).Row 

'Locate last row of ws2 
wb2.Activate 
ws2.Activate 
lastWs2 = Cells(Rows.Count, yourCol2).End(xlUp).Row 

'Loop through all the rows of ws1 
For x = firstWs1 To lastWs1 
    wb1.Activate 
    myData1 = Cells(x, yourCol1) 

    'Loop through all the rows of ws2 
    For y = firstWs2 To lastWs2 
     wb2.Activate 
     myData2 = Cells(y, yourCol2) 

     If myData1 = myData2 Then 

      Cells(y, yourCol2).Interior.Color = xlColorIndexNone 

     End If 

    Next y 

Next x 

Application.ScreenUpdating = True 

End Sub 

編輯:編輯我的代碼了一下,因爲它並沒有進行測試,也沒有工作。現在它已經過測試和工作。

相關問題