Updating Records Quickly using Arrays

ianawwalker

New Member
Joined
Feb 16, 2023
Messages
15
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello,

I am struggling to get my code to work and appreciate any help.

There are two worksheets that I am trying to compare to each other and update the row(s) (5 different cells in that row only), but only update if there is a "N" on the 'records' worksheet and my 'offline' worksheet has a "Y". I have loan numbers in my 'offline' worksheet that will match a specific cell in a row on the 'records' worksheet, then it only needs to update the 5 different cells if there is the "N" on the 'records' tab and my 'offline' tab has a 'Y'. The code below has it replace the whole row on the records tab with the offline tab, but don't want this to replace the whole row. I don't have much experience with arrays but am attempting to use the arrays to match the data faster. Let me know if you want or need additional details. Thank you in advance!

VBA Code:
Sub UpdateRecords()
    Dim offlineData As Variant
    Dim recordsData As Variant
    Dim postingDict As Object
    Dim i As Long
    Dim j As Long
    Dim numOfflineRows As Long
    Dim numRecordsRows As Long
    Dim loanNum As String
    Dim offlinerecord As Long
    
    ' Read data from sheets into arrays
    offlineData = Sheets("offline").Range("A3").Resize(, 65).Value
    recordsData = Sheets("Records").Range("StartSpot").Resize(, 65).Value
    
    ' Create a dictionary to store loan numbers from the offline data
    Set postingDict = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(offlineData, 1)
        loanNum = offlineData(i, 1)
        If Not postingDict.Exists(loanNum) Then
            postingDict.Add loanNum, i
        End If
    Next i
    
    ' Loop through the offline data and update records data
    numOfflineRows = UBound(offlineData, 1)
    numRecordsRows = UBound(recordsData, 1)
    For i = 1 To numOfflineRows
        loanNum = offlineData(i, 1)
        If postingDict.Exists(loanNum) Then
            offlinerecord = postingDict(loanNum) - 3
            For j = 1 To 65
                recordsData(offlinerecord, j) = offlineData(i, j)
            Next j
        Else
            numRecordsRows = numRecordsRows + 1
            ' Resize recordsData if needed
            If numRecordsRows > UBound(recordsData, 1) Then
                ReDim Preserve recordsData(1 To numRecordsRows, 1 To 65)
            End If
            For j = 1 To 65
                recordsData(numRecordsRows, j) = offlineData(i, j)
            Next j
        End If
    Next i
    
    ' Write updated records data to sheet
    Sheets("Records").Range("StartSpot").Resize(numRecordsRows, 65).Value = recordsData
    
    ' Clear contents of offline sheet
    Sheets("offline").Range("A3").Resize(numOfflineRows, 65).ClearContents
    
    ' Display message box to indicate completion
    MsgBox "Import complete."
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,981
Messages
6,175,767
Members
452,668
Latest member
mrider123

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