2016-05-11 38 views
1

我試圖比較兩列中的所有單元格,不同工作簿中的每列。 單元格包含文本和數字,如果兩個單元格(每個在不同的工作簿中)不同,我希望其中一個單元格被突出顯示/着色/填充。比較不同工作簿中的2列內的文本

任務:

1.1 - 小區1 =嗨

1.1 - 小區2 =喜

因此,沒有亮點需要在這裏,兩個值相等

1.2 - 小區1 =你好

1.2 - Cell2 = Hellod

這裏需要

突出,兩者的值不等於彼此

注:兩個小區1和小區2是不同的工作簿

這裏是我的代碼至今:

Sub DescriptionDiscrepency() 
  • 設置文件的位置爲對象
  • 目標路徑在我的代碼冗餘,但可能會爲你們

    有用
    Target_Path = "C:\Users\Example.xlsm" 
    
    
    Set Target_Workbook = Workbooks("Example.xlsm") 
    Target_Workbook.Sheets("Sheet1").Unprotect Password:="****" 
    Set Source_Workbook = Workbooks("Example2.xlsm") 
    Source_Workbook.Sheets("Sheet1").Unprotect Password:="*****" 
    
    • 讀取從目標數據文件,看看是否源文件

      Target_Data = Target_Workbook.Sheets("Sheet1").Cells(2, 6).CurrentRegion.Rows.Count 
      Source_Data = Source_Workbook.Sheets("Sheet1`").Cells(5, 2).CurrentRegion.Rows.Count 
      
    • 亮點相匹配,如果CAT描述並不相同我們的狀態跟蹤器

    • 這部分不起作用

      For i = 1 To lastRow 
      For j = 1 To lastRow 
          If Source_Data.Cells(j, 1).Value <> "" Then 
           If StrComp(Source_Data.Cells(j, 2).Value, 
            Target_Data.Cells(i, 6).Value, CompareMethod.Text) = 0 Then 
      
            Source_Data.Cells(j, 2).Interior.ColorIndex = RGB(255, 255, 255) 
            Source_Data.Cells(j, 2).Font.Color = RGB(0, 0, 0) 
      
           Else 
            Source_Data.Cells(j, 2).Interior.ColorIndex = RGB(0, 0, 0) 
            Source_Data.Cells(j, 2).Font.Color = RGB(255, 199, 206) 
           End If 
          End If 
      Next j 
      Next I 
      End Sub 
      

回答

0

你混合了一些東西。即範圍對象(Excel中的實際單元格)和行計數(簡單數字)。

試試這樣說:

Set Target_Data = Target_Workbook.Sheets("Sheet1").Cells(2, 6).CurrentRegion 
Set Source_Data = Source_Workbook.Sheets("Sheet1`").Cells(5, 2).CurrentRegion 

For i = 1 To Target_Data.Rows.Count 
For j = 1 To Source_Data.Rows.Count 
    If Source_Data.Cells(j, 1).Value <> "" Then 
    If StrComp(Source_Data.Cells(j, 2).Value, Target_Data.Cells(i, 6).Value, vbTextCompare) = 0 Then 

     Source_Data.Cells(j, 2).Interior.Color = RGB(255, 255, 255) 
     Source_Data.Cells(j, 2).Font.Color = RGB(0, 0, 0) 

    Else 
     Source_Data.Cells(j, 2).Interior.Color = RGB(0, 0, 0) 
     Source_Data.Cells(j, 2).Font.Color = RGB(255, 199, 206) 
    End If 
    End If 
Next j 
Next I 
End Sub 
+0

嗯,它給我一個錯誤:如果STRCOMP(Source_Data.Cells(J,2).value的,Target_Data.Cells(I,6).value的,CompareMethod .Text)= 0然後,它給我一個對象所需的錯誤 – Theo

+0

我修復它通過使用:如果StrComp(Source_Data.Cells(j,2).Value,Target_Data.Cells(i,6).Value,vbTextCompare)= 0然後,而不是使用CompareMethod.Text – Theo

+0

哦,是的。我想知道Comparemethod.Text在那裏,但是我猜你在類模塊中有什麼東西......我會更新我的答案。 :) – vacip

相關問題