Hi I have been trying to search for a solution for days to no avail. Basically below code should take a specific value (C2) from ThisWorkbook and find if it exists in Sample Db (Column D), if it doesn't exist, it will populate a range of data (Column A to AI) from ThisWorkbook to Sample Db, if it exist, it will update existing values in the same range in Sample Db. However I'm hitting error 91 at the dStartRow line and can't execute the main function of transferring data to Sample Db, could anyone help please?
VBA Code:
Sub Track()
Dim wb As Workbook, wsCopy As Worksheet, wsDest As Worksheet, i As Integer, j As Integer, cStartRow As Long, cEndRow As Long, dStartRow As Long, dEndRow As Long, rFndCell As Range, stFnd As String
Application.ScreenUpdating = False
Set wb = Workbooks.Open("C:\Users\xyz123\Desktop\Sample Tracker\Sample Db.xlsx")
Set wsCopy = ThisWorkbook.Worksheets("Summary")
Set wsDest = Workbooks("Sample Db.xlsx").Worksheets("Sample Db")
stFnd = wsCopy.Range("C2").Value
j = wsDest.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Row
cStartRow = wsCopy.Range("C:C").Find(what:=stFnd, after:=wsCopy.Range("C1"), LookIn:=xlValues).Row
cEndRow = wsCopy.Range("C:C").Find(what:=stFnd, after:=wsCopy.Range("C1"), LookIn:=xlValues, searchdirection:=xlPrevious).Row
dStartRow = wsDest.Range("D:D").Find(what:=stFnd, after:=wsDest.Range("D1"), LookIn:=xlValues).Row
dEndRow = wsDest.Range("D:D").Find(what:=stFnd, after:=wsDest.Range("D1"), LookIn:=xlValues, searchdirection:=xlPrevious).Row
With wsDest
Set rFndCell = .Range("D:D").Find(stFnd, LookIn:=xlValues)
For i = 2 To 5
If rFndCell Is Nothing And wsCopy.Cells(i, "C").Value <> "" Then
With wsDest
.Range("B" & j & ":AJ" & j).Value = wsCopy.Range("A" & i & ":AI" & i).Value
j = j + 1
End With
Else
With wsDest
.Range("V" & dStartRow & ":V" & dEndRow).Value = wsCopy.Range("U" & cStartRow & ":U" & cEndRow).Value
End With
End If
Next
wb.Save
wb.Close
End With
If rFndCell Is Nothing Then
MsgBox "Request has been submitted!", vbInformation, "Success"
Else
MsgBox "Information has been updated", vbInformation, "Success"
End If
Application.ScreenUpdating = True
End If
End Sub