SEARCH FOR LAST MATCHING OCCURENCE IN Wb2, PASTE RANGE OF CELLS BACK TO Wb1

excelnat

New Member
Joined
May 8, 2014
Messages
12
Hi Guys,
In short, I need to search for a value in column A of a sheet in another workbook and copy that block of cells back to the first workbook.
The code is in Wb1. Search for a Reference number in column 'A' (only) of a specific (chosen) worksheet of Wb2. The specific worksheet name is in Wb1.Sheets("Screen").Range("H18"). The reference number I'm searching for is in Wb1.Sheets("Screen").Range("K18").
If the Reference number isn't found, I need a message box to say "the reference number isn't found and to check the reference number the user has entered (in K18) and try again."
Otherwise, I need to find the LAST occurrence of the Reference number, then copy/paste (or otherwise) the range of cells from A* to U*+7 (the * being the row number of the match e.g. A15:U22) to Wb1.Sheets("Copy").Range("A37:U44"), write the word "DONE" in Wb2.Sheet.V8 then save and close Wb2 and the code should end.
However, if the match already has "DONE" in column V*, I need a message to ask if the user would like to continue to paste the range A* to U*+7 to Wb1.
The code I have so far (Frankensteined from other's code!) is below. I'm not sure I need everything in there but I know I need additional code to make it work how I want. It currently searches though column A of the correct sheet but returns EVERY match but pastes the same matching ref into every cell of "A37:U44".
Thanks guys,
ExcelNat :) (P.S. Still can't figure out how to start a new paragraph in the post new thread editor!! :biggrin:)
Code:
Sub Copy_To_Another_Sheet_1()
    
Dim Wb1 As Workbook
Dim Wb2 As Workbook
Dim Wk As String
Dim RefN As String
Dim PasteTo As Range
Dim FirstAddress As String
Dim Rng As Range
Dim Rcount As Long
Dim Q As Long
    
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
Set Wb1 = Workbooks("Book1.xlsm")
RefN = Wb1.Sheets("Screen").Range("K18").Value
    If RefN = "" Then
       MsgBox "nothing to search for"
       End
    End If
Set PasteTo = Wb1.Sheets("COPY").Range("A37:U44")
Set Wb2 = Workbooks.Open("[URL="file://\\...OMITTED...\Book2.xlsm"]\\...OMITTED...\Book2.xlsm[/URL]", Password:="pass", _
            WriteResPassword:="pass", IgnoreReadOnlyRecommended:=True, UpdateLinks:=True)
Wk = Wb1.Sheets("Screen").Range("H18").Value
With Wb2.Sheets(Wk).Range("A:A")
        Rcount = 0
            
            'I need this to ONLY find the LAST occurrence
            
            Set Rng = .Find(What:=RefN, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                FirstAddress = Rng.Address
                Do
                
                Rcount = Rcount + 1
                    
'NEED SOMETHING IN HERE TO OFFSET/RESIZE/SELECT THE CELL WHERE THE MATCH HAS BEEN FOIND
'AND THE NEXT 20 COLUMNS AND 8 ROWS!!!
                    Rng.Copy PasteTo
                    
                    Set Rng = .FindNext(Rng)
                    
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            Else
            MsgBox "Reference number not found"
            End If
        
    End With
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,216,064
Messages
6,128,562
Members
449,458
Latest member
gillmit

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