读取文件并比较工作表中的内容

时间:2019-07-05 03:27:40

标签: excel vba

当前,我能够比较同一文件中的不同工作表。如果将Sheet3移到另一个文件(例如:File2),如何实现?并且Sheet1 B2将返回“通过”或“失败”,取决于内容匹配与否。

文件1-Sheet1

Sheet1

文件1-Sheet2

Sheet2

文件1-Sheet3

Sheet3

system preferences -> security & privacy -> privacy -> {unlock the padlock putting my admin password} -> {locate, uncheck and recheck my executable from the list}

1 个答案:

答案 0 :(得分:0)

  1. 使用Workbooks.Open
  2. 打开外部工作簿
  3. 使用外部工作表代替Sheet3

类似这样的东西:

Sub Compare()
    Dim dataLength As Long
    dataLength = 100

    Sheet1.Range("B2").Value = "Pass"

    'open the workbook
    Dim ExternalWb As Workbook
    Set ExternalWb = Workbooks.Open(Filename:="C:\your folder\your file.xlsx")

    'get the sheet
    Dim ExternalSheet As Worksheet
    Set ExternalSheet = ExternalWb.Worksheets("Sheet3")


    Dim i As Long
    For i = 1 To dataLength
        If ExternalSheet.Cells(1, i) <> Sheet2.Cells(i, 1) Then
            Sheet1.Range("B2").Value = "Fail"
            Exit For
        End If
    Next i
End Sub
相关问题