远程输入数据时如何限制excel关闭

时间:2016-03-26 07:47:49

标签: excel excel-vba vba

我需要一个宏来阻止excel关闭,同时我将数字远程填入列。最初我已经要求用户输入ID。下一步是阻止关闭。怎么办呢?

下面是ID输入和单元格选择的代码。

Sub Enter_1()
Dim data_1 As String
Dim sCell As Variant
Dim rslt As Integer
Dim x As Integer

Do
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    data_1 = InputBox(Prompt:="Enter Employee No.", Title:="Employee", Default:="Enter Employee No. here")

    If data_1 = "" Then
        QuestionToMessageBox = "Exit?"
        YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "No")
    End If

    If YesOrNoAnswerToMessageBox = vbNo Then

        data_1 = InputBox(Prompt:="Enter Employee No.", Title:="Employee", Default:="Enter Employee No. here")

    Exit sub    
    End If

        If Not IsNumeric(data_1) Or data_1 = "" Then
            rslt = 0
        Else: rslt = 1
        End If

        If rslt = 0 Then
         MsgBox "You can only enter a number in this field"

        Else:
            Sheets("Oven After Assay Test").Activate
            For x = 6 To 50
                If Cells(x, 8).Value = "" Then
                    Cells(x, 8).Select
                    cancel = True
                    Exit For
                End If
            Next
        End If

    Loop While rslt = 0



End Sub

1 个答案:

答案 0 :(得分:0)

您可以按如下方式实现所请求的功能:

插入代码模块并添加以下行:

ThisWorkbook

Sub VBA代码模块中添加Private Sub Workbook_Open() FlagToClose = True End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) If Not FlagToClose Then Cancel = True End Sub

Sub Enter_1()
    FlagToClose = False

    'YOUR PROCEDURE
    '.....................

    FlagToClose = True
End Sub

按照以下步骤修改您的程序:

def main ():
    first_name = input("Enter first name: ")
    last_name = input("Enter last name: ")
    print(last_name + ", " + first_name)
main()