Updating existing data with UserForm

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
I can’t figure this out…<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Instead of replacing existing data, this code is adding a new row of data.

<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Code:
[FONT=Verdana][SIZE=1]Private Sub CommandButton1_Click()[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Dim iRow        As Long<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Dim WS          As Worksheet<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Dim FullName    As String<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Dim CLoc        As Range<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Set WS = Worksheets("Sheet1")<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]FullName = Me.ComboBox1.Value<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Set CLoc = WS.Columns("D:D").Find(What:=FullName, After:=WS.Cells(1, 4), LookIn:= _<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]                     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:= _<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]                     xlNext, MatchCase:=False, SearchFormat:=False)<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]If CLoc Is Nothing Then<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1] iRow = WS.Cells(Rows.Count, 1) _<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]        .End(xlUp).Offset(1, 0).Row<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Else<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1] iRow = CLoc.Row<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]End If<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]'copy the data to the database<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]WS.Cells(iRow, 1).Value = Me.TextBox14.Value<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]WS.Cells(iRow, 2).Value = Me.TextBox41.Value<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]WS.Cells(iRow, 3).Value = Me.TextBox42.Value<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]WS.Cells(iRow, 4).Value = Me.TextBox42.Value & " " & Me.TextBox41.Value<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]WS.Cells(iRow, 5).Value = Me.TextBox1.Value<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]WS.Cells(iRow, 6).Value = Me.TextBox2.Value<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]WS.Cells(iRow, 7).Value = Me.TextBox3.Value[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]'etc...etc...[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]'clear the data from the textboxes<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Me.ComboBox1.Value = ""<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Me.TextBox14.Value = ""<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Me.TextBox41.Value = ""<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Me.TextBox42.Value = ""<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Me.TextBox42.Value = ""<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Me.TextBox1.Value = ""<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Me.TextBox2.Value = ""<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Me.TextBox3.Value = ""[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]'etc...etc...<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]Range("A2:AZ" & iRow).Sort Key1:=Range("C2"), Order1:=xlAscending, Key2:=Range("B2"), Order1:=xlAscending, Header:= _<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]                        xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]                        DataOption1:=xlSortNormalHeader<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
[SIZE=1][FONT=Verdana]End Sub[/FONT][/SIZE]
<o:p></o:p>
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,

you are going the the last row then offsetting it by 1, try this instead:

Code:
 Dim iRow        As Long
Dim WS          As Worksheet
Dim FullName    As String
Dim CLoc        As Range
Set WS = Worksheets("Sheet1")
FullName = Me.ComboBox1.Value

Set CLoc = WS.Columns("D:D").Find(What:=FullName, After:=WS.Cells(1, 4), LookIn:= _
                     xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:= _
                     xlNext, MatchCase:=False, SearchFormat:=False)

If CLoc Is Nothing Then
 [U][B]iRow = WS.Cells(Rows.Count, 1) _
        .End(xlUp).Row[/B][/U]
Else
 iRow = CLoc.Row
End If

'copy the data to the database
WS.Cells(iRow, 1).Value = Me.TextBox14.Value
WS.Cells(iRow, 2).Value = Me.TextBox41.Value
WS.Cells(iRow, 3).Value = Me.TextBox42.Value
WS.Cells(iRow, 4).Value = Me.TextBox42.Value & " " & Me.TextBox41.Value
WS.Cells(iRow, 5).Value = Me.TextBox1.Value
WS.Cells(iRow, 6).Value = Me.TextBox2.Value
WS.Cells(iRow, 7).Value = Me.TextBox3.Value
'etc...etc...
 
Upvote 0
Code:
[FONT=Verdana][SIZE=1]Set CLoc = WS.Columns("D:D").Find(What:=FullName, After:=WS.Cells(1, 4), LookIn:= _<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]                   xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:= _<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]                   xlNext, MatchCase:=False, SearchFormat:=False)<o:p></o:p>[/SIZE][/FONT]
[FONT=Verdana][SIZE=1]<o:p></o:p>[/SIZE][/FONT]
<o:p></o:p>
Just a guess... try replacing the xlFormulas in the above code line with xlValues and see if that makes your code work.
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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