Excel VBA:For循环在For块外执行代码

Excel VBA:For循环在For块外执行代码,excel,vba,Excel,Vba,我有一个大的For循环,用来创建一个变体数组。当我试图从我的一个sub调用函数时,我得到了“运行时错误'28':堆栈空间不足” 函数本身完成时没有错误,但是,我注意到,当我在for循环外print.debug(“complete”)时,它会不断地将“complete”打印到即时窗口。我复制并粘贴了从一次函数调用生成的“complete”,结果打印了199张“complete” for循环应该只运行50次迭代。它也不应该在for块之外的代码上进行迭代(至少我不希望它进行迭代) 我担心我无意中创建了

我有一个大的For循环,用来创建一个变体数组。当我试图从我的一个sub调用函数时,我得到了“运行时错误'28':堆栈空间不足”

函数本身完成时没有错误,但是,我注意到,当我在for循环外print.debug(“complete”)时,它会不断地将“complete”打印到即时窗口。我复制并粘贴了从一次函数调用生成的“complete”,结果打印了199张“complete”

for循环应该只运行50次迭代。它也不应该在for块之外的代码上进行迭代(至少我不希望它进行迭代)

我担心我无意中创建了一个无限循环

双重问题:

  • 代码中是否存在可能导致此错误的错误
  • 是否有某种方法来表示For块中应该包含什么以及For块之外应该包含什么
  • 任何帮助都将不胜感激

    以下代码的作用:

    • 创建一个50行乘4列的多维数组
    • 迭代for循环,将月/季度/年分配给第1列
    • 为第2列中的单选按钮指定一个用作标志的字符串
    • 为要在第3列中显示的节指定起始列
    • 为要在第4列中显示的节指定结束列
    另一个子函数调用该函数,该函数取消隐藏从第0行的开始列到第50行的结束列的所有内容。然后,它只显示在数组中月份和单选按钮标志匹配的条目的坐标

    Function a_pane_coords() As Variant
        'Will hold the visible column numbers for each month in the inputs pane and returns the result as an array.
    
        Dim months As Variant
        'Multidimensional array; 12 rows, 4 columns
        'Column 0 = month name; column 1 = radio button flag; column 2 = start_row for month; column 3 = end_row for month.
        Dim a_pane_cols(0 To 50, 0 To 3)
        Dim in_month_arr As Variant
        Dim quarters As Variant  'Will hold an array
        Dim start_col As Integer
        Dim end_col As Integer
        Dim visible_cols As Integer
        Dim space_cols As Integer
        Dim month_counter As Integer
        Dim quarter_counter As Integer
        Dim irow As Integer
        Dim icol As Integer
    
        'Radio button flags assigned to column 1:
        '  current month:    percent = m_pct;  hours = m_hrs;  financials = m_fin
        '  current quarter:  percent = q_pct;  hours = q_hrs;  financials = q_fin
        '  fiscal year:      percent = y_pct;  hours = y_hrs;  financials = y_fin
    
        'This variable is the number of columns that will be visible for each month section.  This should be one less than
        '   the actual number of visible columns since this is non-inclusive for the first column.
        '   Visible columns = start_row through start_row + increment. e.g. start_row = 8; end_row = 8 + 3 = 11
        visible_cols = 2
    
        'This variable is the number of columns between each month section.  When written, there was one column separating
        '  each month section. This is the iterator that dictates the start column for the next month section therefore, this should be equal to
        '  the number of spacing columns +1 to get to the next start_col.
        space_cols = 2
    
        'Calls the in_month_coords() function in order to find the last column of the month input panes
        in_month_arr = in_month_coords()
        'Sets the initial value for the end column to the to the last column of the month inputs pane.
        end_col = in_month_arr(11, 2)
    
        months = Array("June", "July", "August", "September", "October", "November", "December", "January", "February", "March", "April", "May")
        month_counter = 0
    
        quarters = Array("Q1", "Q2", "Q3", "Q4")
        quarter_counter = 0
    
        For irow = 0 To 50
            If irow < 12 Then  'Rows 0 through 11 are monthly percent allocation panes.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = months(month_counter)
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "m_pct"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
    
                'Increments the month counter in order to move to the next index of the month array on the next loop.
                month_counter = month_counter + 1
    
                'Resets the month counter for the next set of month panes.
                If month_counter >= 12 Then
                    month_counter = 0
                End If
            ElseIf irow >= 12 And irow < 24 Then  'Rows 12 through 23 are hourly allocation panes.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = months(month_counter)
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "m_hrs"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
    
                'Increments the month counter in order to move to the next index of the month array on the next loop.
                month_counter = month_counter + 1
    
                'Resets the month counter for the next set of month panes.
                If month_counter >= 12 Then
                    month_counter = 0
                End If
            ElseIf irow >= 24 And irow < 36 Then 'Rows 24 through 35 are monthly financial panes.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = months(month_counter)
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "m_fin"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
    
                'Increments the month counter in order to move to the next index of the month array on the next loop.
                month_counter = month_counter + 1
    
                'Resets the month counter for the next set of month panes.
                If month_counter >= 12 Then
                    month_counter = 0
                End If
            ElseIf irow >= 36 And irow < 40 Then 'Rows 36 through 39 are quarterly percent panes.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = quarters(quarter_counter)
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "q_pct"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
    
                'Increments the quarter counter in order to move to the next index of the quarters array on the next loop.
                quarter_counter = quarter_counter + 1
    
                'Resets the quarter counter for the next set of quarter panes.
                If quarter_counter >= 4 Then
                    quarter_counter = 0
                End If
            ElseIf irow >= 40 And irow < 44 Then 'Rows 40 through 43 are quarterly hours panes.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = quarters(quarter_counter)
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "q_hrs"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
    
                'Increments the quarter counter in order to move to the next index of the quarters array on the next loop.
                quarter_counter = quarter_counter + 1
    
                'Resets the quarter counter for the next set of quarter panes.
                If quarter_counter >= 4 Then
                    quarter_counter = 0
                End If
            ElseIf irow >= 44 And irow < 48 Then 'Rows 44 through 47 are quarterly financial panes.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = quarters(quarter_counter)
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "q_fin"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
    
                'Increments the quarter counter in order to move to the next index of the quarters array on the next loop.
                quarter_counter = quarter_counter + 1
    
                'Resets the quarter counter for the next set of quarter panes.
                If quarter_counter >= 4 Then
                    quarter_counter = 0
                End If
            ElseIf irow = 48 Then 'Row 48 is an annual percent pane.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = "FY 2020"
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "y_pct"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
            ElseIf irow = 49 Then 'Row 49 is an annual hours pane.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = "FY 2020"
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "y_hrs"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
            ElseIf irow = 50 Then 'Row 50 is an annual financials pane.
                'Sets the first column equal to the month assigned in the months array
                a_pane_cols(irow, 0) = "FY 2020"
                'Sets the second column to the radio buttons designated above in the comments section
                a_pane_cols(irow, 1) = "y_fin"
    
                'Declare start column and assign it to column 1 in the month_cols array
                start_col = end_col + space_cols
                a_pane_cols(irow, 2) = start_col
    
                'Calculate end_column and assign it to column 2 in the month_cols array
                end_col = start_col + visible_cols
                a_pane_cols(irow, 3) = end_col
            Else
                Exit For
            End If
    
            ''***Uncomment this section to debug***
            '' Note: Because of the size of the array, Excel may freeze when printing to the immediate window #vba_sux***'
            'Debug.Print (a_pane_cols(irow, 0))
            'Debug.Print (a_pane_cols(irow, 1))
            'Debug.Print (a_pane_cols(irow, 2))
            'Debug.Print (a_pane_cols(irow, 3))
        Next irow
    
    
    Debug.Print ("complete")
    a_pane_coords() = a_pane_cols
    
    End Function
    
    函数a_pane_coords()作为变量
    '将保存输入窗格中每个月的可见列编号,并将结果作为数组返回。
    作为变体的暗淡月份
    多维数组;12行4列
    '第0列=月份名称;第1列=单选按钮标志;第2列=月份的起始行;第3列=结束月份的行。
    调暗窗格颜色(0到50,0到3)
    月内变暗
    变暗四分之一将容纳一个阵列
    Dim start_col为整数
    Dim end_col为整数
    将可见列变暗为整数
    Dim space_cols作为整数
    Dim MOUNT_计数器为整数
    Dim四分之一计数器为整数
    Dim irow作为整数
    作为整数的Dim-icol
    '指定给列1的单选按钮标志:
    '本月:百分比=百万分之一;小时=百万小时;财务=财务
    “本季度:百分比=q_pct;小时=q_小时;财务=q_fin
    '会计年度:百分比=y_pct;小时=年小时;财务=财政部
    '此变量是每个月部分可见的列数。这应该是比
    '可见列的实际数目,因为第一列不包括此数目。
    '可见列=起始行到起始行+增量。e、 g.起始行=8;结束行=8+3=11
    可见值=2
    '此变量是每个月节之间的列数。编写时,有一列分隔
    “每月一节。这是一个迭代器,指示下个月部分的开始列,因此,它应该等于
    '要到达下一个起始列,间隔列数+1。
    空格_cols=2
    '调用in_month_coords()函数以查找月份输入窗格的最后一列
    月份内=月份内
    '将“结束”列的初始值设置为“月份输入”窗格最后一列的起始值。
    结束月份=月内月份(11,2)
    月份=数组(“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”、“一月”、“二月”、“三月”、“四月”、“五月”)
    月份计数器=0
    季度=阵列(“第一季度”、“第二季度”、“第三季度”、“第四季度”)
    四分之一计数器=0
    对于irow=0到50
    如果irow<12,则“第0行到第11行是每月百分比分配窗格。
    '将第一列设置为等于在月份数组中指定的月份
    a\u窗格\u列(irow,0)=月份(月份计数器)
    '将第二列设置为上述注释部分中指定的单选按钮
    a_pane_cols(irow,1)=“m_pct”
    '声明起始列并将其分配给month_cols数组中的第1列
    开始列=结束列+空格列
    一个窗格(irow,2)=开始列
    '计算结束列并将其分配给月列数组中的第2列
    结束列=开始列+可见列
    a_pane_cols(irow,3)=end_col
    '递增月份计数器,以便移动到下一个循环中月份数组的下一个索引。
    月份计数器=月份计数器+1
    '重置下一组月份窗格的月份计数器。
    如果月份计数器>=12,则
    月份计数器=0
    如果结束
    ElseIf irow>=12且irow<24则“第12行到第23行是每小时分配窗格。
    '将第一列设置为等于在月份数组中指定的月份
    a\u窗格\u列(irow,0)=月份(月份计数器)
    '将第二列设置为上述注释部分中指定的单选按钮
    a_pane_cols(irow,1)=“m_hrs”
    '声明起始列并将其分配给month_cols数组中的第1列
    开始列=结束列+空格列
    一个窗格(irow,2)=开始列
    '计算结束列并将其分配给月列数组中的第2列
    结束列=开始列+可见列
    a_pane_cols(irow,3)=end_col
    '递增月份计数器,以便移动到下一个循环中月份数组的下一个索引。
    月份计数器=月份计数器+1
    '重置下一组月份窗格的月份计数器。
    如果月份计数器>=12,则
    月份计数器=