简体   繁体   English

Excel VBA 带 3 个嵌套循环

[英]Excel VBA with 3 nested loops

I want to give Multiple Checkboxes within my userform a caption coming from a cell.我想在我的用户窗体中为多个复选框提供来自单元格的标题。 Since I don't want to write down every single reference, I would like to use a loop.因为我不想写下每一个参考,我想使用一个循环。 Unfortunately, I have no clue how to do that.不幸的是,我不知道该怎么做。

I have 8 checkboxes per Workday and 23 Workdays in total.我每个工作日有 8 个复选框,总共有 23 个工作日。 The naming is like whis WD1_1, WD1_2... WD1_8, WD2_1, WD2_2... WD2_8.命名类似于 WD1_1、WD1_2...WD1_8、WD2_1、WD2_2...WD2_8。

WD1_1 get's the value from the tab "tasks" cell B2. WD1_1 从选项卡“任务”单元格 B2 中获取值。 From there on, it is always +1.从那以后,它总是+1。

The issue I have is, that I can't just loop until 185, since three things are changing: Count up the task number and the cell number, when task 8 in WD1 is reached count up WD +1 and restart task number from 1 to 8 and also keep counting up the cell reference.我遇到的问题是,我不能一直循环到 185,因为三件事正在发生变化:计算任务编号和单元格编号,当达到 WD1 中的任务 8 时,向上计数 WD +1 并从 1 重新启动任务编号到 8 并继续计数单元格引用。

I just get that going for one instance, for example the cell reference, but not all three together.我只是在一个实例中做到这一点,例如单元格引用,但不是所有三个都在一起。

This is what I have to loop through the Tasks per WD, but I can't figure out how to count up the Workdays and also the cell number:这是我必须遍历每个 WD 的任务,但我不知道如何计算工作日以及单元格编号:

 For x = 1 To 23
 For i = 1 To 8
 For y = 2 To 185
        
      If Controls("WD" & x & "_" & i).Caption = "" Then
        Controls("WD" & x & "_" & i).Visible = False
        Else
        Controls("WD" & x & "_" & i).Visible = True
      End If
    
      If Sheets("Tasks").Range("I" & y) = 1 Then
        Controls("WD" & x & "_" & i).Value = True
        Else
        Controls("WD" & x & "_" & i).Value = False
      End If

 Next y, i, x

Thanks in advance for any help!提前感谢您的帮助!

Found the answer.找到了答案。 My error was to go through a third loop.我的错误是通过第三个循环到达 go 。 Deleting the third loop and then use a linear function to go through each line is the right way to do it:删除第三个循环,然后通过每一行使用线性 function 到 go 是正确的方法:

 Dim x As Integer Dim i As Integer For x = 1 To 23 For i = 1 To 8 If Controls("WD" & x & "_" & i).Caption = "" Then Controls("WD" & x & "_" & i).Visible = False Else Controls("WD" & x & "_" & i).Visible = True End If If Sheets("Tasks").Range("I" & (x - 1) * 8 + i + 1).Value = 1 Then Controls("WD" & x & "_" & i).Value = True Else Controls("WD" & x & "_" & i).Value = False End If Next i, x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM