简体   繁体   English

如何比较两个不同的Excel工作簿中的列

[英]How to compare columns from two different excel workbooks

I have an Excel-Workbook which has two sheets 'Task' and 'Cities'. 我有一个Excel工作簿,其中有两个工作表“任务”和“城市”。 I need to compare the code for validation of records from 'Task' sheet with that one in 'Cities' Sheet. 我需要将“任务”表中的记录验证代码与“城市”表中的代码进行比较。

I am able to do it as follows: 我能够做到如下:

Dim CityString As String
Dim CityArray() As String

'Get the last row
'Dim lastRow As Integer
LastRow = Sheets("Task").UsedRange.Rows.Count
nLastRowSheet2 = Sheets("Cities").UsedRange.Rows.Count

Dim c As Range
Dim d As Range
Dim e As Variant

'Turn screen updating off to speed up macro code.
'User won't be able to see what the macro is doing, but it will run faster.
Application.ScreenUpdating = False

For Each c In Worksheets("Task").Range("A2:A" & LastRow)
    CityString = c
    CityArray() = Split(CityString, ";")
    For Each e In CityArray()
        e = Trim(e)

        Dim rngFnder As Range
        On Error Resume Next

            Set rngFnder = Sheets("Cities").Range("A2:A" & nLastRowSheet2).Find(e)

            If rngFnder Is Nothing Then
                c.Interior.Color = vbRed
            End If

        On Error GoTo 0
    Next
Next

Now I have another requirement where I have to do the same from two different workbooks. 现在,我还有另一个要求,我必须在两个不同的工作簿中执行相同的操作。

('Task' and 'Cities' sheets are on two different Workbooks) (“任务”和“城市”工作表位于两个不同的工作簿上)

Can anyone tell me; 谁能告诉我; what all changes I have to make to the above code? 我必须对上述代码进行哪些更改?

When you say "on two different excelssheets" I'm guessing you mean they are in a different Excel workbook file? 当您说“在两个不同的excelssheets上”时,我猜您是说它们在不同的Excel工作簿文件中?

When you use the following line you are refering to the sheet "Task" in the active workbook. 使用以下行时,是指活动工作簿中的“任务”表。

Worksheets("Task").Range("A2:A" & LastRow)

You should specify the workbook to avoid writing to the wrong one. 您应该指定工作簿,以避免写错工作簿。 Using thisWorkbook refers to the workbook with the code eg 使用thisWorkbook指的是带有代码的工作簿,例如

ThisWorkbook.Worksheets("Task").Range("A2:A" & LastRow)

To access any open workbook including the current workbook use 要访问任何打开的工作簿(包括当前工作簿),请使用

Workbooks("example.xls").Worksheets("Task").Range("A2:A" & LastRow)

If the workbook is closed you can open it using 如果工作簿已关闭,则可以使用

dim wrk as Workbook
set wrk = Workbooks.Open("C:\Docs\example.xls") 
wrk.Worksheets("Task").Range("A2:A" & LastRow)

If you want to know more about workbooks in VBA you can read about them here 如果您想了解有关VBA中工作簿的更多信息,可以在这里阅读有关它们的信息。

I have prepared another workbook with name 'Sample_Data.xlsx' under which I will have a sheet 'cities'. 我准备了另一个名为“ Sample_Data.xlsx”的工作簿,在该工作簿下将有一个工作表“城市”。

So where else in the below code I have to make changes to work it correctly; 因此,在以下代码中的其他地方,我必须进行更改才能正常工作; when my 'Sample_Data.xlsx' workbook is closed. 当我的“ Sample_Data.xlsx”工作簿关闭时。 The below code opens the workbook; 下面的代码打开工作簿; but do not do validation. 但不要进行验​​证。

Dim CityString As String
Dim CityArray() As String

'Get the last row
'Dim lastRow As Integer
LastRow = Sheets("Task").UsedRange.Rows.Count

Dim wrk As Workbook
Set wrk = Workbooks.Open("E:\Final\Sample_Data.xlsx")

nLastRowSheet2 = wrk.Worksheets("cities").UsedRange.Rows.Count

Dim c As Range
Dim d As Range
Dim e As Variant

'Turn screen updating off to speed up macro code.
'User won't be able to see what the macro is doing, but it will run faster.
Application.ScreenUpdating = False

For Each c In Worksheets("Task").Range("A2:A" & LastRow)
    CityString = c
    CityArray() = Split(CityString, ";")
    For Each e In CityArray()
        e = Trim(e)

        Dim rngFnder As Range
        On Error Resume Next

        Set rngFnder = wrk.Sheets("Cities").Range("A2:A" & nLastRowSheet2).Find(e)

        If rngFnder Is Nothing Then
            c.Interior.Color = vbRed
        End If

        On Error GoTo 0
    Next
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM