Excel VBA代码在中断模式下工作正常,在运行时跳过

时间:2014-05-12 09:08:41

标签: vba excel-vba excel

上次我在这里问过这个解决方案有点好吃:)。

下面的代码选择工作表,根据它们设置变量,然后调用一系列函数对它们进行计算。我根据6种不同的表格为14种不同的情况做了这个。

当我在代码中断模式下对它进行测试时,它适用于所有工作表/案例,但是当我运行它时,它会跳过工作表的area_flag(calcs_sheet)函数(" valve"),情况2。知道什么会导致它只在运行时跳过部分代码吗?

提前致谢!

        WS_Count = ActiveWorkbook.Worksheets.Count

        Set table_array = Sheets("flag_matrix").Range("a:bl") ''' for whole of current flag matrix

        For i = 1 To WS_Count

        If ActiveWorkbook.Worksheets(i).Name = "valve" Then ''''VALVE'''''''''''
        'setting variables for looping through each worksheet/parameter
                  For case_no = 1 To 4
                    Select Case case_no

                           Case 1 'valve length

                            Set filter_range = Worksheets(i).Range("j:j,s:s")
                            Set calcs_sheet = Sheets("valve_length_calcs")
                            Set tables_sheet = Sheets("valve_length_tables")

                            col_ind = 57

                            Call filter(filter_range, calcs_sheet, tables_sheet)
                            Call area_flag(calcs_sheet)
                            Call temp_calcs(table_array, col_ind, calcs_sheet, tables_sheet) ''''<---------------needs work first

                           Case 2

                            Set filter_range = Worksheets(i).Range("j:j,u:u") 'valve diam
                            Set calcs_sheet = Sheets("valve_diam_calcs")
                            Set tables_sheet = Sheets("valve_diam_tables")

                            col_ind = 58

                            Call area_flag(calcs_sheet)
                            Call filter(filter_range, calcs_sheet, tables_sheet)
                             Call temp_calcs(table_array, col_ind, calcs_sheet, tables_sheet)

                           Case 3

                            Set filter_range = Worksheets(i).Range("j:j,y:y") 'valve_k
                            Set calcs_sheet = Sheets("valve_k_calcs")
                            Set tables_sheet = Sheets("valve_k_tables")

                            col_ind = 60

                            Call filter(filter_range, calcs_sheet, tables_sheet)
                            Call area_flag(calcs_sheet)
                             Call temp_calcs(table_array, col_ind, calcs_sheet, tables_sheet)

                           Case 4

                            Set filter_range = Worksheets(i).Range("j:j,aq:aq")
                            Set calcs_sheet = Sheets("valve_con_calcs")
                            Set tables_sheet = Sheets("valve_con_tables")

                            col_ind = 61

                            Call filter(filter_range, calcs_sheet, tables_sheet)
                            Call area_flag(calcs_sheet)
                            Call temp_calcs(table_array, col_ind, calcs_sheet, tables_sheet)

                    End Select
                Next



        ElseIf ActiveWorkbook.Worksheets(i).Name = "pipe" Then ''''PIPE''''''''''
            For case_no = 1 To 5
                Select Case case_no........................

  '<><><><>><><><><><><><><><><><><><><><><><

Private Function area_flag(calcs_sheet As Worksheet)

Dim z As Integer
Dim c As Integer
Dim filter_value As String
Dim p As Integer


calcs_sheet.Select
EndRow = Application.CountA(Range("C:C"))    'determine length of
z = 4 'starting column after the other calcs (will always be three columns)

            For c = 2 To EndRow 'Loop through sheet1 and search for criteria

If Cells(c, Columns("C:C").column).Value <> "" Then 'if cell isnt blank in c
        filter_value = Cells(c, Columns("C:C").column).Value 'filter value is  DMA value in c
                calcs_sheet.Range("A:A").AutoFilter field:=1, Criteria1:=filter_value 'filters flags associated with DMA 'filter_Value'
                    Range("A:B").Copy
            calcs_sheet.AutoFilterMode = False 'turns filter off
        calcs_sheet.Activate
    Cells(1, z).Select
ActiveSheet.Paste ' pastes the three columns into calcs sheet without overwriting previous

Call ClearClipboard

        z = z + 3 ' moves z along three columns ready for the next group of calcs.

End If
            Next c

End Function

1 个答案:

答案 0 :(得分:0)

事实上这是一个简单的星期五晚上/星期一早上的错误。

我已经以错误的顺序调用了阀门的功能,案例2.第二个功能依赖于第一个完成的功能。

Case 2

                        Set filter_range = Worksheets(i).Range("j:j,u:u") 'valve diam
                        Set calcs_sheet = Sheets("valve_diam_calcs")
                        Set tables_sheet = Sheets("valve_diam_tables")