ADD / EDIT button not working correctly

Phillip2

Board Regular
Joined
Aug 5, 2019
Messages
79
Office Version
  1. 365
Platform
  1. Windows
I am having a problem with my Add/Edit Button.

What I want is if the textbox for my Record Number (TBrecord) has a value then I want it to go and edit that Record.
If the textbox doesn't have a value I want it to go to the lastRow available in my data worksheet and create a new Record number.

However, what it is doing is getting the value and going to that line number and then replacing the incorrect record.


VBA Code:
___________________________Add / Edit Record Button____________________________________________

Private Sub Add_Click()

If TBrecord.Value = "" Then
    LastRow = Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
    EditRow = LastRow + 1
    TBrecord = Format(EditRow.Value, "000")

Else: EditRow = TBrecord.Value
End If

'____________Information Frame________________________

Cells(EditRow, 1).Value = TBrecord.Value
Cells(EditRow, 2).Value = TBccxName.Text
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Is this what you want
VBA Code:
Private Sub Add_Click()
    Dim cel As Range, rng As Range, ws As Worksheet, v As Long, editRow As Long
    Set ws = Worksheets("Data")
    Set rng = ws.Range("A:A")
    
    On Error Resume Next
    v = TBrecord.Value
    If v > 0 Then editRow = rng.Find(v, lookat:=xlWhole).Row
    If editRow = 0 Then
        Set cel = rng.Find("*", searchdirection:=xlPrevious)
        editRow = cel.Row + 1
        TBrecord.Value = cel.Value + 1
    End If
    

'____________Information Frame________________________
    
    ws.Cells(editRow, 1).Value = TBrecord.Value
    ws.Cells(editRow, 2).Value = TBccxName.Text
    TBrecord = ""
    TBccxName = ""
End Sub
Add1.jpg

Add2.jpg

Add3.jpg

Add4.jpg
 
Upvote 0
Yongle, I believe this is exactly what I need. Thank you so much.
 
Upvote 0

Forum statistics

Threads
1,215,697
Messages
6,126,269
Members
449,308
Latest member
VerifiedBleachersAttendee

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