Loop Data By Rows

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
235
Office Version
  1. 365
Platform
  1. Windows
Hopefully this should be the last request for a while.

I have been using the following code to populate sequential textboxes in a UserForm where the each array is compiled of columns. In the following the sequence of cell values would be:-
For Draw 1 TxtBox1 = B5, TxtBox2 = C5, TxtBox3 = D5, TxtBox4 = E5, TxtBox5 = B6, TxtBox6 = C6 etc Through to Cell E8
For Draw 2 TxtBox1 = Y5, TxtBox2 = Z5, TxtBox3 = AA5, TxtBox4 = AB5, TxtBox5 = Y6, TxtBox6 = Z6 etc

VBA Code:
Option Explicit

Dim ws As Worksheet
Dim lngCtrlLoop As Long
Dim lngRowLoop As Long
Dim tbCounter As Long
Dim vCols As Variant
Dim vCol As Variant
Dim DrawToColsDict As Object

Private Sub userForm_Initialize()
    Set ws = Sheets("Sheet1")
End Sub

Private Sub cmdCallResult_Click()
    Set DrawToColsDict = CreateObject("Scripting.Dictionary")
    
        With DrawToColsDict
            .Add "Draw 1", Array("B", "C", "D", "E")
            .Add "Draw 2", Array("Y", "Z", "AA", "AB")
        End With
        With Me
                vCols = DrawToColsDict(.cboDrawNumber.Value)
            tbCounter = 1
                For lngRowLoop = 5 To 8
                    For Each vCol In vCols
                        .Controls("txtBox" & tbCounter).Text = ws.Cells(lngRowLoop, vCol).Text
                    tbCounter = tbCounter + 1
                    Next
                Next
        End With
End Sub

I am looking for similar code but where the informaton is held in rows so all the TextBoxes per "Draw" (Combox Value) will be populated from a single row
For Draw 1 TxtBox1 = B5, TxtBox2 = B6, TxtBox3 = B7 TxtBox4 = B8 TxtBox5 = B9 etc
For Draw 2 TxtBox1 = C5, TxtBox2 = C6, TxtBox3 = C7 etc

A similar solution would be most appreciated
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hopefully this should be the last request for a while.

I have been using the following code to populate sequential textboxes in a UserForm where the each array is compiled of columns. In the following the sequence of cell values would be:-
For Draw 1 TxtBox1 = B5, TxtBox2 = C5, TxtBox3 = D5, TxtBox4 = E5, TxtBox5 = B6, TxtBox6 = C6 etc Through to Cell E8
For Draw 2 TxtBox1 = Y5, TxtBox2 = Z5, TxtBox3 = AA5, TxtBox4 = AB5, TxtBox5 = Y6, TxtBox6 = Z6 etc

VBA Code:
Option Explicit

Dim ws As Worksheet
Dim lngCtrlLoop As Long
Dim lngRowLoop As Long
Dim tbCounter As Long
Dim vCols As Variant
Dim vCol As Variant
Dim DrawToColsDict As Object

Private Sub userForm_Initialize()
    Set ws = Sheets("Sheet1")
End Sub

Private Sub cmdCallResult_Click()
    Set DrawToColsDict = CreateObject("Scripting.Dictionary")
   
        With DrawToColsDict
            .Add "Draw 1", Array("B", "C", "D", "E")
            .Add "Draw 2", Array("Y", "Z", "AA", "AB")
        End With
        With Me
                vCols = DrawToColsDict(.cboDrawNumber.Value)
            tbCounter = 1
                For lngRowLoop = 5 To 8
                    For Each vCol In vCols
                        .Controls("txtBox" & tbCounter).Text = ws.Cells(lngRowLoop, vCol).Text
                    tbCounter = tbCounter + 1
                    Next
                Next
        End With
End Sub

I am looking for similar code but where the informaton is held in rows so all the TextBoxes per "Draw" (Combox Value) will be populated from a single row
For Draw 1 TxtBox1 = B5, TxtBox2 = B6, TxtBox3 = B7 TxtBox4 = B8 TxtBox5 = B9 etc
For Draw 2 TxtBox1 = C5, TxtBox2 = C6, TxtBox3 = C7 etc

A similar solution would be most appreciated
Please see Change (Typo "row" should have read as "column")

I am looking for similar code but where the informaton is held in Columns so all the TextBoxes per "Draw" (Combox Value) will be populated from a single Column
For Draw 1 TxtBox1 = B5, TxtBox2 = B6, TxtBox3 = B7 TxtBox4 = B8 TxtBox5 = B9 etc
For Draw 2 TxtBox1 = C5, TxtBox2 = C6, TxtBox3 = C7 etc
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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