○○○○○○○○○○○○○○○○○
Sub aaa()
'●判定結果の文字と色の設定
Dim markOK As String: markOK = "○"
Dim markNG As String: markNG = "×"
Dim colorNG As String: colorNG = "&H00FFFF"
Dim colorMidasi As String: colorMidasi = "&HFFE4B5"
Dim colorHantei As String: colorHantei = "&HFFB6FF"
'●シート指定
Dim compSheet1 As Worksheet: Set compSheet1 = Worksheets(1) '比較元
Dim compSheet2 As Worksheet: Set compSheet2 = Worksheets(2) '比較先
Dim hanteiSheet As Worksheet: Set hanteiSheet = Worksheets(3) 'セルの比較結果
Dim saiSheet As Worksheet: Set saiSheet = Worksheets(4) 'NGセルの比較文字
Dim row As Integer: row = 1
Dim col As Integer: col = 1
If Worksheets.Count < 5 Then
Worksheets.Add after:=Worksheets(Worksheets.Count)
End If
'シートのクリア
hanteiSheet.Cells.Clear
saiSheet.Cells.Clear
saiSheet.Cells(1, 4).Value = "項目名"
saiSheet.Cells(1, 5).Value = "テキスト"
saiSheet.Cells(1, 7).Value = "値"
'シート内の比較範囲
MaxRow = compSheet1.Range("A1").SpecialCells(xlLastCell).row
MaxRow2 = compSheet2.Range("A1").SpecialCells(xlLastCell).row
If MaxRow < MaxRow2 Then
MaxRow = MaxRow2
End If
MaxCol = compSheet1.Range("A1").SpecialCells(xlLastCell).Column
MaxCol2 = compSheet2.Range("A1").SpecialCells(xlLastCell).Column
If MaxCol < MaxCol2 Then
MaxCol = MaxCol2
End If
'比較処理
Dim HanteiRow As Integer: HanteiRow = 2
For col = 1 To MaxCol
For row = 1 To MaxRow
comp1 = compSheet1.Cells(row, col).Text
comp2 = compSheet2.Cells(row, col).Text
comp1v = compSheet1.Cells(row, col).Value
comp2v = compSheet2.Cells(row, col).Value
If comp1 <> comp2 Then
keka = markNG
compSheet1.Cells(row, col).Interior.Color = colorNG
compSheet2.Cells(row, col).Interior.Color = colorNG
With saiSheet.Cells(HanteiRow, 1)
.Hyperlinks.Add Anchor:=saiSheet.Cells(HanteiRow, 1), _
Address:="##'" & compSheet1.Name & "'!R" & row & "C" & col
.Value = "link"
.Interior.Color = colorMidasi
End With
With saiSheet.Cells(HanteiRow, 2)
.Hyperlinks.Add Anchor:=saiSheet.Cells(HanteiRow, 2), _
Address:="##'" & compSheet2.Name & "'!R" & row & "C" & col
.Value = "link"
.Interior.Color = colorMidasi
End With
With saiSheet.Cells(HanteiRow, 3)
.Value = compSheet1.Cells(1, col)
.Interior.Color = colorMidasi
End With
If comp1 <> 0 Then
saiSheet.Cells(HanteiRow, 4).Value = "'" + comp1
saiSheet.Cells(HanteiRow, 6).Value = comp1v
End If
If comp2 <> 0 Then
saiSheet.Cells(HanteiRow, 5).Value = "'" + comp2
saiSheet.Cells(HanteiRow, 7).Value = comp2v
End If
HanteiRow = HanteiRow + 1
Else
keka = markOK
compSheet1.Cells(row, col).Interior.ColorIndex = 0
compSheet2.Cells(row, col).Interior.ColorIndex = 0
End If
With hanteiSheet.Cells(row, col)
.Value = keka
.HorizontalAlignment = xlCenter
If keka = markNG Then
.Interior.Color = colorHantei
.Font.Bold = True
End If
End With
Next
If row >= MaxRow Then
'列集計
hanteiSheet.Cells(row, col).FormulaR1C1 = _
"=COUNTIF(R" & 1 & "C" & col & ":R" & row - 1 & "C" & col & ",""" & markNG & """)"
With hanteiSheet.Cells(row, col)
.Interior.Color = colorMidasi
.HorizontalAlignment = xlCenter
End With
End If
Next
For row = 1 To MaxRow
'行集計
hanteiSheet.Cells(row, col).FormulaR1C1 = _
"=COUNTIF(R" & row & "C" & 1 & ":R" & row & "C" & MaxCol & ",""" & markNG & """)"
With hanteiSheet.Cells(row, col)
.Interior.Color = colorMidasi
.HorizontalAlignment = xlCenter
End With
Next
'列幅調整
For i = 1 To 7
saiSheet.Cells(1, i).EntireColumn.AutoFit
Next
Dim wd: wd = 0
For i = 4 To 7
If wd < saiSheet.Cells(1, i).ColumnWidth Then
wd = saiSheet.Cells(1, i).ColumnWidth
End If
Next
For i = 4 To 7
saiSheet.Cells(1, i).ColumnWidth = wd
Next
End Sub