Hello,
I am new to Visual Basic and have put together the following code for a form to enter basic details into a worksheet in Excel. On the first worksheet page, I have a macro button which opens the VB form. The form works and the details are entered in my second worksheet when I press OK. However, I am having a problem with the Combo boxes as they don't show any of the values that I have entered in the code for the form.
Could someone please have a look at my code and tell me where I am going wrong.
Many thanks in advance.
The code I currently have is:
Private Sub UserForm1_Initialize()
With TitleListBox
TitleListBox.AddItem "Mr", 1
TitleListBox.AddItem "Mrs", 2
TitleListBox.AddItem "Ms", 3
TitleListBox.AddItem "Miss", 4
TitleListBox.AddItem "Dr", 5
End With
TitleListBox_Click() = TitleListBox.Value = ""
FirstNameBox_Click() = FirstNameBox.Value = ""
SurnameBox_Click() = SurnameBox.Value = ""
HouseBox_Click() = HouseBox.Value = ""
RoadBox_Click() = RoadBox.Value = ""
TownBox_Click() = TownBox.Value = ""
CityBox_Click() = CityBox.Value = ""
CountyBox_Click() = CountyBox.Value = ""
PostcodeBox_Click() = PostcodeBox.Value = ""
DOBBOX_Click() = DOBBOX.Value = ""
NIBox_Click() = NIBox.Value = ""
ContactNameBox_Click() = ContactNameBox.Value = ""
ContactTelephoneBox_Click() = ContactTelephoneBox.Value = ""
ContactMobileBox_Click() = ContactMobileBox.Value = ""
ContactAddressBox_Click() = ContactAddressBox.Value = ""
DateofVisitBox_Click() = DateofVisitBox.Value = ""
With DurationListBox
.AddItem "15", 1
.AddItem "30", 2
.AddItem "45", 3
.AddItem "60", 4
.AddItem "75", 5
.AddItem "90", 6
.AddItem "90+", 7
End With
DurationListBox.Value = ""
With VisitListBox
.AddItem "1", 1
.AddItem "2", 2
.AddItem "3", 3
.AddItem "4", 4
.AddItem "5", 5
.AddItem "6", 6
.AddItem "7", 7
.AddItem "8", 8
.AddItem "9", 9
.AddItem "10", 10
End With
VisitListBox.Value = ""
ReferredHeardBox_Click() = ReferredHeardBox.Value = ""
LivingWithBox_Click() = LivingWithBox.Value = ""
With RelationshipListBox
.AddItem "Husband/Wife"
.AddItem "Civil Partner"
.AddItem "Parent"
.AddItem "Child"
.AddItem "Sibling"
.AddItem "Friend"
.AddItem "Landlord"
End With
RelationshipListBox.Value = ""
With HouseIsListBox
.AddItem "Privately Owned"
.AddItem "Rented"
.AddItem "Council Property"
.AddItem "Housing Trust"
.AddItem "Shared Ownership"
End With
HouseIsListBox.Value = ""
IfOtherBox_Click() = IfOtherBox.Value = ""
FirstNameBox.SetFocus
End Sub
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub ClearButton_Click()
Call UserForm1_Initialize
End Sub
Private Sub OkButton_Click()
ActiveWorkbook.Sheets("Basic Information").Activate
Range("A2").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Offset(0, 1) = TitleListBox.Value
ActiveCell.Offset(0, 2) = FirstNameBox.Value
ActiveCell.Offset(0, 3) = SurnameBox.Value
ActiveCell.Offset(0, 4) = HouseBox.Value
ActiveCell.Offset(0, 5) = RoadBox.Value
ActiveCell.Offset(0, 6) = TownBox.Value
ActiveCell.Offset(0, 7) = CityBox.Value
ActiveCell.Offset(0, 8) = CountyBox.Value
ActiveCell.Offset(0, 9) = PostcodeBox.Value
ActiveCell.Offset(0, 10) = DOBBOX.Value
ActiveCell.Offset(0, 11) = NIBox.Value
ActiveCell.Offset(0, 12) = ContactNameBox.Value
ActiveCell.Offset(0, 13) = ContactTelephoneBox.Value
ActiveCell.Offset(0, 14) = ContactMobileBox.Value
ActiveCell.Offset(0, 15) = ContactAddressBox.Value
ActiveCell.Offset(0, 16) = DateofVisitBox.Value
ActiveCell.Offset(0, 17) = DurationListBox.Value
ActiveCell.Offset(0, 18) = VisitListBox.Value
ActiveCell.Offset(0, 19) = ReferredHeardBox.Value
ActiveCell.Offset(0, 22) = LivingWithBox.Value
ActiveCell.Offset(0, 23) = RelationshipListBox.Value
ActiveCell.Offset(0, 24) = HouseIsListBox.Value
ActiveCell.Offset(0, 25) = IfOtherBox.Value
If YesOptionButton = True Then
ActiveCell.Offset(0, 20).Value = "Yes"
Else: NoOptionButton = True
ActiveCell.Offset(0, 20).Value = "No"
End If
End Sub
I am new to Visual Basic and have put together the following code for a form to enter basic details into a worksheet in Excel. On the first worksheet page, I have a macro button which opens the VB form. The form works and the details are entered in my second worksheet when I press OK. However, I am having a problem with the Combo boxes as they don't show any of the values that I have entered in the code for the form.
Could someone please have a look at my code and tell me where I am going wrong.
Many thanks in advance.
The code I currently have is:
Private Sub UserForm1_Initialize()
With TitleListBox
TitleListBox.AddItem "Mr", 1
TitleListBox.AddItem "Mrs", 2
TitleListBox.AddItem "Ms", 3
TitleListBox.AddItem "Miss", 4
TitleListBox.AddItem "Dr", 5
End With
TitleListBox_Click() = TitleListBox.Value = ""
FirstNameBox_Click() = FirstNameBox.Value = ""
SurnameBox_Click() = SurnameBox.Value = ""
HouseBox_Click() = HouseBox.Value = ""
RoadBox_Click() = RoadBox.Value = ""
TownBox_Click() = TownBox.Value = ""
CityBox_Click() = CityBox.Value = ""
CountyBox_Click() = CountyBox.Value = ""
PostcodeBox_Click() = PostcodeBox.Value = ""
DOBBOX_Click() = DOBBOX.Value = ""
NIBox_Click() = NIBox.Value = ""
ContactNameBox_Click() = ContactNameBox.Value = ""
ContactTelephoneBox_Click() = ContactTelephoneBox.Value = ""
ContactMobileBox_Click() = ContactMobileBox.Value = ""
ContactAddressBox_Click() = ContactAddressBox.Value = ""
DateofVisitBox_Click() = DateofVisitBox.Value = ""
With DurationListBox
.AddItem "15", 1
.AddItem "30", 2
.AddItem "45", 3
.AddItem "60", 4
.AddItem "75", 5
.AddItem "90", 6
.AddItem "90+", 7
End With
DurationListBox.Value = ""
With VisitListBox
.AddItem "1", 1
.AddItem "2", 2
.AddItem "3", 3
.AddItem "4", 4
.AddItem "5", 5
.AddItem "6", 6
.AddItem "7", 7
.AddItem "8", 8
.AddItem "9", 9
.AddItem "10", 10
End With
VisitListBox.Value = ""
ReferredHeardBox_Click() = ReferredHeardBox.Value = ""
LivingWithBox_Click() = LivingWithBox.Value = ""
With RelationshipListBox
.AddItem "Husband/Wife"
.AddItem "Civil Partner"
.AddItem "Parent"
.AddItem "Child"
.AddItem "Sibling"
.AddItem "Friend"
.AddItem "Landlord"
End With
RelationshipListBox.Value = ""
With HouseIsListBox
.AddItem "Privately Owned"
.AddItem "Rented"
.AddItem "Council Property"
.AddItem "Housing Trust"
.AddItem "Shared Ownership"
End With
HouseIsListBox.Value = ""
IfOtherBox_Click() = IfOtherBox.Value = ""
FirstNameBox.SetFocus
End Sub
Private Sub CancelButton_Click()
Unload Me
End Sub
Private Sub ClearButton_Click()
Call UserForm1_Initialize
End Sub
Private Sub OkButton_Click()
ActiveWorkbook.Sheets("Basic Information").Activate
Range("A2").Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Offset(0, 1) = TitleListBox.Value
ActiveCell.Offset(0, 2) = FirstNameBox.Value
ActiveCell.Offset(0, 3) = SurnameBox.Value
ActiveCell.Offset(0, 4) = HouseBox.Value
ActiveCell.Offset(0, 5) = RoadBox.Value
ActiveCell.Offset(0, 6) = TownBox.Value
ActiveCell.Offset(0, 7) = CityBox.Value
ActiveCell.Offset(0, 8) = CountyBox.Value
ActiveCell.Offset(0, 9) = PostcodeBox.Value
ActiveCell.Offset(0, 10) = DOBBOX.Value
ActiveCell.Offset(0, 11) = NIBox.Value
ActiveCell.Offset(0, 12) = ContactNameBox.Value
ActiveCell.Offset(0, 13) = ContactTelephoneBox.Value
ActiveCell.Offset(0, 14) = ContactMobileBox.Value
ActiveCell.Offset(0, 15) = ContactAddressBox.Value
ActiveCell.Offset(0, 16) = DateofVisitBox.Value
ActiveCell.Offset(0, 17) = DurationListBox.Value
ActiveCell.Offset(0, 18) = VisitListBox.Value
ActiveCell.Offset(0, 19) = ReferredHeardBox.Value
ActiveCell.Offset(0, 22) = LivingWithBox.Value
ActiveCell.Offset(0, 23) = RelationshipListBox.Value
ActiveCell.Offset(0, 24) = HouseIsListBox.Value
ActiveCell.Offset(0, 25) = IfOtherBox.Value
If YesOptionButton = True Then
ActiveCell.Offset(0, 20).Value = "Yes"
Else: NoOptionButton = True
ActiveCell.Offset(0, 20).Value = "No"
End If
End Sub