如何在表单中包含两个控制变量

时间:2016-07-29 20:01:07

标签: excel vba excel-vba

我有两个选择案例,取决于.value条件,如果它是真或假。如果.value = true然后使用没有扩展的情况,否则你的情况WITH扩展似乎工作但是我得到一个已经在使用的控制变量。如何使用多个控制变量?请参阅下面的代码

If onetimeoption.value = True Then

    Select Case MonthComboBox.value
        Case "Week One"
            iCol = "AY"

        Case "Week Two"
            iCol = "AZ"

        Case "Week Three"
            iCol = "BA"

        Case "Week Four"
            iCol = "BB"

        Case "Week Five"
            iCol = "BC"

        Case "Week Six"
            iCol = "BD"

        Case "One Seven"
            iCol = "BE"

        Case "One Eight"
            iCol = "BF"

        Case "One Nine"
            iCol = "BG"

        Case "One Ten"
            iCol = "BH"

        Case "One Eleven"
            iCol = "BI"

        Case "One Twelve"
            iCol = "BJ"
    End Select

Else



    nExtend = 1 'Set this as a default.
    Select Case MonthComboBox.value

        Case "Week One"
            iCol = "AY"
            nExtend = 12
        Case "Week Two"
            iCol = "AZ"
            nExtend = 11
        Case "Week Three"
            iCol = "BA"
            nExtend = 10
        Case "Week Four"
            iCol = "BB"
            nExtend = 9
        Case "Week Five"
            iCol = "BC"
            nExtend = 8
        Case "Week Six"
            iCol = "BD"
            nExtend = 7
        Case "One Seven"
            iCol = "BE"
            nExtend = 6
        Case "One Eight"
            iCol = "BF"
            nExtend = 5
        Case "One Nine"
            iCol = "BG"
            nExtend = 4
        Case "One Ten"
            iCol = "BH"
            nExtend = 3
        Case "One Eleven"
            iCol = "BI"
            nExtend = 2
        Case "One Twelve"
            iCol = "BJ"
            nExtend = 1
    End Select

End If

    actWarehouse = Me.WarehouseComboBox.ListIndex + 1

    With ws
        .Cells(irow, "A").value = Me.PartTextBox.value
        **For Each cel In .Cells(irow, iCol).Resize
        For Each cel In .Cells(irow, iCol).Resize(, nExtend)**
            cel.value = cel.value + CLng(Me.AddTextBox.value)
            cel.Interior.ColorIndex = 6
        Next cel

        Next cel

    End With

1 个答案:

答案 0 :(得分:2)

如果要使用两个for循环 - 只需更改目标变量名称

当然,我不知道你要对每个细胞做什么 - 所以检查你想如何使用cel1和cel2 - 我猜对了

 For Each cel1 In .Cells(irow, iCol).Resize

    For Each cel2 In .Cells(irow, iCol).Resize(, nExtend)
        cel1.value = cel2.value + CLng(Me.AddTextBox.value)
        cel1.Interior.ColorIndex = 6
    Next cel2

 Next cel1