Excel VBA,运行时错误-2147417848

时间:2018-06-06 15:23:17

标签: excel vba excel-vba excel-formula excel-2010

我正在运行一个允许用户使用条形码进行扫描并在工作表上打印的UserForm,我还有一个标签,显示到目前为止扫描件总数的计数。当我得到170-180个扫描件时,它会给我一个运行时错误。

Run Time Error

'-2147417848 (80010108)

Method 'Cells' of object_WorkSheet' failed



If KeyCode = 13 Then
    'Application.ThisWorkbook.Worksheets("Registos").Cells(1, 30) = Now()
    last = Application.ThisWorkbook.Worksheets("Registos").Range("A65536").End(xlUp).Row
    Length = Len(registos.TextBox3.Text)
    If Length > 32 Or Length < 1 Then
        MsgBox "O código inserido não tem um nº de caracteres válido!", vbExclamation
        'registos.Hide
        'registos.Show
        registos.TextBox3.Text = ""
        registos.TextBox3.SetFocus
            Else
                If registos.TextBox3 <> "" And registos.TextBox1 <> "" And registos.TextBox2 <> "" And registos.TextBox4 = "" And registos.ComboBox5 <> "" Then
                    Application.ThisWorkbook.Worksheets("Registos").Visible = True
                    WS_Count = Application.ThisWorkbook.Worksheets.Count
                    For j = 1 To WS_Count
                        Application.ThisWorkbook.Worksheets(j).Unprotect Password:="stockregistos"
                    Next j
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 1) = Now()                                               'data
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 6) = registos.Label11.Caption                            'cc
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 5) = registos.Label20.Caption                               'ano fiscal
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 7) = a1logiin.TextBox1.Text                              'OPERARIO
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 8) = registos.TextBox1.Text                              'referencia formata
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 9) = registos.TextBox2.Text                              'ordem
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 10) = registos.TextBox3.Text                             'codigo
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 11) = 1                                                  'quantidade
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 12) = registos.ComboBox1                                 'Entrada/Saida
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 13) = registos.ComboBox5                                 ' ESTADO
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 14) = Format(registos.ComboBox3, "#")                    'CODIGO DEFEITO
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 16) = registos.ComboBox6                                 'ORIGEM DEFEITO
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 17) = registos.TextBox5                                  'OBSERVAÇÕES
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 1, 18) = registos.ComboBox7                                    'TIPO
                    contador = contador + 1
                    registos.Label21.Caption = contador
                    GoTo fim2
                End If
fim2:
                If registos.ComboBox1 = "SAÍDA" And registos.TextBox3 <> "" And registos.TextBox1 <> "" And registos.TextBox2 <> "" And registos.TextBox4 = "" And registos.ComboBox5 <> "" And registos.ComboBox2 <> "" Then
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 1) = Now()                                                 'data
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 6) = registos.ComboBox2                                    'cc
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 5) = registos.Label20.Caption                               'ano fiscal
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 7) = a1logiin.TextBox1.Text                                'operario
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 8) = registos.TextBox1                                     'referencia formata
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 9) = registos.TextBox2                                     'ordem
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 10) = registos.TextBox3.Text                               'codigo
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 11) = 1                                                    'quantidade
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 12) = "ENTRADA"                                            'Defenir saída de um CC como a entrade de outro
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 13) = registos.ComboBox5                                   ' ESTADO
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 14) = Format(registos.ComboBox3, "#")                      'CODIGO DEFEITO
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 16) = registos.ComboBox6                                   'ORIGEM DEFEITO
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 17) = registos.TextBox5                                    'OBSERVAÇÕES
                    Application.ThisWorkbook.Worksheets("Registos").Cells(last + 2, 18) = registos.ComboBox7                                    'TIPO
                    GoTo salto
                End If
            registos.Hide
            registos.Show
            registos.TextBox3.Text = ""
            registos.TextBox3.SetFocus
    End If
    registos.Hide
    registos.Show
    registos.TextBox3.Text = ""
    registos.TextBox3.SetFocus
 End If

我添加了更多代码,因此您可以了解正在发生的事情,并且可能会看到我没有意识到的事情。

1 个答案:

答案 0 :(得分:0)

错误表示有些过载。您可以尝试重新构造代码,以便将每个(大多数)重复指令放入子/函数中,因为子/函数在返回时释放所有变量(包括对象),从而释放内存。

我还有一些建议/问题:

  1. 我会像这样使用With ... End With

    Dim regSh as Worksheet
    Set regSh = ThisWorkbook.Worksheets("Registos")
    With regSh
        .Cells(last + 2, 1) = Now()
        .Cells(last + 2, 6) = registos.ComboBox2
        ....
    End With
    
  2. 此方法仅将工作表对象加载到内存中并将其保留在那里直到End With,从而提高性能。

    1. 我会重新构建代码以避免GoTos
    2. Me.Show
    3. 后紧接着Me.Hide表单的原因有点模糊
    4. 您似乎在每个循环中取消保护工作簿中的所有工作表。你确定这是必要的吗?它需要付出相当大的努力。