Loop through userform controls

Magriza

Well-known Member
Joined
Nov 16, 2005
Messages
509
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I've got a little code that cycles through controls on my multipage userform to write values to a worksheet. All the data is captured correctly but not in the correct order! This is the code:
Code:
    For Each mpPage In Me.mpQuestions.Pages
        For Each frmControl In mpPage.Controls
            If frmControl.Name Like "part*Ans*" Then
                With ws
                    .Cells(lrow, i).Value = Format(frmControl.Name, "#")
                    i = i + 1
                End With
            End If
        Next frmControl
    Next mpPage
What I'm trying to ascertain is how Excel/VBA decides what order to loop through the controls. I'm getting part1Ans1a and 1b followed by part1Ans2b, then 2a, and then (for some unknown reason) part 17. I'd really like to get part1Ans2a before 2b and part17 in between 16 and 18!

I've changed the TabIndex property accordingly but that hasn't helped. So I'm turning to anyone here to help! Please, this is driving me nuts!

p.s. The reason I'm using
Code:
If frmControl.Name Like "part*Ans*" Then
is because I'm collecting data from a mixture of TextBoxes, Labels and ComboBoxes, but also omiting data from the same!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I think the order is the order in which they were created. But you should be able to get the value of i from the control's name.
 
Upvote 0
Thanks Andrew - I was worried that'd be the answer! Now to find a way to loop using the tabindex value....
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,034
Members
448,940
Latest member
mdusw

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