VBA Loop Through to Create Specifically Ordered Array

DixiePiper

New Member
Joined
Oct 19, 2015
Messages
41
Office Version
  1. 365
Platform
  1. Windows
Is it possible to loop through a series of textboxes to create an array in a specific order?

In a userform, I have 17 textboxes named "txtG1" through "txtG17". I would like to loop through and build an array to pass to the workbook vs. passing each value independently. The code below loops through and passes the values but the order is wrong. I'm not sure what I need to do. I'm using Excel 2013 in a Windows 10 OS.

Code:
    For Each ctrl In Me.mpData.Pages(2).Controls
            Debug.Print TypeName(ctrl)
        If TypeOf ctrl Is MSForms.TextBox Then
            i = i + 1
            ReDim Preserve onPct(1 To i)
            onPct(i) = ctrl.Value
        End If
    Next ctrl
    
    Set destOn = data.Range("D28")
    Set destOn = destOn.Resize(UBound(onPct), 1)
    destOn.Value = Application.Transpose(onPct)

The result is that 17 values get pasted into column D, starting at D28 but the order is random. I need the array to be sequential from 1 through 17 so everything matches up.

I have to do this for two MultiPage pages for a total of 34. As such, I'd prefer to loop it. That and it helps me learn

Thanks in advance for any assistance.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
hi try

Code:
    Dim onPct(1 To 17) As Variant
    Dim i As Integer
    For i = 1 To 17
        onPct(i) = Me.mpData.Pages(2).Controls("txtG" & i).Value
    Next i
    
    Set destOn = Data.Range("D28")
    Set destOn = destOn.Resize(UBound(onPct), 1)
    destOn.Value = Application.Transpose(onPct)

Dave
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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