1

I have 2 workbooks book1 & book2. Both book1 and book2 have first row as dates

e.g. Book1
A       B       C
Jan-13       Feb-13      Mar-13
1                 4
2                 5
3                 6

Book2
A       B       C
Jan-13       Feb-13      Mar-13
1                 4                 3
5                 7                 6
9                 8                 1

The idea is to select a date from Book 1 and find if the date exists in book2 and copy the contents of book 2 column into book1 column

e.g. if I select Mar-13 in book 1, I should be able to find Mar-13 in book 2 and copy C3 to C5 from book2 to C3 to C5 of book1.

I am struggling with the find command in vba, I am using something like this to find

With ThisWorkbook.ActiveSheet.Range("A1:Z1")

    Set rngSMonthYr = .Find(What:=dtMonthYr, _
        LookIn:=xlValues, _
        LookAt:=xlWhole, _
        SearchOrder:=xlByColumns, _
        SearchDirection:=xlNext)
End With

But it isn't working at all.

0

1 Answer 1

1

Make sure you declare your variable as a Date type, and make sure that you're including the "day" portion of the date.

Dim dtMonthYr As Date
dtMonthYr = #1/1/2013#  

Even when you enter a value like 1/2013 in to a cell, Excel stores this as a Long date. This is tested and it works.

Sub TestFindDate()
Dim rngSMonthYr As Range
Dim dtMonthYr As Date
Dim wsFind as Worksheet


dtMonthYr = #1/1/2013#   

Set wsFind = ThisWorkbook.Sheets("Sheet2") '## Modify as needed

    With wsFind.Range("A1:Z1")
    Set rngSMonthYr = .Find(What:=dtMonthYr, _
        LookIn:=xlValues, _
        LookAt:=xlWhole, _
        SearchOrder:=xlByColumns, _
        SearchDirection:=xlNext)
    End With

If rngSMonthYr Is Nothing Then
    Debug.Print dtMonthYr & " not found"
Else:
    Debug.Print rngSMonthYr.Address
End If

Emd Sub
Sign up to request clarification or add additional context in comments.

2 Comments

I am using 2 different sheets, select date from sheet 1, and check if it exists in sheet 2, the date format is MMM-YY, but once I get the value it becomes DD/MM/YYYY, but if I get the text it stays MMM-YY,but the check actually returns address from sheet 1 which I need it from sheet 2
See update, I made a change to force the .Find operation on a specific worksheet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.