Excel循环VBA递增行

时间:2016-09-28 13:56:32

标签: excel vba loops

您好我有一段代码,我正在尝试编写循环但我正在努力解决它。

这部分代码运行正常。但实际上我有4个细胞,它们是C26,C91,C156和C221。 (参见代码中的容器1注释)

我设法让它循环,但接下来我的参考文献(例如B33,C33,D33等)只是写在顶部。反正有没有写一个循环,可以通过所需的65行增加所有连续代码??

我真的想学习如何正确地做到这一点,而不是复制和粘贴4次并手动更新参考文献!

Private Sub RunStabSetup()


' Confirmation of Entry to Form

If MsgBox("Have you double checked your data is correct and ALL test points have been selected before entering on the spreadsheet?", vbYesNo) = vbNo Then Exit Sub

Application.ScreenUpdating = False

Application.Worksheets("Req Sheet").Range("C83") = " "

If Container1CB.Value > "" Then

'Container 1

    Application.Worksheets("StabDataCapture").Range("C26") = Container1CB

  '60° CheckBox logic statements

    If W1T60.Value = True Then Application.Worksheets("StabDataCapture").Range("B33") = "1"
    If W1T60.Value = False Then Application.Worksheets("StabDataCapture").Range("B33") = ""

    If W2T60.Value = True Then Application.Worksheets("StabDataCapture").Range("C33") = "2"
    If W2T60.Value = False Then Application.Worksheets("StabDataCapture").Range("C33") = ""

    If W3T60.Value = True Then Application.Worksheets("StabDataCapture").Range("D33") = "3"
    If W3T60.Value = False Then Application.Worksheets("StabDataCapture").Range("D33") = ""

    If W4T60.Value = True Then Application.Worksheets("StabDataCapture").Range("E33") = "4"
    If W4T60.Value = False Then Application.Worksheets("StabDataCapture").Range("E33") = ""

    If W5T60.Value = True Then Application.Worksheets("StabDataCapture").Range("F33") = "5"
    If W5T60.Value = False Then Application.Worksheets("StabDataCapture").Range("F33") = ""

    If W6T60.Value = True Then Application.Worksheets("StabDataCapture").Range("G33") = "6"
    If W6T60.Value = False Then Application.Worksheets("StabDataCapture").Range("G33") = ""

    If W7T60.Value = True Then Application.Worksheets("StabDataCapture").Range("H33") = "7"
    If W7T60.Value = False Then Application.Worksheets("StabDataCapture").Range("H33") = ""

    If W8T60.Value = True Then Application.Worksheets("StabDataCapture").Range("I33") = "8"
    If W8T60.Value = False Then Application.Worksheets("StabDataCapture").Range("I33") = ""
 End If

End Sub

谢谢你帮助大家!

2 个答案:

答案 0 :(得分:1)

我做得像:

i=2
do while i<= maxColumn 
        If W1T60.Value = True Then Application.Worksheets("StabDataCapture").Cells(i,33).Value2 = i-1
        If W1T60.Value = False Then Application.Worksheets("StabDataCapture").Cells(i,33).Value2 = ""
loop

从您的代码我不知道如何更改单元格(i, j )参数,所以我保持不变,但是使用类似的逻辑可以修改它

答案 1 :(得分:0)

使用for循环和偏移功能可以使用几种不同的方法。我可能会首先将您的范围定义为范围数组。 Dim rng(0 to 3) as Range然后定义C列中的每个4个单元格。

Set rng(0) = Range("C26")
Set rng(1) = Range("C91")
Set rng(2) = Range("C156")
Set rng(3) = Range("C221")

然后你可以在每个循环中包含你的“if”语句。

Dim c As Variant
For Each c In rng
    if Container1CB.Value > "" Then

        Sheets("StabDataCapture").c.Value = Container1CB

        If W1T60.Value = True Then Sheets("StabDataCapture").c.Offset(7,-1).Value = "1"
        If W1T60.Value = False Then sheets("StabDataCapture").c.Offset(7,-1).Value = ""

        If W2T60.Value = True Then sheets("StabDataCapture").c.Offset(7,0).Value = "2"
        If W2T60.Value = False Then sheets("StabDataCapture").c.Offset(7,0).Value = ""

....

end if

或者,您可以使用For i = 0 to 65*4 Step 65之类的for循环,并且可以将Range("C26")等语句替换为Cells(i,3).Value

要在“IF-THEN”语句中设置每个值,您的最​​佳解决方案可能是一个数组。 Dim WT(1 To 8) as Variant然后您可以将数组的每个值设置为等于W1T60,W2T60等的值WT(1) = W1T60.Value。然后代码可以更新为:

Dim c As Variant
Dim i as Integer
For Each c In rng
    if Container1CB.Value > "" Then

        Sheets("StabDataCapture").c.Value = Container1CB

        For i = 1 To 8
            If WT(i) Then 
                Sheets("StabDataCapture").c.Offset(7, i - 2).Value = i
            else
                Sheets("StabDataCapture").c.Offset(7, i - 2).Value = ""
            end if
        next i

    End If
Next