Compare Two Columns in two different workbooks using VBA

sravanrisho

New Member
Joined
Feb 17, 2020
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
The below one has written with specific range and in the same workbook in two sheets(sheet1,Sheet2)
Code:



looking for your help guys,If row range changes dynamically(no specific range) and in two different books.how to write
Different way of approaching the common column in excel is also accepted
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the forum
Did you forgot to paste your code into the vba code box ?
 
Upvote 0
Sub CheckAvailability()
Dim rMyRng As Range, rCompare As Range, r As Range, lFound As Long,
blStatus As Boolean

Application.ScreenUpdating = False

With Sheets("Sheet1")
Set rMyRng = .Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row)
End With

With Sheets("Sheet2")
Set rCompare = .Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row)
End With

For Each r In rMyRng.Rows
With r
.Select
blStatus = False
lFound = Application.CountIfs(rCompare.Columns(1), .Cells(1).Value,
rCompare.Columns(2), .Cells(2).Value)
If lFound Then blStatus = True
.Cells(2).Offset(, 1).Value = blStatus
End With
Next r

Application.ScreenUpdating = True
 
Upvote 0
2 errors I spotted in your code (same error)

A
Set rMyRng = .Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row)
Set rMyRng = .Range("A1:B" & .Range("B" & Rows.Count).End(xlUp).Row)
B
Set rCompare = .Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row)
Set rCompare = .Range("A1:B" & .Range("B" & Rows.Count).End(xlUp).Row)


VBA Code:
Sub CheckAvailability()

    Dim rMyRng As Range, rCompare As Range, r As Range, lFound As Long, blStatus As Boolean
    Application.ScreenUpdating = False

    With Sheets("Sheet1")
        Set rMyRng = .Range("A1:B" & .Range("B" & Rows.Count).End(xlUp).Row)
    End With

    With Sheets("Sheet2")
        Set rCompare = .Range("A1:B" & .Range("B" & Rows.Count).End(xlUp).Row)
    End With
   
    For Each r In rMyRng.Rows
        With r
            .Select
            blStatus = False
            lFound = Application.CountIfs(rCompare.Columns(1), .Cells(1).Value, rCompare.Columns(2), .Cells(2).Value)
            If lFound Then blStatus = True
                .Cells(2).Offset(, 1).Value = blStatus
        End With
    Next r

    Application.ScreenUpdating = True

End Sub

Do you need any other help ?
 
Upvote 0
Hey Thnaks yongle "Range if that is fixed,my real case problem is the row range is not fixed and column A in two different workbooks " is it psossible to help me that
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top