mattintosh note

どこかのエンジニアモドキの備忘録

Excel でシートを比較する

「Sheet1」と「Sheet2」を比較して差異によって背景色を変更するマクロ(かなり適当な)。シート比較くらい標準機能で用意しておいてくれればいいのに…。

sub SheetDiff()
  dim max_row, max_col
  max_row = 1024
  max_col = 256

  dim s1, s2
  set s1 = worksheets("sheet1")
  set s2 = worksheets("sheet2")
  

  dim current_address
  with s2
    current_address = selection.address
    cells.select
    selection.interior.colorindex = 0
    range(current_address).select
  end with

  dim x, y, ci
  for x = 1 to max_row
    for y = 1 to max_col
      if strcomp(s1.cells(x, y).value, s2.cells(x, y).value) then
        ci = 35
      else
        ci = 3
      end if
      s2.cells(x, y).interior.colorindex = ci
    next
  next

end sub