Search code examples
excelvbauserform

Overwrite data being sent to table if it exists already?


I have a script that sends data from a userform to a table on a network drive. I also have code to populate that table data back in the form for users to make edits. Say i have an existing entry, pull the data to make updates, how would make sure it overwrites an already existing entry instead of appending extra rows? Could I implement an if statement to check if it exists already?

EDITED CODE:

Private Sub cmdSendData_Click()

    Set wb = Workbooks.Open("\\\OFFER_LOG_DATA_TABLE.xlsx")
    Dim wsTgt As Worksheet: Set wsTgt = wb.Worksheets("Sheet1")
    Dim recRow As Range

    'See if there's a match on an existing row
    '  adjust function to suit...
    Set recRow = MatchRow(wsTgt.Range("A1").CurrentRegion, _
                          txtCandidateName.Text, _
                          txtCurrentPosition.Text)

    'If there's no existing row to update then add a new row at the bottom
    If recRow Is Nothing Then Set recRow = wsTgt.Range("A50000").End(xlUp).Offset(1, 0)

    With recRow.EntireRow
        .Cells(1).Value = txtTodays_Date.Text 'section 1
        .Cells(2).Value = Me.cmbReason_for_Offer.Value
        .Cells(33).Value = txtMgrJustification.Text
    End With
        
    
    wb.Close savechanges:=True
    Application.Quit   '????
    wb.Saved = True
        
End Sub

'Return a row from a table based on matches in two columns
'   returns nothing if no match
Function MatchRow(tableRange As Range, lStore, lName) As Range
    Dim rw As Range
    lStore = Me.txtStore.Text
    lName = Me.txtCandidateName.Text
    For Each rw In tableRange.Rows
        'adjust the column numbers/match types as needed
        If rw.Cells(4).Value = lStore Then
            If rw.Cells(16).Value = lName Then
                Set MatchRow = rw
                Exit Function
            End If
        End If
    Next rw
End Function

Solution

  • Should look something like this:

    Private Sub cmdSendData_Click()
    
        Set wb = Workbooks.Open("\\TABLE.xlsx")
        Dim wsTgt As Worksheet: Set wsTgt = wb.Worksheets("Sheet1")
        Dim recRow As Range 
    
        'See if there's a match on an existing row
        '  adjust function to suit...
        Set recRow = MatchRow(wsTgt.Range("A1").CurrentRegion, _
                              txtCandidateName.Text, _
                              txtCurrentPosition.Text)
    
        'If there's no existing row to update then add a new row at the bottom 
        If recRow is nothing then set recRow = wsTgt.Range("A50000").End(xlUp).Offset(1, 0)
    
        With recRow.EntireRow          
            .cells(1).Value = txtTodays_Date.Text 'section 1
            .cells(2).Value = Me.cmbReason_for_Offer.Value
            '....
            .cells(33).Value = txtMgrJustification.Text
        End With
            
        
        wb.Close savechanges:=True
        Application.Quit   '????
        wb.Saved = True
            
    End Sub
    
    'Return a row from a table based on matches in two columns
    '   returns nothing if no match
    Function MatchRow(tableRange As Range, match1, match2) As Range
        Dim rw As Range
        For Each rw In tableRange.Rows
            'adjust the column numbers/match types as needed
            If rw.Cells(1).Value = match1 Then
                If rw.Cells(3).Value = match2 Then
                    Set MatchRow = rw
                    Exit Function
                End If
            End If
        Next rw
    End Function
    

    Whatever code you have to load the existing record should keep track of which row it came from, or you will need some method to re-find the row when you save the record later.