|
代码供参考,楼主看着办:
- Type PostInfo
- TopLeft_RowID As Long '左上角单元格行号
- TopLeft_ColID As Long '左上角单元格列号
- BottomRight_RowID As Long '右下角单元格行号
- BottomRight_ColID As Long '右下角单元格列号
- Offset_Left_Cols As Long '定位区域左边的列数
- Offset_Right_Cols As Long '定位区域右边的列数
- Count_Rows As Long '总行数
- Count_Cols As Long '总列数
- End Type
- Dim rgStandard As Range '对照区域
- Dim rgSource As Range '比对区域
- Dim rgResult As Range '结果区域
- Dim rgPosition As Range '定位单元格
- Dim rgTemp As Range '临时单元格
- Dim myRgInfo As PostInfo '对照区域的信息
- Dim SourceInfo As PostInfo '比对区域的信息
- Dim objDic As Object '字典对象,存储非空单元格信息
- Dim blIsSelect As Boolean
- Sub myCheck()
- Dim arr As Variant, strTemp As String
- Dim lngRow As Long, lngCol As Long
- Dim lngTemp As Long
- Dim lngIndex_Row As Long
- Dim lngIndex_Col As Long
- Dim myShape As Shape
- '取消原有背景色
- 'ActiveSheet.Cells.Interior.ColorIndex = 0
- ' 删除原有圆圈
- ' DelShapes
-
- 'Step 1 选择对照区域
- blIsSelect = False
- Do Until blIsSelect = True
- Set rgStandard = GetRange("请选择对照区域", "Step 1 - 对照区域选择")
- If rgStandard Is Nothing Then Exit Sub
- blIsSelect = True
- If rgStandard.Count < 2 Then
- MsgBox "请正确选择对照区域!"
- blIsSelect = False
- End If
- Loop
- '对照区域填色
- 'rgStandard.Interior.ColorIndex = 44
-
- myRgInfo.TopLeft_RowID = rgStandard.Row
- myRgInfo.TopLeft_ColID = rgStandard.Column
- myRgInfo.BottomRight_RowID = rgStandard.Row + rgStandard.Rows.Count - 1
- myRgInfo.BottomRight_ColID = rgStandard.Column + rgStandard.Columns.Count - 1
- myRgInfo.Count_Rows = rgStandard.Rows.Count
- myRgInfo.Count_Cols = rgStandard.Columns.Count
-
- 'Step 2 选择定位区域
- Set rgTemp = Range(Cells(myRgInfo.BottomRight_RowID + 1, myRgInfo.TopLeft_ColID), Cells(myRgInfo.BottomRight_RowID + 1, myRgInfo.BottomRight_ColID))
- blIsSelect = False
- Do Until blIsSelect = True
- Set rgPosition = GetRange("请选择定位区域", "Step 2 - 定位区域选择")
- If rgPosition Is Nothing Then Exit Sub
- blIsSelect = True
- If rgPosition.Count > 1 Or (rgPosition.Row <> myRgInfo.BottomRight_RowID + 1) Or rgPosition.Column < myRgInfo.TopLeft_ColID Or rgPosition.Column > myRgInfo.BottomRight_ColID Then
- MsgBox "定位区域只能在【" & rgTemp.Address(0, 0) & "】中选择,且只能是【1】个单元格"
- blIsSelect = False
- Else
- If Trim(rgPosition.Value) = "" Then
- MsgBox "定位区域必须是 非空 单元格!"
- blIsSelect = False
- End If
- End If
- Loop
-
- '定位区域填色
- 'rgPosition.Interior.ColorIndex = 3
-
- myRgInfo.Offset_Left_Cols = rgPosition.Column - myRgInfo.TopLeft_ColID
- myRgInfo.Offset_Right_Cols = myRgInfo.BottomRight_ColID - rgPosition.Column
-
- 'Step 3 选择比对区域
- blIsSelect = False
- Do Until blIsSelect = True
- Set rgSource = GetRange("请选择比对区域", "Step 3 - 比对区域选择")
- If rgSource Is Nothing Then Exit Sub
- blIsSelect = True
- If rgSource.Count < 2 Then
- MsgBox "请正确选择比对区域!"
- blIsSelect = False
- End If
- Loop
-
- '比对区域填色
- 'rgSource.Interior.ColorIndex = 6
-
- SourceInfo.TopLeft_RowID = rgSource.Row
- SourceInfo.TopLeft_ColID = rgSource.Column
- SourceInfo.BottomRight_RowID = rgSource.Row + rgSource.Rows.Count - 1
- SourceInfo.BottomRight_ColID = rgSource.Column + rgSource.Columns.Count - 1
- SourceInfo.Count_Rows = rgSource.Rows.Count
- SourceInfo.Count_Cols = rgSource.Columns.Count
-
- '结果显示区域
- Set rgResult = Range(Cells(SourceInfo.BottomRight_RowID + 1, SourceInfo.TopLeft_ColID), Cells(SourceInfo.BottomRight_RowID + 1, SourceInfo.BottomRight_ColID))
-
- '结果区域填色
- 'rgResult.Interior.ColorIndex = 3
-
- '将对照区域的非空单元格存入字典
- Set objDic = CreateObject("Scripting.Dictionary")
- arr = rgStandard
- For lngRow = LBound(arr) To UBound(arr)
- For lngCol = LBound(arr, 2) To UBound(arr, 2)
- strTemp = arr(lngRow, lngCol)
- If Trim(strTemp) <> "" Then
- strTemp = lngRow & "," & lngCol
- objDic(strTemp) = ""
- End If
- Next
- Next
-
- '开始比对
- If myRgInfo.Count_Rows < SourceInfo.Count_Rows Then
- lngIndex_Row = 0
- Else
- lngIndex_Row = myRgInfo.Count_Rows - SourceInfo.Count_Rows
- End If
-
- For Each rgTemp In rgResult
- lngTemp = rgTemp.Column - SourceInfo.TopLeft_ColID
- If lngTemp > myRgInfo.Offset_Left_Cols Then lngTemp = myRgInfo.Offset_Left_Cols
- SourceInfo.Offset_Left_Cols = lngTemp
-
- lngTemp = SourceInfo.BottomRight_ColID - rgTemp.Column
- If lngTemp > myRgInfo.Offset_Right_Cols Then lngTemp = myRgInfo.Offset_Right_Cols
- SourceInfo.Offset_Right_Cols = lngTemp
-
- lngIndex_Col = myRgInfo.Offset_Left_Cols - SourceInfo.Offset_Left_Cols
-
- arr = rgTemp.Offset(SourceInfo.Count_Rows * -1, SourceInfo.Offset_Left_Cols * -1).Resize(myRgInfo.Count_Rows - lngIndex_Row, myRgInfo.Count_Cols - lngIndex_Col)
- lngTemp = 0
- For lngRow = LBound(arr) To UBound(arr)
- For lngCol = LBound(arr, 2) To UBound(arr, 2)
- strTemp = arr(lngRow, lngCol)
- If Trim(strTemp) <> "" Then
- strTemp = lngRow + lngIndex_Row & "," & lngCol + lngIndex_Col
- If objDic.Exists(strTemp) Then lngTemp = lngTemp + 1
- End If
- Next
- Next
- If lngTemp >= 2 Then
- rgTemp.Value = lngTemp
- Set myShape = ActiveSheet.Shapes.AddShape(msoShapeOval, rgTemp.Left, rgTemp.Top, rgTemp.Width, rgTemp.Height)
- myShape.Fill.Visible = msoFalse
- myShape.Line.Weight = 2
- myShape.Line.ForeColor.ObjectThemeColor = msoThemeColorAccent4
- Set myShape = Nothing
- End If
- Next
-
- MsgBox "比对OK"
- End Sub
- Function GetRange(strPrompt As String, Optional strTitle As String = "区域选择") As Range
- On Error Resume Next
- Set GetRange = Application.InputBox(Prompt:=strPrompt, Title:=strTitle, Type:=8)
- On Error GoTo 0
- End Function
- Function DelShapes()
- Dim sh As Shape
- For Each sh In ActiveSheet.Shapes
- If sh.Type = 1 Then sh.Delete
- Next
- End Function
复制代码 |
|