VBA beginner needs assistance with ComboBox in a User Form

N

Nufk@mp

I'm trying to add a Custom User Form to a simple spreadsheet I use for
keeping track of my fuel consumption. The code for the user form is as
follows:-

Private Sub CommandButton1_Click()

Dim LastRow As Object

Set LastRow = Sheet1.Range("a65536").End(xlUp) '1st empty row

'textbox1 removed to insert date directly into cell

LastRow.Offset(1, 0).Value = Now() 'insert current date
LastRow.Offset(1, 1).Value = TextBox2.Value 'amount spent
LastRow.Offset(1, 2).Value = TextBox3.Value 'litres of fuel
LastRow.Offset(1, 3).Value = ComboBox1.Text 'where purchased
LastRow.Offset(1, 4).Value = TextBox4.Value 'mileage on clock

MsgBox "One entry made"
Response = MsgBox("Do you want to enter another?", _
vbYesNo)

If Response = vbYes Then
TextBox2.Text = ""
TextBox3.Text = ""
ComboBox1.Value = ""
TextBox4.Text = ""
End If

End Sub

Private Sub CommandButton2_Click()

Unload Me


End Sub

Private Sub UserForm1_Initialize()
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
With ComboBox1
.AddItem "Tesco" 'garage name
.AddItem "Esso - Gainsborough" 'garage name
.AddItem "Esso - Solar" 'garage name
.AddItem "Esso - Halstead" 'garage name
End With
ComboBox1.Value = ""

TextBox1.SetFocus
End Sub


The problem I've got is that when the User Form displays, I cannot get
the ComboBox to pick up the items I have set in the
UserForm1_Initialize() section, and the data from the last textbox is
therefore put into the wrong cell, it goes into the cell where the
ComboBox value should go.

Like I said I'm a total beginner at this, and I've been trying to
modify code that I've found on any number of tutorial sites. None of
the above code is mine from scratch.

Thanks for any assistance.
--

~~~~~~~
Nufkamp
~~~~~~~
And I can still see your Dad,
Running after me with a shovel in his hand.
I don't remember much after that.....
 
N

Nufk@mp

The name of the initializing sub should be:

UserForm_Initialize

That is, no "1"

Ah, I see. I should have spotted that but I think I'm going
'code-blind' what with the number of pages I've read this weekend.

Thank you.
--

~~~~~~~
Nufkamp
~~~~~~~
And I can still see your Dad,
Running after me with a shovel in his hand.
I don't remember much after that.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top