find a value in two w/book sheets for value exists or not - vba code

tkraju

Board Regular
Joined
Apr 1, 2009
Messages
79
Office Version
  1. 2016
Platform
  1. Windows
I need Excel Vba code in excel for a following task.
Two w/books >first w/book -- ABC.xlsx >sheet1> Sheet1(A2) > has date value> if this value occurs in > second w/book--TKR.xlsx>sheet1>
search in entire Column B>If the date value exists> message>"Date already exists">Exit Sub> If date value not exists> do something else code.

both w/books are not opened. Location of 2 w/books>C:\Raju\My documents. I am using windows11> office365
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try this. It's untested because I don't have your file structure, so let me know if it works.
VBA Code:
Sub chkDate()
Dim wb1 As Excel.Workbook, wb2 As Excel.Workbook, ws1 As Worksheet, ws2 As Worksheet, chkVal As Variant, chkRng As Range
Dim myDir As String
Dim app1 As New Excel.Application, app2 As New Excel.Application
Dim wb As Excel.Workbook

myDir = "C:\Raju\My Documents\"
Set wb1 = app1.Workbooks.Add(myDir & "ABC.xlsx")
Set wb2 = app2.Workbooks.Add(myDir & "TKR.xlsx")
Set ws1 = wb1.Worksheets(1): Set ws2 = wb2.Worksheets(1)
Set chkRng = ws2.Range.Columns(2)
chkVal = ws1.Range("A2")

If Not chkRng.Find(chkVal) = "" Then
    MsgBox "Date already exists.", vbOKOnly, "Found"
Else
    'Other actions if no match
End If

wb1.Close False: wb2.Close False
Set app1 = Nothing: Set app2 = Nothing

End Sub
 
Upvote 0
I am thinking that you are running this from a workbook other than ABC or TKR. If that is the case try this code in that workbook...
VBA Code:
Sub FindDate()

    Dim wb As Workbook
    Dim dt As String, strFolder As String, strFile As String
    Dim rng As Range
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    strFolder = "C:\Raju\My documents\"
    strFile = "ABC.xlsx"
    
    Set wb = Workbooks.Open(strFolder & strFile)
    dt = ActiveSheet.Range("A2")
    wb.Close
    strFile = "TKR.xlsx"
    Set wb = Workbooks.Open(strFolder & strFile)
    Set rng = ActiveSheet.Range("B1:B" & Cells(Rows.Count, 2).End(xlUp).Row).Find(What:=dt, _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                            
    If Not rng Is Nothing Then
        MsgBox "Date already exists"
        wb.Close
        Exit Sub
    End If
    wb.Close
    MsgBox "do something else"
    
End Sub
 
Upvote 0
Solution
Try this. It's untested because I don't have your file structure, so let me know if it works.
VBA Code:
Sub chkDate()
Dim wb1 As Excel.Workbook, wb2 As Excel.Workbook, ws1 As Worksheet, ws2 As Worksheet, chkVal As Variant, chkRng As Range
Dim myDir As String
Dim app1 As New Excel.Application, app2 As New Excel.Application
Dim wb As Excel.Workbook

myDir = "C:\Raju\My Documents\"
Set wb1 = app1.Workbooks.Add(myDir & "ABC.xlsx")
Set wb2 = app2.Workbooks.Add(myDir & "TKR.xlsx")
Set ws1 = wb1.Worksheets(1): Set ws2 = wb2.Worksheets(1)
Set chkRng = ws2.Range.Columns(2)
chkVal = ws1.Range("A2")

If Not chkRng.Find(chkVal) = "" Then
    MsgBox "Date already exists.", vbOKOnly, "Found"
Else
    'Other actions if no match
End If

wb1.Close False: wb2.Close False
Set app1 = Nothing: Set app2 = Nothing

End Sub
Thank you so much, Skybot. Perfectly its done.
 
Upvote 0
Thank you so much, Skybot. Perfectly its done.
Sorry to say, I forgotten to mention that this macro runs from my second w/book TKR as this is my destination w/book. if no match found copy ws1.Range(Ä2:E4") and paste the values in destination w/book TKR at last row. What changes are to be done in your code ?
 
Upvote 0
I am confused, do you mean to be talking with me (igold) or @Skyybot...
 
Upvote 0
I am thinking that you are running this from a workbook other than ABC or TKR. If that is the case try this code in that workbook...
VBA Code:
Sub FindDate()

    Dim wb As Workbook
    Dim dt As String, strFolder As String, strFile As String
    Dim rng As Range
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    strFolder = "C:\Raju\My documents\"
    strFile = "ABC.xlsx"
   
    Set wb = Workbooks.Open(strFolder & strFile)
    dt = ActiveSheet.Range("A2")
    wb.Close
    strFile = "TKR.xlsx"
    Set wb = Workbooks.Open(strFolder & strFile)
    Set rng = ActiveSheet.Range("B1:B" & Cells(Rows.Count, 2).End(xlUp).Row).Find(What:=dt, _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                           
    If Not rng Is Nothing Then
        MsgBox "Date already exists"
        wb.Close
        Exit Sub
    End If
    wb.Close
    MsgBox "do something else"
   
End Sub
Thank You igold. If date does not exists please add this action in your code. Copy ABC.xlsx active sheet.Range A2 to E used range(the sheet's dynamic range) and paste copied values in TKR.xlsx column B last row. Close the Sub with msg "w/book updated".
 
Upvote 0
Is this what you are looking for. This code should be run from the TKR.xlsx workbook.

VBA Code:
Sub FindDate()

    Dim wb As Workbook, wbT As Workbook
    Dim dt As String, strFolder As String, strFile As String
    Dim rng As Range, usRng As Range, lRow As Long
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    strFolder = "C:\Raju\My documents\"
    strFile = "ABC.xlsx"
    Set wbT = ThisWorkbook
    Set wb = Workbooks.Open(strFolder & strFile)
    With wb.Worksheets("Sheet1")
        dt = .Range("A2")
        .UsedRange.Copy
    End With
    
    wb.Close
    lRow = wbT.Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
    Set rng = Worksheets("Sheet1").Range("B1:B" & lRow).Find(What:=dt, _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
                            
    If Not rng Is Nothing Then
        MsgBox "Date already exists"
        Exit Sub
    End If
    wbT.Worksheets("Sheet1").Range("B" & lRow + 1).PasteSpecial xlPasteAll
    Application.CutCopyMode = False
    
    MsgBox "Workbook Updated"
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
both w/books are not opened. Location of 2 w/books>C:\Raju\My documents. I am using windows11> office365
So to be clear, the Workbook TKR is open, and being used when the code runs?
 
Upvote 0

Forum statistics

Threads
1,215,307
Messages
6,124,172
Members
449,146
Latest member
el_gazar

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