I've got two workbooks, one named "LocalBooks" and another named "CentralIndex". All of the entries in the sheet "LocalBooks" have a unique reference number. I'm trying to write something that looks for a row in "CentralIndex" with that matching reference number and then updates specific columns in that row. (I do understand that a match and update entire row is a commonly asked question, but I couldn't find it for just updating specific columns in the row)
Workbook: "Localbooks" - Please assume first cell address is A1, sheet name is books
Workbook: "CentralIndex" - Please assume first cell address is A1, sheet name is Central Index
If my code runs correctly I'd like the "Central Index" to look like this:
With rows 2 (C2,E2,I2), 6 (C6,E6,I6) and 10 updated.
Considerations/Constraints
The above are sample sheets for my task, as I'm unable to share the actual data, but the actual sets are looking over 200+ rows.
There won't be any duplicate reference numbers in the "Central Index" sheet. So multiple matches aren't an issue.
I did contemplate using an array, but got stuck on holding the multiple column values from "Books" and then putting them in the different columns. If there is a way to do that then I welcome it.
I cannot use a classic index/match or other formula solution as the ask is to "Make update with press button" and I cannot amend the "central index" sheet.
In a very ideal world, I'd love for the code to also highlight any rows in "Local Books" that were not matched in the "Central Index". But as my code is very not working I hadn't got that far.
My code below uses the match function to find the row address, however when I go to run it, nothing seems to happen....
Sub Update()
Dim wbLocal As Workbook
Dim wbCentral As Workbook
Dim wsBooks As Worksheet
Dim wsCentral As Worksheet
Dim lrBooks As Long
Dim lrCentral As Long
Dim i As Long
Dim rc As Variant
Set wbLocal = Workbooks("LocalBooks.xlsx")
Set wbCentral = Workbooks("CentralIndex.xlsx")
Set wsBooks = wbLocal.Worksheets("Books")
Set wsCentral = wbCentral.Worksheets("Central Index")
lrBooks = wsBooks.Cells(wsBooks.Rows.Count, 1).End(xlUp).Row
lrCentral = wsCentral.Cells(wsCentral.Rows.Count, 1).End(xlUp).Row
For i = 2 To lrCentral
rc = Application.Match(wsCentral.Cells(i, 1).Value, wsBooks.Range("A1:A" & lrBooks), 0)
If Not IsError(rc) Then
wsBooks.Range("D").Select
Selection.Copy
Windows("CentralIndex.xlsx").Activate
wsCentral.Range("C").Select
ActiveSheet.Paste
Windows("LocalBooks.xlsx").Activate
End If
Next
End Sub
Debugging doesn't seem to pick anything up, so I haven't even been able to see if the copy paste part works either. (I'm aware that the current iteration of the copy paste won't get me the results above, I just wanted to see if what I'd done worked before using it for the other cells).
Happy to provide more info, and a giant thanks in advance. Promise I am learning so much from each question I ask :)