无法调用子程序

时间:2015-11-19 05:49:42

标签: excel-vba excel-2010 subroutine vba excel

如果用户在userform中输入有效的周数,我无法调用子BCD。如果用户输入无效或空周数,则执行子BCD。

我的代码有什么问题?

Private Sub cmdOK1_Click()

Call ABC
Call BCD

End Sub

Sub ABC()

'Prompt message box when week number is empty
    If Me.txtWeekNo.Value = "" Then
        MsgBox "Please enter week number to print.", vbExclamation, "Print Work Order by Schedule"
        Me.txtWeekNo.SetFocus
        Exit Sub
    End If

'Check for valid week number
LColumn = Worksheets("RIG PMS").Cells(5, Worksheets("RIG PMS").Columns.Count).End(xlToLeft).Column 'Define last column in week number
Set WeekNoRng = Worksheets("RIG PMS").Range(Cells(5, 10), Cells(5, LColumn))
    If WorksheetFunction.CountIf(WeekNoRng, txtWeekNo.Value) = 0 Then
         MsgBox "Invalid Week Number"
         Exit Sub
        Else
            Unload UserForm1
    End If

    'Determine Column
        For i = 10 To LColumn
            If WorksheetFunction.CountIf(Worksheets("RIG PMS").Cells(5, i), txtWeekNo.Value) > 0 Then
                Acolumn = i
            End If
        Next i

LRow = Worksheets("RIG PMS").Cells(Rows.Count, "I").End(xlUp).Row
Set OpenWORng = Worksheets("Open WO").Range("B3:B" & Worksheets("Open WO").Range("B" & Rows.Count).End(xlUp).Row)

    For a = 7 To LRow Step 3
        AA = 0
        If Worksheets("RIG PMS").Cells(a, Acolumn).Value <> "" And Worksheets("RIG PMS").Cells(a, Acolumn - 2).Value = 1 Then
            b = Worksheets("RIG PMS").Cells(a, 7).Value
            Worksheets("Power Pack Monthly").Range("C5").Value = Worksheets("RIG PMS").Cells(a - b, 2).Value 'Asset Code
            Worksheets("Power Pack Monthly").Range("C6").Value = Worksheets("RIG PMS").Cells(a - b, 3).Value 'Description
            Worksheets("Power Pack Monthly").Range("P5").Value = Worksheets("RIG PMS").Cells(a, Acolumn).Value 'WO
            Worksheets("Power Pack Monthly").Range("P6").Value = Worksheets("RIG PMS").Cells(a + 1, Acolumn).Value 'Proposed Date
            AA = 1
        End If

        If Worksheets("RIG PMS").Cells(a, Acolumn).Value <> "" And Worksheets("RIG PMS").Cells(a, Acolumn - 2).Value = 2 Then
            b = Worksheets("RIG PMS").Cells(a, 7).Value
            Worksheets("Power Pack 6 Monthly").Range("C5").Value = Worksheets("RIG PMS").Cells(a - b, 2).Value 'Asset Code
            Worksheets("Power Pack 6 Monthly").Range("C6").Value = Worksheets("RIG PMS").Cells(a - b, 3).Value 'Description
            Worksheets("Power Pack 6 Monthly").Range("P5").Value = Worksheets("RIG PMS").Cells(a, Acolumn).Value 'WO
            Worksheets("Power Pack 6 Monthly").Range("P6").Value = Worksheets("RIG PMS").Cells(a + 1, Acolumn).Value 'Proposed Date
            AA = 1
        End If

    'Copy Open WO to Open WO Sheet
         If WorksheetFunction.CountIf(OpenWORng, Worksheets("RIG PMS").Cells(a, Acolumn).Value) = 0 And AA = 1 Then
            Worksheets("Open WO").Range("B" & Worksheets("Open WO").Range("B" & Rows.Count).End(xlUp).Row + 1).Value = Worksheets("RIG PMS").Cells(a, Acolumn).Value 'WO No.
            Worksheets("Open WO").Range("C" & Worksheets("Open WO").Range("C" & Rows.Count).End(xlUp).Row + 1).Value = Worksheets("RIG PMS").Cells(a - b, 2).Value 'Asset Code
            Worksheets("Open WO").Range("D" & Worksheets("Open WO").Range("D" & Rows.Count).End(xlUp).Row + 1).Value = Worksheets("RIG PMS").Cells(a - b, 3).Value 'Asset Description
            Worksheets("Open WO").Range("E" & Worksheets("Open WO").Range("D" & Rows.Count).End(xlUp).Row).Value = 1 'For trigger purpose
        End If

        If a = LRow - 2 Then
            MsgBox "Continue"
            Exit Sub
        End If
    Next a

End Sub

Sub BCD()

   MsgBox "ABC"

End Sub

2 个答案:

答案 0 :(得分:0)

错误再现:我能够通过发布所有过程CodeModule UserForm来重现问题中提到的行为。

行为原因:当周数有效时,第二个过程BCD未运行,因为按此行卸载了UserFrom

Unload UserForm1

Unload Statement的评论说:

  

备注:当一个对象被卸载时,它会被从内存中移除   回收与该对象关联的内存。直到放入   内存再次使用Load语句,用户无法与之交互   对象,并且无法以编程方式操作对象。

解决方案:虽然有几种解决方案适用,但我认为最简单的方法是将程序BCD移至标准模块 - 测试

其他解决方案:

  • 将两个程序移出UserForm,但必须对UserForm中对象的所有引用进行限定(即将Me替换为UserForm1和引用要TxtWeekNo必须限定为UserForm1.TxtWeekNo) - 已经过测试

  • 隐藏用户表单并在cmdOK1_Click程序结束时将其卸载。

  • 可能还有很多其他人。

答案 1 :(得分:-3)

代码位于表单的类模块中。它应该在标准模块中。