excel 按列循环数据

irtuqstp  于 2023-01-14  发布在  其他
关注(0)|答案(1)|浏览(175)

按列循环数据
我一直在使用下面的代码填充UserForm中的连续文本框,其中每个数组都是由列编译而成的。在下面的代码中,单元格值的顺序是:-

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

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 14
                    For Each vCol In vCols
                        .Controls("txtBox" & tbCounter).Text = ws.Cells(lngRowLoop, vCol).Text
                    tbCounter = tbCounter + 1
                    Next
                Next
        End With
End Sub

我正在寻找信息保存在列中的代码,以便每次"绘制"的所有文本框(组合框值)将从单个列填充。对于绘制1,TxtBox1= B5、TxtBox2 = B6、TxtBox3 = B7、TxtBox4 = B8、TxtBox5 = B9等。对于绘制2,TxtBox1= C5、TxtBox2 = C6、TxtBox3 = C7等。
类似的解决方案将是最受欢迎的
注意:我已经尝试在这个查询中使用"代码"选项格式化示例代码,但是,这不会格式化所有代码。

ar7v8xwq

ar7v8xwq1#

Option Explicit

Private Sub cmdCallResult_Click()

    Dim DrawToColsDict As Object, ws As Worksheet
    Dim vCol As Variant, tbCounter As Long

    Set DrawToColsDict = CreateObject("Scripting.Dictionary")
    With DrawToColsDict
        .Add "Draw 1", "B"
        .Add "Draw 2", "C"
    End With
    
    Set ws = Sheets("Sheet1")
    With Me
        vCol = DrawToColsDict(.cboDrawNumber.Value)
        For tbCounter = 1 To 16
            .Controls("txtBox" & tbCounter).Text = ws.Cells(tbCounter + 4, vCol).Text
        Next
    End With
End Sub

相关问题