返回列表 上一主題 發帖

[發問] 比對和標示 內容

  1. Private Sub Workbook_Open()
  2. Dim sh, fs$, s As Worksheet, mystr$, a(), C As Range
  3. Set d = CreateObject("Scripting.Dictionary")
  4. fs = ThisWorkbook.Path & "\source.xls" 'source.xls目錄與mapping.xls相同
  5. sh = Array("2.2", "3.0")
  6. With Workbooks.Open(fs)
  7.    For Each s In .Sheets(sh)
  8.       With s
  9.         a = .Range(.[A1], .[A65536].End(xlUp))
  10.         For i = 1 To UBound(a)
  11.            mystr = .Name & Right(a(i, 1), 4)
  12.            d(mystr) = d.Count
  13.         Next
  14.       End With
  15.    Next
  16. .Close
  17. End With
  18. With Me
  19.    For Each s In .Sheets(sh)
  20.    s.UsedRange.Interior.ColorIndex = -4142
  21.       For Each C In s.UsedRange
  22.          If d.exists(s.Name & C) Then C.Interior.ColorIndex = 6
  23.       Next
  24.    Next
  25. End With
  26. End Sub
複製代碼
學海無涯_不恥下問

TOP

fs$是將變數宣告為字串型態,這裡是用來當作source.xls的完整目錄字串
sh沒有宣告任何型態,所以被默認宣告為Variant
正確寫法應為Dim sh As Variant
而sh = Array("2.2", "3.0")
就指定sh的內容為一維陣列,其內容是2個數值型的字串,此處就是你的來源檔案要比對的2個工作表名稱
a()就是宣告一個陣列變數a,此陣列內容
a = .Range(.[A1], .[A65536].End(xlUp))
就是source.xls的sheets("2.2")及sheets("3.0")工作表內A欄的資料範圍的值
學海無涯_不恥下問

TOP

        靜思自在 : 一個人不怕錯,就怕不改過,改過並不難。
返回列表 上一主題