Excel VBA:执行www.example.com后无法通过Userform对象进行制表ActiveWorkbook.save

ssgvzors  于 2023-05-19  发布在  其他
关注(0)|答案(2)|浏览(106)

这是在Excel VBA for Microsoft Office 2007中。操作系统是Windows 10。
翻译后摘要:在一个用户表单中,我可以使用标签循环通过表单的字段/对象,然后在运行一个函数,有Activeworkbook.save在它我不能再循环通过表单的字段和按Enter键不再激活默认的命令按钮。如果Activeworkbook.save被注解掉,上述问题不会发生。为什么会发生这种情况,我该如何避免?
编辑:只有在我关闭Excel并再次打开宏文件后,问题才会出现。宏第一次运行并与之交互时工作正常。只有当它再次运行时,bug才会出现。此外,在上述用户表单的终止事件函数中,我也有一个Activeworkbook.save行。
EDIT 2:userformtermination函数的共享代码。
这里讨论的函数是一个Submitbuttonclicked事件,它在扫描员工ID时存储时间、日期和姓名。上述问题使我无法在扫描第一个ID后继续存储扫描的ID。我现在只关心www.example.com的ActiveWorkbook.save,但我将分享函数的代码,以防其他行也与该问题有关。

Private Sub CommandButton1_Click()

    Dim ImageFolder As String
    Dim FilePath As String
    Dim FullImagePath As String
    Dim ColumnIndex As Integer
    Dim Errorflag As Boolean
    
    Errorflag = False
    
    If MorningButton.Value And AfternoonButton.Value And OvertimeButton.Value = False Then
    MsgBox "Please select from the menu options before scanning"
    Errorflag = True
    ElseIf OutButton.Value = False And InButton.Value = False Then
    MsgBox "Please select from the menu options before scanning"
    Errorflag = True
    End If
    
    'Column Index Value based on radio button selections
    If MorningButton.Value = True And InButton.Value = True Then
    ColumnIndex = 2
    ElseIf MorningButton.Value = True And InButton.Value = False Then
    ColumnIndex = 3
    ElseIf AfternoonButton.Value = True And InButton.Value = True Then
    ColumnIndex = 4
    ElseIf AfternoonButton.Value = True And InButton.Value = False Then
    ColumnIndex = 5
    ElseIf OvertimeButton.Value = True And InButton.Value = True Then
    ColumnIndex = 6
    ElseIf OvertimeButton.Value = True And InButton.Value = False Then
    ColumnIndex = 7
    Else
    MsgBox "Please select from the menu options before scanning"
    Errorflag = True
    End If

    If Errorflag = False Then
    
        Lastrow = Sheets("Database").Cells(Rows.Count, "A").End(xlUp).Row + 1
        With Sheets("Database")
            .Cells(Lastrow, 1).Value = TextBox1.Text
            .Cells(Lastrow, ColumnIndex).Value = Now()
        End With
        Label2.Caption = TextBox1.Text
        Label5.Caption = Now()
        
        ImageName = TextBox1.Text & ".jpg"
        ImageFolder = "\PICS CHRYSOS 1x1\"
        FilePath = ThisWorkbook.Path
        FullImagePath = FilePath & ImageFolder & ImageName
        
        If Dir(FullImagePath) <> "" Then
            Image1.Picture = LoadPicture(FullImagePath)
            Image1.PictureSizeMode = 3
        Else
            MsgBox "Could not load image - File not Found. Please check file name and QR encoding if they match. QR Code is" & " '" & TextBox1.Text & "'"
        End If
        
        'ActiveWorkbook.Save
        TextBox1.Value = ""
        TextBox1.SetFocus
    Else
        TextBox1.Value = ""
        TextBox1.SetFocus
        Errorflag = False
    End If
    
    
End Sub

EDIT 2:共享Userform终止函数

Private Sub Userform4_Terminate()
ActiveWorkbook.Save
End Sub
bkhjykvo

bkhjykvo1#

我在MS Excel 2010中尝试了完全相同的宏文件。我在MS Excel上所做的复制错误不再复制错误。问题解决了MS Excel 2007有问题:(

5ktev3wc

5ktev3wc2#

首先,将表单的Cycle属性设置为2 - fmCycleCurrentForm。然后,使用Application.SendKeys导航到所需的对象。
下面是一个示例代码,我在Serial_number文本框中扫描QR码,并使用提供的代码将焦点设置回Serial_number文本框:

Private Sub cmd_add_color_Click()
    Call addColor
    cmb_color.SetFocus
    Application.SendKeys "{TAB}"       ' Getting setfocus back to the serial textbox
End Sub

相关问题