I am using a userform to update existing data on a worksheet. I can create new records just fine. I created an update userform with a combobox to search for the names. It pulls the persons data just fine and I am able to change the information. But when I go to click the update button, an error occurs. Before it was adding a totally new line which I did not want to happen so I adjusted my code. I just want to update an existing line of data with the edited information.
I have tried to use the MATCH function in VBA after it was replicating records.
Private Sub Update_record_Click()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Master")
Dim n As Long
Dim empname As String
empname = Application.Match(VBA.CStr(Me.Update_record.Value),
sh.Range("C:C"), 0)
sh.Range("A" & empname).Value = Me.First_Name.Value
sh.Range("B" & empname).Value = Me.Last_Name.Value
sh.Range("D" & empname).Value = Me.MainPX.Value
sh.Range("E" & empname).Value = Me.AltPX.Value
sh.Range("F" & empname).Value = Me.Job_Role.Value
sh.Range("G" & empname).Value = Me.WristBand.Value
sh.Range("H" & empname).Value = Me.Team.Value
sh.Range("I" & empname).Value = Me.Unit.Value
Range("A2:J" & n).Sort key1:=Range("A2:A" & n), order1:=xlAscending,
Header:=xlNo
Me.First_Name.Value = ""
Me.Last_Name.Value = ""
Me.MainPX.Value = ""
Me.AltPX.Value = ""
Me.Job_Role.Value = ""
Me.WristBand.Value = ""
Me.Team.Value = ""
Me.Unit.Value = ""
MsgBox "Record has been updated", vbInformation
End Sub
This is where the application is erroring out...It stops here....on this line
empname = Application.Match(VBA.CStr(Me.Update_record.Value),
sh.Range("C:C"), 0)
So the data never gets updated to the row. Below are two screen shots... one of the worksheet and one of the userform.


sh.RangequalifiedRangecalls, and then sort with unqualifiedRange("A2:J" & n).Sort Key1:=Range(...)...they should be qualified as well. ButMatchwill return aVariant/Errorif it doesn't find a match, and you're coercing that return value into aString-- I'm betting the error you're getting is a type mismatch.Application.AnyLateBoundWorksheetFunctioninto aVariant, then only proceed to treat that value as something usable whenNot IfError(thatVariantResult).Me.Update_Record.Valuelook like? Is it more like54204or more likeCA0041? If the former (i.e. a numeric ID), thenCStrcould be responsible forMatchfailing.