Add entry at the end of a list with a UserForm

Allan91

New Member
Joined
Dec 17, 2020
Messages
33
Office Version
  1. 2019
Platform
  1. Windows
Hi guys,

I am trying to write a code within a userform which can be used to enter data on 3 columns. The columns are F, G, H and the row start from row 4. But when I try to run the code I get an object error on ActiveSheet.Cells(lastrow + 1, 6).Value = TextBox1.Value. I'm new to VBA so the whole code could be wrong but I'm pretty sure there's a small fix I need. Please see an image of the userform and target range I have created as well. "TargetSheet" is a combo box I have created with items with names of sheets I want the data to be copied. CommandButton1 is the "Save Entry" button.

Thank you very much already!

VBA Code:
Private Sub CommandButton1_Click()

Targetsheet = ComboBox1.Value
If Targetsheet = "" Then
Exit Sub
End If
Worksheets(Targetsheet).Activate
lastrow = ActiveSheet.Cells(Rows.Count, 4).End(xlUp).Row
ActiveSheet.Cells(lastrow + 1, 6).Value = TextBox1.Value
ActiveSheet.Cells(lastrow + 1, 7).Value = TextBox2.Value
ActiveSheet.Cells(lastrow + 1, 8).Value = TextBox3.Value
MsgBox ("Data entry is successful!")

End Sub
 

Attachments

  • UserForm.PNG
    UserForm.PNG
    4.1 KB · Views: 3
  • Target Range.PNG
    Target Range.PNG
    7.9 KB · Views: 3

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,253
Office Version
  1. 365
Platform
  1. Windows
Do you have a textbox called textbox1 or have you renamed it?
 
Solution

Allan91

New Member
Joined
Dec 17, 2020
Messages
33
Office Version
  1. 2019
Platform
  1. Windows
Do you have a textbox called textbox1 or have you renamed it?
Yes that was my problem Fluff. Apparently my problem was the textbox name. Thank you very much! But this surfaced another problem. The code starts counting from row 1. I need it to start counting from row 4. But this is a different problem. Maybe I should start a new thread for this?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,253
Office Version
  1. 365
Platform
  1. Windows
As you are putting the values into col F why not use that column to find the next blank row
VBA Code:
lastrow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
 

Allan91

New Member
Joined
Dec 17, 2020
Messages
33
Office Version
  1. 2019
Platform
  1. Windows
As you are putting the values into col F why not use that column to find the next blank row
VBA Code:
lastrow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
You are right!. Thank you a bunch Fluff!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,253
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,126,997
Messages
5,622,081
Members
415,875
Latest member
Tarali

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
Top