简体   繁体   中英

Excel VBA , subscript out of range

so i started writting a code that would write from Workbook1 UserForm to WorkBook2 sheet. For some unknown reason it's not copying the data .

Private Sub CommandButton1_Click()
  On Error GoTo ErrHandler
  Application.ScreenUpdating = False
  Dim src As Workbook
   ' Open EXCEL
Set src = Workbooks.Open("U:\Mecânica\Produção\63177 - Qualidade\Rejeição Interna\Dados\Gaspar\Projeto Manutenção.xlsm", True, False)
WS_Count = src.Worksheets.Count
For o = 1 To WS_Count
 src.Worksheets(o).Unprotect password:="projmanutencao"
Next o

last = src.Worksheets(Manutencao).Range("A65536").End(xlUp).Row

    ' Write regists

   src.Worksheets(Manutencao).Cells(last + 1, 1) = Now()                        'data
   src.Worksheets(Manutencao).Cells(last + 1, 2) = manutencaoexp.ComboBox3      'nº equipamento
   src.Worksheets(Manutencao).Cells(last + 1, 3) = manutencaoexp.ComboBox5                          'avaria
   src.Worksheets(Manutencao).Cells(last + 1, 4) = manutencaoexp.ComboBox4      'serviços
'      src.Worksheets(Manutencao).Cells(last + 1, 5) = Velocidade                   'produtos
'      src.Worksheets(Manutencao).Cells(last + 1, 6) = Qualidade                    'duração
'      src.Worksheets(Manutencao).Cells(last + 1, 7) = Data                         'operario
   src.Worksheets(Manutencao).Cells(last + 1, 8) = manutencaoexp.ComboBox6      'tipo de manutenção

      For o = 1 To WS_Count
 src.Worksheets(o).Protect password:="projmanutencao"
 Next o


  Application.DisplayAlerts = False 'IT WORKS TO DISABLE ALERT PROMPT

  'SAVES FILE USING THE VARIABLE BOOKNAME AS FILENAME
  src.Save

  Application.DisplayAlerts = True 'RESETS DISPLAY ALERTS

' CLOSE THE SOURCE FILE.
src.Close True             ' FALSE - DON'T SAVE THE SOURCE FILE.
Set src = Nothing



ErrHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

If someone can help fix this piece of code , or maybe have a code that copies from a diferente workbook userform . I'm getting a subscript out of range error in

last = src.Worksheets(Manutenção).Range("A65536").End(x1Up).Row

Assuming 'Manutencao' is the worksheet name, then change this line to:

last = src.Worksheets("Manutencao").Range("A65536").End(xlUp).Row

Note the "" around the worksheet name. You'll have to change this wherever you reference this worksheet.

EDIT: Your code could be rewritten like this, to be a little clearer;

Private Sub CommandButton1_Click()
    Dim src As Workbook
    Dim last As Long

    On Error GoTo ErrHandler

    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
    End With

    Set src = Workbooks.Open("U:\Mecânica\Produção\63177 - Qualidade\Rejeição Interna\Dados\Gaspar\Projeto Manutenção.xlsm", True, False)

    With src.Worksheets("Manutencao")
        .Unprotect Password:="projmanutencao"
        last = .Cells(Rows.Count, "A").End(xlUp).Row
        .Cells(last + 1, 1) = Now()                        'data
        .Cells(last + 1, 2) = manutencaoexp.ComboBox3      'nº equipamento
        .Cells(last + 1, 3) = manutencaoexp.ComboBox5                          'avaria
        .Cells(last + 1, 4) = manutencaoexp.ComboBox4      'serviços
'        .Cells(last + 1, 5) = Velocidade                   'produtos
'        .Cells(last + 1, 6) = Qualidade                    'duração
'        .Cells(last + 1, 7) = Data                         'operario
        .Cells(last + 1, 8) = manutencaoexp.ComboBox6      'tipo de manutenção
        .Protect Password:="projmanutencao"
    End With
    src.Close True             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set src = Nothing

ErrHandler:
    If Err Then
        Debug.Print "Error", Err.Number, Err.Description
        Err.Clear
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM