VBA and Userform with button to add additional fields

STEVEMILLS04

Board Regular
Joined
Oct 8, 2009
Messages
113
To provide me with a way to learn VBA I am creating an exercise workbook. I have a form setup with three initial fields, a textbox that tracks an action ID, dropdown with pre-defined list for action type and finally, action duration. The idea is I would enter an exercise, such as "Run for 2 minutes", then click the "Add New Action" button to duplicate the previous fields to enter a new exercise. The ID would increment by 1 with the ability to enter as many of these "actions" as necessary. Then, once done, append the data to a table for reference.

I looked into creating a Class Module, would this be the way to go and will it meet my needs? Not really sure where to start so I just need a little push.

Any help is appreciated.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
.
Not certain how much of this project will suffice but take a look :

Download workbook : https://www.amazon.com/clouddrive/s...?_encoding=UTF8&*Version*=1&*entries*=0&mgh=1

Code:
Option Explicit
Private Sub btnCancel_Click()
    Unload Me
End Sub


Private Sub btnOK_Click()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim newRow As Long
    
    newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1
    
    'The next two lines can be expanded as many times as needed for all the entry fields in your project
    
    ws.Cells(newRow, 1).Value = Me.txtFirstName.Value
    ws.Cells(newRow, 2).Value = Me.txtSurname.Value
    ws.Cells(newRow, 3).Value = Me.TextBox1.Value
    ws.Cells(newRow, 4).Value = Me.TextBox2.Value
    
    Me.txtFirstName.Value = ""
    Me.txtSurname.Value = ""
    Me.TextBox1.Value = ""
    Me.TextBox2.Value = ""
    
End Sub
Sub CommandButton1_Click()
    Selection.EntireRow.Delete
End Sub


Private Sub CommandButton2_Click()
    Me.TextBox2.Value = Now
End Sub


Private Sub UserForm_Initialize()
    Me.TextBox2.Value = Now
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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