Loop through Combobox

PKennedyG77

New Member
Joined
Jan 2, 2016
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hi,
Absolute amateur here.

I have created a form to allow information to be entered that will be put into a spreadsheet.

I am trying to loop through the comboboxes on the form to have the values assigned to a variable

Code is

Private Sub Cmdenter_Click()
Dim theDate As Date
Dim Member(1 To 50) As String
Dim MoneyIn(1 To 50) As Single
Dim c As Integer

TxtDate.Value = Format(TxtDate.Value, "dd-mm-yy")

theDate = TxtDate.Value

For c = 1 To 50
Member(c) = cboMember(c).Value

MoneyIn(c) = TxtMoneyIn(c).Value

MsgBox Member(c)
MsgBox MoneyIn(c)
Next c

End Sub


Getting an error on the combobox when running this.
No doubt something simple but cannot fix this.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi try this!
PS: your combobox is 0-based and your Members array is not

VBA Code:
Private Sub Cmdenter_Click()
 Dim Member(1 To 50) As String, c As Long
 For c = 1 To 50
   Member(c) = cboMember.List(c - 1)
 Next
End Sub


To compare give it a try, this would just write the combobox list to the variable "a".

VBA Code:
Private Sub Cmdenter_Click()
 Dim a
 a = cboMember.List
End Sub

And this is limited to 50 items:

VBA Code:
Private Sub Cmdenter_Click()
 Dim a
 a = Application.Index(cboMember.List, [row(1:50)], 0)
End Sub
 
Upvote 0
Thanks for your help.
I really appreciate it.

When I run the first code I am getting a runtime 424 Object Required error.
My combo boxes on the form are named cboMember1, cboMember2 and all the way up to 50.

I have checked spelling etc and all is OK
 
Upvote 0
Try it
VBA Code:
Private Sub Cmdenter_Click()
    Dim Member(1 To 50) As String
    Dim MoneyIn(1 To 50) As Single
    Dim c As Long

    For c = 1 To 50
        Member(c) = Me.Controls("cboMember" & c).Value
        MoneyIn(c) = Me.Controls("TxtMoneyIn" & c).Value

        MsgBox Member(c)
        MsgBox MoneyIn(c)
    Next c
End Sub
Artik
 
Upvote 0
Solution
Try it
VBA Code:
Private Sub Cmdenter_Click()
    Dim Member(1 To 50) As String
    Dim MoneyIn(1 To 50) As Single
    Dim c As Long

    For c = 1 To 50
        Member(c) = Me.Controls("cboMember" & c).Value
        MoneyIn(c) = Me.Controls("TxtMoneyIn" & c).Value

        MsgBox Member(c)
        MsgBox MoneyIn(c)
    Next c
End Sub
Artik
Worked perfectly.
Thanks
 
Upvote 0
Worked perfectly.
Thanks
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.

BTW, I suggest that you update your forum profile (click your user name at the top right of the forum, then ‘Account details’) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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