Transfer Data from Userform

Robby19

Board Regular
Joined
Mar 19, 2018
Messages
227
I have a Userform that transfers data, sort of. I need to change the code, as it doesn't do what I need it to do. What ends up happening is that it creates a new row at the end of the table (good!). What it doesn't do, is add the userform data to that new row, but instead overwrites the data in row 5 (bad!). My table starts on A3 (headers), data starts on A4

I also need code to create a unique transaction #. As simple as 1, 2, 3, etc... that is automatically added when the user clicks the Save button. The transaction # should NOT be reset if a user deletes a row. For example, table shows Transaciton # 1, 2, 3. User deletes transaction 2 and then adds a new line of data using the form. This new line of data should be transaction 4. Transaction 2 is just gone forever.

The transaction # should be in Row A.

Below is my current code;
Code:
 ' Transfer Userform Data
 With Worksheets("Bookkeeping").Range("A4")
    .ListObject.ListRows.Add
    .Offset(1, 1).Value = ExpIncDrop.Value
    If ExpIncDrop.Value = "Expense" Then
     .Offset(1, 2).Value = TypeDrop.Value
    Else
     .Offset(1, 3).Value = TypeDrop.Value
    End If
    .Offset(1, 4).Value = DateTxt.Value
    .Offset(1, 5).Value = AmountTxt.Value
    .Offset(1, 6).Value = RemarksTxt.Value
 End With
 
Last edited:
Figured out the problem.
Code:
rng.Cells(lrow, 4).Value = TypeDrop.Value
Should be
Code:
rng.Cells(1, 4).Value = TypeDrop.Value
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Final Save Button code;
Code:
Private Sub SaveCloseBtn_Click()
'Modified  9/21/2018

If Not IsDate(Me.DateTxt.Text) Then
    MsgBox "The Data entered is not a Date" & Chr(10) & "Please re-enter in the following format, m/dd/yyyy"
    Exit Sub
End If

' Check to see if Reason for Editing is blank
If Trim(ReasEditTxt.Text) <> "" Then

Dim fCell As Range
Dim idx As Long

idx = TransTxt

If idx = -1 Then Exit Sub

Set rng = Sheets("Bookkeeping").ListObjects("Bookkeeping").ListRows(idx + 1).Range
    
    ' Transfer Userform Data to Table Bookkeeping
    rng.Cells(1, 2).Value = Me.ExpIncDrop.Value
    If ExpIncDrop.Value = "Expense" Then
        rng.Cells(1, 3).Value = TypeDrop.Value
    Else
        rng.Cells(1, 4).Value = TypeDrop.Value
    End If
    rng.Cells(1, 5).Value = Me.DateTxt.Value
    rng.Cells(1, 6).Value = Me.AmountTxt.Value
    rng.Cells(1, 7).Value = Me.RemarksTxt.Value
    rng.Cells(1, 8).Value = Me.ReasEditTxt.Value
    rng.Cells(1, 9).Value = Now()
    
Else
MsgBox "Reason for Editing cannot be blank."
Exit Sub
End If

' Worksheet Protection
ThisWorkbook.ActiveSheet.Protect Password:="", AllowFiltering:=True

Unload Me

End Sub
 
Upvote 0
That method will probably fail when transactions are deleted, you'd be better off searching for the row you need

Code:
Set rng = Sheets("Bookkeeping").ListObjects("Bookkeeping").ListColumns(1).Range.Find(TransTxt, LookIn:=xlValues, lookat:=xlWhole)

and then offset from there

Code:
rng.offset(,1).Value = Me.ExpIncDrop.Value
 
Upvote 0
What if transactions are never deleted?

For example, instead of deleting Transaction #2 , I could use the Edit Userform, clear the information and save it. That way Transaction 2 is still there, and the only cells that have data are the Reason for Editing (text = "Deleted") and Edited Date
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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