Writing data from UserForm to empty row and different colums

Pernella

New Member
Joined
Nov 22, 2018
Messages
2
Hello,

I am busy with make a code that we can use de UserForm, fill in the Form and the button OK will copy the information into the first empty row and in differen colums.

This is the code I Use:

Code:
Private Sub OK_Click()


Dim emptyRow As Long


'Make Sheet1 active
Blad5.Activate


'Determine emptyRow
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1


'Transfer information
Cells(emptyRow, 1).Value = NameTextBox.Value
Cells(emptyRow, 2).Value = PhoneTextBox.Value
Cells(emptyRow, 3).Value = CityListbox.Value
Cells(emptyRow, 4).Value = DinnerComboBox.Value


If DateCheckBox1.Value = True Then Cells(emptyRow, 5).Value = DateCheckBox1.Caption


If DateCheckBox2.Value = True Then Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & " " & DateCheckBox2.Caption


If DateCheckBox3.Value = True Then Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & " " & DateCheckBox3.Caption


If CarOptionButton1.Value = True Then
        Cells(emptyRow, 6).Value = "Yes"
    Else
        Cells(emptyRow, 6).Value = "No"
End If


Cells(emptyRow, 7).Value = MoneyTextBox.Value




End Sub

The only thing that willl be writing in the worksheet is the first row --> Name.

How can I be sure that all the 7 colums will be filled in one row?
 
Last edited by a moderator:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I can see nothing wrong with the code, although I would recommend changing this line
Code:
emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1
to
Code:
emptyRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row
If it's not working, then do you get any errors?
 
Upvote 0
I changed the recommended line, but also now only the first line (Name) will be written in the workbook.

This is the completed code:

Code:
Private Sub MoneySpinButton_Change()


MoneyTextBox.Text = MoneySpinButton.Value


End Sub




Private Sub Cancel_Click()


Unload Me


End Sub


Private Sub Clear_Click()


Call UserForm_Initialize


End Sub


Private Sub OK_Click()


Dim emptyRow As Long


'Make Sheet1 active
Blad5.Activate


'Determine emptyRow
emptyRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row




'Transfer information
Cells(emptyRow, 1).Value = NameTextBox.Value
Cells(emptyRow, 2).Value = PhoneTextBox.Value
Cells(emptyRow, 3).Value = CityListbox.Value
Cells(emptyRow, 4).Value = DinnerComboBox.Value


If DateCheckBox1.Value = True Then Cells(emptyRow, 5).Value = DateCheckBox1.Caption


If DateCheckBox2.Value = True Then Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & " " & DateCheckBox2.Caption


If DateCheckBox3.Value = True Then Cells(emptyRow, 5).Value = Cells(emptyRow, 5).Value & " " & DateCheckBox3.Caption


If CarOptionButton1.Value = True Then
        Cells(emptyRow, 6).Value = "Yes"
    Else
        Cells(emptyRow, 6).Value = "No"
End If


Cells(emptyRow, 7).Value = MoneyTextBox.Value




End Sub


Private Sub UserForm_Initialize()


'Empty NameTextBox
NameTextBox.Value = ""


'Empty PhoneTextBox
PhoneTextBox.Value = ""


'Empty CityListBox
CityListbox.Clear


'Fill CityListBox
With CityListbox
    .AddItem "San Francisco"
    .AddItem "Oakland"
    .AddItem "Richmond"
End With


'Empty DinnerComboBox
DinnerComboBox.Clear


'Fill DinnerComboBox
With DinnerComboBox
    .AddItem "Italian"
    .AddItem "Chinese"
    .AddItem "Frites and Meat"
End With


'Uncheck DataCheckBoxes
DateCheckBox1.Value = False
DateCheckBox2.Value = False
DateCheckBox3.Value = False


'Set no car as default
CarOptionButton2.Value = True


'Empty MoneyTextBox
MoneyTextBox.Value = ""


'Set Focus on NameTextBox
NameTextBox.SetFocus


End Sub
 
Last edited by a moderator:
Upvote 0
When posting code please use code tags, the # icon in the reply window.

Do you get any error messages?
 
Upvote 0
When posting code please use code tags, the # icon in the reply window.

Do you get any error messages?
Hello, hope you can help me.
When I use this code:

emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

When data are written into the table it skips one row e.g. first information is written in row 1, next information in written in row 3 and leaves row 2 blank.

When I use this code:

emptyRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row

It skips 5 rows for every entry
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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