Code to transfer data entered in userform to table

Channce

New Member
Joined
Jan 22, 2019
Messages
8
Hello all...I used to think I was great with excel until I started making VBA userforms. I've created the form and the button to show the form (which does work), but I can't seem to figure out the code to transfer the data from each field to the respective row in the table. It shows I don't have permission to attach screenshots, so I attached a link to them here...

screenshot of table in excel --- https://drive.google.com/open?id=1NqL1KN7iXpkZTq6w-9uwF3YmpVbrDjKP
screenshot of userform --- https://drive.google.com/open?id=1qp8ip1ok9ObOnxQZcGth9LO-QgNCI8bK

What I need specificially...
- All fields EXCEPT the checkbox need to have a value entered or it should present a msgbox saying "You must enter something in every field before saving."
- When you press save item, I need the data to transfer to the next available row and fill into the respective fields. (I know I haven't provided the exact names of each text box but I can fill them in).
- After entry has been saved, I would like the form to close.

I will also be creating another form to pull up an existing entry that will pull up the existing info on that row about the item, and have the ability to change that and save which will over write that data on that row... I haven't started on that yet, because I wanted to try this first.

Any help would be much appreciated!!
-Chance
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
It would make it easier to test possible solutions if you could attach a link to your file rather than a screen shot.
 
Upvote 0
Place this macro in the code module for cmdSaveItem command button.
Code:
Private Sub cmdSaveItem_Click()
    Dim ctrl As Control, LastRow As Long
    LastRow = Sheets("Item").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Or TypeName(ctrl) = "ComboBox" Then
            If ctrl.Value = "" Then
                MsgBox ("Please enter a value for " & ctrl.Name)
                ctrl.SetFocus
                Exit Sub
            End If
        End If
    Next ctrl
    Sheets("Item").Cells(LastRow, 1).Resize(1, 7) = Array(txt_ItemName.Value, _
        txt_ProdCodeSKU.Value, txt_VendorSelection.Value, txt_Location, txt_MaxStock.Value, txt_ReorderLevel.Value, txt_ItemStocked.Value)
End Sub
 
Upvote 0
Mumps, you're a life saver! Upon saving, can we also clear the data from the form?

It seems to be overwriting on the same line on every save rather than entering new data to the next line.
 
Upvote 0
Try:
Code:
Private Sub cmdSaveItem_Click()
    Dim ctrl As Control, LastRow As Long
    LastRow = Sheets("Item").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Or TypeName(ctrl) = "ComboBox" Then
            If ctrl.Value = "" Then
                MsgBox ("Please enter a value for " & ctrl.Name)
                ctrl.SetFocus
                Exit Sub
            End If
        End If
    Next ctrl
    Sheets("Item").Cells(LastRow + 1, 1).Resize(1, 7) = Array(txt_ItemName.Value, _
        txt_ProdCodeSKU.Value, txt_VendorSelection.Value, txt_Location, txt_MaxStock.Value, txt_ReorderLevel.Value, txt_ItemStocked.Value)
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "TextBox" Or TypeName(ctrl) = "ComboBox" Then
            ctrl.Value = ""
        End If
    Next ctrl
    txt_ItemStocked = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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