需要帮助比较不同WorkBook中两列的值

问题描述 投票:0回答:1

我试图在两个不同的WB中比较两个列,让我们说A和B,每个只有列。每当A列中的单元格值也在B列中时,我想msgbox文本。

我设法将值放在变量变量中,就像现在比较它们一样。我仍然在检查对应的最终if语句中得到424错误。

这是代码:

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

任何帮助将非常感谢,谢谢!

excel vba access-vba range
1个回答
0
投票

你的定义到处都是,而且代码太长了,不应该做什么。此外,您选择的变体并非您想要做的事情。这是一个可以帮助您入门的较短版本:

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
© www.soinside.com 2019 - 2024. All rights reserved.