简体   繁体   English

需要帮助比较不同工作簿中两列的值

[英]Need help comparing values of two columns in different WorkBooks

I'm trying to compare two columns in two different WB let's say A and B which have only column each.我正在尝试比较两个不同 WB 中的两列,假设 A 和 B 各只有一列。 I'd like to msgbox a text whenever the value of cell in the column of A is also in the column of B.每当 A 列中的单元格值也在 B 列中时,我想对文本进行 msgbox。

I managed to put values in a variant variable and like now to compare them.我设法将值放入一个变体变量中,然后像现在一样比较它们。 I still get a 424 error at the final if statement that checks the correspondance.在检查对应关系的最终 if 语句中,我仍然收到 424 错误。

Here is the code :这是代码:

Option Explicit

Sub uniformisation()

Dim range1 As Variant
Dim range2 As Variant

Dim Tab1 As Variant, tab2 As Variant 

Dim fichierM As Workbook 

Dim fichierF As Workbook 


Set fichierF = Workbooks.Open("thepath")
Set fichierMission = Workbooks.Open("thepath")

fichierF.Activate
fichierM.Activate

Dim wsF As Worksheet
Dim wsM As Worksheet

Set wsF = fichierF.Worksheets("test")
Set wsM = fichierM.Worksheets("A")

Dim C As range
Dim D As range


Set C = wsFlex.Columns(1)
Set D = wsMiss.Columns(1)


Dim TotalRows1 As Long
Dim TotalRows2 As Long



With wsF
    TotalRows1 = C.Rows(Rows.Count).End(xlUp).Row
    Tab1 = range(Cells(2, 1), Cells(TotalRows1, 1)).Value
    MsgBox UBound(Tab1)

End With


With wsM
    TotalRows2 = Rows(D.Rows.Count).End(xlUp).Row

    tab2 = range(Cells(2, 2=1), Cells(TotalRows2, 1))

    MsgBox UBound(tab2)

End With


For Each range1 In Tab1
For Each range2 In tab2


        If range1.Value = range2.Value Then
            MsgBox range1

            End If

        Next range2
        Next range1




fichierM.Close
fichierF.Close



End Sub

Any help would be really apreciated, thanks !任何帮助都会非常感谢,谢谢!

you definitions are all over the place and the code is too long for what it is supposed to do.你的定义到处都是,代码对于它应该做的来说太长了。 Also, you have chosen variant which is not really needed for what you want to do.此外,您选择了您想要做的事情并不真正需要的变体。 Here is a shorter version that can get you started:这是一个较短的版本,可以帮助您入门:

Sub CompareTwoColumns()
    Dim rng1 As Range
    Dim rng2 As Range
    Dim WB1 As Workbook
    Dim WB2 As Workbook

    'make sure both workbooks are open
    Set WB1 = Workbooks.Open("thepath1")
    Set WB2 = Workbooks.Open("thepath2")

    'loop through both columns and compare
    For Each rng1 In WB1.Worksheets("Sheet1").UsedRange.Columns(1).Cells
        For Each rng2 In WB2.Worksheets("Sheet1").UsedRange.Columns(1).Cells
            If rng1.Value = rng2.Value Then
                MsgBox rng1.Value
            End If
        Next rng2
    Next rng1
End Sub

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

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