Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 比较两张Excel表格,找出差异_Vba_Excel - Fatal编程技术网

Vba 比较两张Excel表格,找出差异

Vba 比较两张Excel表格,找出差异,vba,excel,Vba,Excel,我想找出或强调两张Excel表格之间的差异 从上图中,我想根据“名称”和“规则名称”比较这两张表,如果数字匹配,则需要检查“文本”和“规则文本”的差异,并需要在第二个Excel文档中找到突出显示的文本等差异。我创建此文件是为了比较几年前的两本Excel工作簿,代码非常简单,但它的工作并没有什么限制 限制: 两个文件不应具有相同的名称 它只比较单元格中的值,不比较任何图形 它只比较前300行和前200列,您可以很容易地在代码中更新它以满足您的需要 代码分为两个子部分。1.比较工作簿和2。创建新工

我想找出或强调两张Excel表格之间的差异


从上图中,我想根据“名称”和“规则名称”比较这两张表,如果数字匹配,则需要检查“文本”和“规则文本”的差异,并需要在第二个Excel文档中找到突出显示的文本等差异。

我创建此文件是为了比较几年前的两本Excel工作簿,代码非常简单,但它的工作并没有什么限制

限制:

  • 两个文件不应具有相同的名称
  • 它只比较单元格中的值,不比较任何图形
  • 它只比较前300行和前200列,您可以很容易地在代码中更新它以满足您的需要
  • 代码分为两个子部分。1.比较工作簿和2。创建新工作表

    您可以创建browse button宏来填充命名单元格“file1”和命名单元格“file2”中的两个excel文件名。然后可以使用Compareworkbook宏比较两个excel文件。运行“Compareworkbook”宏后,它将创建新的工作表以显示报告。它只显示不同的值

    您可以修改此代码以比较某些列或满足您的需要。这应该给你一个很好的起点

    Sub CompareWorkbook1()
    'this subroutine is created to compare two excel files
    'This will only compare first 300 rows and 150 column for all worksheet
    'in both workbook assuming both workbook has same number of worksheets
    
    Dim wb As Workbook, wb1 As Workbook, wb2 As Workbook
        Dim ws As Worksheet
        Dim wBook1 As Variant
        Dim wBook2 As Variant
        'Set source workbook
        Set wb = ActiveWorkbook
                'Open the target workbook
    
    wBook1 = ActiveWorkbook.Sheets("Sheet1").Range("file1").Value
    wBook2 = ActiveWorkbook.Sheets("Sheet1").Range("file2").Value
    
    
        Answer = MsgBox("This will generate a new report, Do you want to proceed?", vbQuestion + vbYesNo, "Are you sure? This will delete existing reports and generate new reports")
        If Answer = vbNo Then
            GoTo exit1
        Else
            If Range("file1").Value = "" Then
                Msg = "ERROR: INFORMATION MISSING ..." & vbNewLine & vbNewLine
                Msg = Msg & "Make sure you browse the file  "
                Msg = Msg & "by clicking on Browse button next to Step 1 " & vbNewLine & vbNewLine
                Msg = Msg & "REPORT WILL NOT GENERATE"
                MsgBox Msg, vbCritical
                GoTo exit1
            End If
            If Range("file2").Value = "" Then
                Msg = "ERROR: INFORMATION MISSING ..." & vbNewLine & vbNewLine
                Msg = Msg & "Make sure you browse the file  "
                Msg = Msg & "by clicking on Browse button next to Step 2 " & vbNewLine & vbNewLine
                Msg = Msg & "REPORT WILL NOT GENERATE"
                MsgBox Msg, vbCritical
                GoTo exit1
            End If
    
    
        'generate new worksheet
        ReportName = "Comparison Results"
        Call CreateNewWorksheet(ReportName)
    
        'set workbooks as variable wb1 and wb2
        Set wb1 = Workbooks.Open(wBook1)
        Set wb2 = Workbooks.Open(wBook2)
    wb.Sheets(2).Cells(4, 2).Value = wb1.Name
    wb.Sheets(2).Cells(4, 3).Value = wb2.Name
    wb.Sheets(2).Cells(3, 7).Value = wb1.Name
    wb.Sheets(2).Cells(3, 10).Value = wb2.Name
    
    'Pull data from browsed workbook for All incident
    'MsgBox "WOrkbooks are opened"
    ThisWorkbook.Activate
    Dim oSheet As Excel.Worksheet
    
    'This will populate all Worksheet name in Combo box
        Dim a As Integer
        Dim b As Integer
        Dim c As Integer
        Dim d As Integer
        Dim wSheetsNo As Integer
        Dim wSheetsNo1 As Integer
        Dim wSheetsNo2 As Integer
        a = 1
        b = 1
        c = 1
        d = 1
        wSheetsNo1 = 0
        wSheetsNo2 = 0
    
        a = 5
        b = 2
        For Each oSheet In wb1.Sheets
    
    
            wb.Sheets(2).Cells(a, b) = oSheet.Name
            a = a + 1
            wSheetsNo1 = wSheetsNo1 + 1
        Next oSheet
        a = 5
        b = 3
        For Each oSheet In wb1.Sheets
    
            wb.Sheets(2).Cells(a, b) = oSheet.Name
            a = a + 1
            wSheetsNo2 = wSheetsNo2 + 1
        Next oSheet
    
        a = 5
        b = 7
    'populates all worksheet from 1st workbook to compare
    For wSheetsNo = 1 To wSheetsNo1
        'Compares from row 1 to 300
        For c = 1 To 300
            'Compares columns 1 to 200
            For d = 1 To 200
                'Compares each cell value in each worksheets for these two workbook
                If wb1.Sheets(wSheetsNo).Cells(c, d).Value <> wb2.Sheets(wSheetsNo).Cells(c, d).Value Then
                    wb.Sheets(2).Cells(a, b + 1) = "Cells (" & c & ", " & d & ")"
                    wb.Sheets(2).Cells(a, b + 4) = "Cells (" & c & ", " & d & ")"
                    wb.Sheets(2).Cells(a, b + 2) = wb1.Sheets(wSheetsNo).Cells(c, d).Value
                    wb.Sheets(2).Cells(a, b + 5) = wb2.Sheets(wSheetsNo).Cells(c, d).Value
                    wb.Sheets(2).Cells(a, b) = wb1.Sheets(wSheetsNo).Name
                    wb.Sheets(2).Cells(a, b + 3) = wb2.Sheets(wSheetsNo).Name
                    a = a + 1
                End If
            'looks into next column
            Next
        'looks into next row
        Next
    'looks into next worksheet
    Next
    
    'closes both workbook
    wb1.Close
    wb2.Close
    End If
    'exit if files is now browsed or path is empty
    exit1:
    
    End Sub
    
    Sub CreateNewWorksheet(ReportName)
    
    Dim wsSheet As Worksheet
    
        On Error Resume Next
            Set wsSheet = Sheets(ReportName)
        On Error GoTo 0
            If Not wsSheet Is Nothing Then
                Application.DisplayAlerts = False
                Sheets(ReportName).Delete
                Application.DisplayAlerts = True
            End If
    
    'Add New sheet at end of worksheet
        ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
    
        ActiveSheet.Name = ReportName
    
    Sheets("Comparison Results").Select
        Range("B4").Select
        Sheets("Comparison Results").Select
        Range("B3").Select
        ActiveCell.FormulaR1C1 = "Worksheets which are compared"
        Range("B4").Select
        Columns("B:B").ColumnWidth = 27.57
        Columns("B:B").Select
        Selection.ColumnWidth = 28
        Columns("C:C").Select
        Selection.ColumnWidth = 28
        Range("B3:C3").Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Selection.Font.Bold = True
        Range("B4").Select
        ActiveCell.FormulaR1C1 = "1st Workbook"
        Range("C4").Select
        ActiveCell.FormulaR1C1 = "2nd Workbook"
        Range("B4:C4").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Range("B3:C50").Select
        ActiveWindow.SmallScroll Down:=-45
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Range("B3:C4").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 10092543
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Range("B5:C50").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 10092543
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Range("C13").Select
        ActiveWindow.SmallScroll Down:=-15
        Range("B3:C3").Select
        ActiveCell.FormulaR1C1 = "Worksheets which are compared"
        Columns("G:L").Select
        Selection.ColumnWidth = 28
        Selection.ColumnWidth = 10
        Selection.ColumnWidth = 15
        Selection.ColumnWidth = 18
        Range("G3:I3").Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Selection.Copy
        Range("J3").Select
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        Range("G3:I3").Select
        ActiveCell.FormulaR1C1 = ""
        Range("G4").Select
        ActiveCell.FormulaR1C1 = "Worksheet"
        Range("H4").Select
        ActiveCell.FormulaR1C1 = "Cell number"
        Range("I4").Select
        ActiveCell.FormulaR1C1 = "Value in the cell"
        Range("G4:I4").Select
        Selection.Copy
        Range("J4").Select
        ActiveSheet.Paste
        Range("B4").Select
        Application.CutCopyMode = False
        Selection.ClearContents
        Range("C4").Select
        Selection.ClearContents
        Range("G3:L10000").Select
        Range(Selection, Selection.End(xlDown)).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 10092543
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Range("G3:L4").Select
        Selection.Font.Bold = True
        Range("B3:C4").Select
        Selection.Font.Bold = False
        Selection.Font.Bold = True
        Range("B4:L4").Select
        Range(Selection, Selection.End(xlDown)).Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Range("G3:L3").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
        End With
        Range("G5").Select
        ActiveWindow.SmallScroll Down:=-15
        Range("G3:I3").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlToLeft)).Select
        Range("G3:L3").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Range("G3:L4").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
         Range("G3:L10000").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
    
    
        Range("B3:C4").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Range("B3:C50").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Range("G3:I10000").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
    Columns("D:F").Select
        Range("F1").Activate
        Selection.ColumnWidth = 3
        Range("G2:L2").Select
        With Selection
            .HorizontalAlignment = xlGeneral
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 10092543
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Range("G2:L2").Select
        ActiveCell.FormulaR1C1 = "Comparison Results"
        Range("G2:L2").Select
        Selection.Font.Bold = True
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        With Selection.Font
            .Name = "Calibri"
            .Size = 14
            .Strikethrough = False
            .Superscript = False
            .Subscript = False
            .OutlineFont = False
            .Shadow = False
            .Underline = xlUnderlineStyleNone
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
            .ThemeFont = xlThemeFontMinor
        End With
        Range("G2:L2").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
        'Sheets("Sheet1").Select
        Range("B2").Select
    Range("G3:L4").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlMedium
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Range("B1").Select
    End Sub
    
    Sub CompareWorkbook1()
    '创建此子例程是为了比较两个excel文件
    '这将仅比较所有工作表的前300行和150列
    '在两个工作簿中,假设两个工作簿的工作表数相同
    将wb设置为工作簿、wb1设置为工作簿、wb2设置为工作簿
    将ws设置为工作表
    Dim wBook1作为变体
    Dim wBook2作为变体
    '设置源工作簿
    设置wb=ActiveWorkbook
    '打开目标工作簿
    wBook1=ActiveWorkbook.Sheets(“Sheet1”).Range(“file1”).Value
    wBook2=ActiveWorkbook.Sheets(“Sheet1”).Range(“file2”).Value
    Answer=MsgBox(“这将生成新报告,是否继续?”,vbQuestion+vbYesNo,“是否确定?这将删除现有报告并生成新报告”)
    如果答案=vbNo,则
    转到出口
    其他的
    如果范围(“文件1”).Value=“”,则
    Msg=“错误:信息丢失…”&vbNewLine&vbNewLine
    Msg=Msg&“确保浏览该文件”
    Msg=Msg&“通过单击步骤1旁边的浏览按钮”&vbNewLine&vbNewLine
    Msg=Msg&“将不生成报告”
    MsgBox Msg,vb临界
    转到出口
    如果结束
    如果范围(“文件2”).Value=“”,则
    Msg=“错误:信息丢失…”&vbNewLine&vbNewLine
    Msg=Msg&“确保浏览该文件”
    Msg=Msg&“通过单击第2步旁边的浏览按钮”&vbNewLine&vbNewLine
    Msg=Msg&“将不生成报告”
    MsgBox Msg,vb临界
    转到出口
    如果结束
    '生成新工作表
    ReportName=“比较结果”
    调用CreateNewWorksheet(ReportName)
    '将工作簿设置为变量wb1和wb2
    设置wb1=工作簿。打开(wBook1)
    设置wb2=工作簿。打开(wBook2)
    wb.Sheets(2).Cells(4,2).Value=wb1.Name
    wb.Sheets(2).单元格(4,3).Value=wb2.Name
    wb.Sheets(2).单元格(3,7).Value=wb1.Name
    wb.Sheets(2).Cells(3,10).Value=wb2.Name
    '从浏览的工作簿中提取所有事件的数据
    “MsgBox”工作簿已打开
    此工作簿。激活
    将oSheet设置为Excel.工作表
    '这将填充组合框中的所有工作表名称
    将a变暗为整数
    作为整数的Dim b
    作为整数的Dim c
    作为整数的Dim d
    将wSheetsNo设置为整数
    将wSheetsNo1设置为整数
    将wSheetsNo2设置为整数
    a=1
    b=1
    c=1
    d=1
    wSheetsNo1=0
    wSheetsNo2=0
    a=5
    b=2
    对于wb1.表中的每个oSheet
    wb.表(2).单元格(a,b)=oSheet.名称
    a=a+1
    wSheetsNo1=wSheetsNo1+1
    下一个奥希特
    a=5
    b=3
    对于wb1.表中的每个oSheet
    wb.表(2).单元格(a,b)=oSheet.名称
    a=a+1
    wSheetsNo2=wSheetsNo2+1
    下一个奥希特
    a=5
    b=7
    '填充第一个工作簿中要比较的所有工作表
    对于wSheetsNo=1到wSheetsNo1
    '从第1行比较到第300行
    对于c=1至300
    '将第1列与第200列进行比较
    对于d=1到200
    '比较这两个工作簿的每个工作表中的每个单元格值
    如果wb1.Sheets(wSheetsNo).Cells(c,d).Value wb2.Sheets(wSheetsNo).Cells(c,d).Value然后
    工作分解表(2).单元(a、b+1)=“单元(“&c&”、“&d&”)
    工作分解表(2).单元(a、b+4)=“单元(“&c&”、“&d&”)
    wb.表(2).单元格(a,b+2)=wb1.表(wSheetsNo).单元格(c,d).值
    wb.表格(2).单元格(a,b+5)=wb2.表格(wSheetsNo).单元格(c,d).值
    wb.Sheets(2).单元格(a,b)=wb1.Sheets(wSheetsNo).名称
    wb.Sheets(2).单元格(a,b+3)=wb2.Sheets(wSheetsNo).名称
    a=a+1
    如果结束
    '查看下一列
    下一个
    “看下一排
    下一个
    '查看下一个工作表
    下一个
    '关闭两个工作簿
    wb1.关闭
    wb2.关闭
    如果结束
    '如果现在已浏览文件或路径为空,则退出
    出口1:
    端接头
    子CreateNewWorksheet(报表名称)
    将工作表设置为工作表
    出错时继续下一步
    设置wsSheet=Sheets(报告名称)
    错误转到0
    如果不是,那么wsSheet什么都不是
    Application.DisplayAlerts=False
    工作表(报告名称)。删除
    Application.DisplayAlerts=True
    如果结束
    '在工作表末尾添加新工作表
    ActiveWorkbook.Sheets.Add After:=工作表(Worksheets.Count)
    ActiveSheet.Name=ReportName
    表格(“比较结果”)。选择
    范围(“B4”)。选择
    表格(“比较结果”)。选择
    范围(“B3”)。选择
    ActiveCell.FormulaR1C1=“比较的工作表”
    范围(“B4”)。选择
    列(“B:B”)。列宽=27.57
    列(“B:B”)。选择
    Selection.ColumnWidth=28
    
    Sub HighlightDiffBtwSheets()
    'Substitute "TEST1" with the name of the sheet where you have the Name-Text columns
    'Substitute "TEST2" with the name of the sheet where you have the RuleName-RuleText columns
    'Substitute A in the Range with the column letter of Name/RuleName
    
    For Each Name In Sheets("TEST1").Range("A2:A" & Sheets("TEST1").Cells(Rows.Count, 1).End(xlUp).Row)
            For Each RuleName In Sheets("TEST2").Range("A2:A" & Sheets("TEST2").Cells(Rows.Count, 1).End(xlUp).Row)
            If InStr(RuleName.Value, Name.Value) <> 0 Then
                If Name.Offset(, 1).Value <> RuleName.Offset(, 1).Value Then
                 RuleName.Offset(, 1).Select
                 With Selection.Interior
                .Color = 65535
                 End With
    
                 End If
            End If
            Next
    Next
    End Sub