Inquiridor
Erro 1004 - O Método Pasteespecial da Classe Range Falhou

Pergunta
-
Prezados,
Tenho um problema com um botão de importar dados de uma planilha.
Esse botão chama um diálogo de abertura de arquivo.
Ao escolher o arquivo que vai abrir, o intervalo específico é copiado da Workbook de origem.
Então o intervalo é colado na Workbook de destino.
Até certo dia funcionava perfeitamente, mas a partir de hoje, a mesma gera erro do nada.
As duas pastas de trabalho são protegidas, e desprotegidas para executar as macros.
A mesma gera erro no momento de colar o intervalo na planilha de destino.
1- Na parte que está em negrito, no momento de colar, as vezes dá erro 1004, que o metodo PasteEspecial falhou.
2- As vezes, cola o intervalo encima do cabeçalho, que não deveria. Pois quero que o intervalo seja copiado para a linha 9, acontece que é calculado que o numero de linhas usadas é 9, quando na verdade é 8.
Se somar mais 1, ele joga na linha 10, se subtrair 1, ele cola encima do cabeçalho da linha 8.3- As vezes, no mesmo local, é calculado que o numero de linhas usadas é mais de 10.000, e o intervalo é copiado bem lá em baixo... esquisito demais.
4- Agora no momento de ordenar os dados:
As vezes os dados são ordenados da maneira que espero, em ordem alfabetica de nome.
mas as vezes, os nomes saem todos na mesma linha, e os dados misturados, ao invés de sair cada nome em sua linha!Preciso de ajuda para modificar esse código, se puderem me ajudar, agradeço.
Private Sub btnimportar_Click() '******************************************************************************* Plan1.Unprotect "sic" Dim abrir As FileDialog Set abrir = Application.FileDialog(msoFileDialogOpen) abrir.Show abrir.Execute Dim Lno As Long Lno = Application.ActiveWorkbook.ActiveSheet.UsedRange.Rows.Count - 1 Application.ActiveSheet.Unprotect "sic" 'Application.CutCopyMode = True Application.Range("A9:Z" & Lno).Copy Dim wbdestino As Workbook Dim wsdestino As Worksheet Set wbdestino = Workbooks("ACOMPANHAMENTO DE CASOS V15.xlsm") Set wsdestino = wbdestino.Worksheets("Casos") 'Set wsdestino = ThisWorkbook.Worksheets("Casos") wsdestino.Activate Dim Lnd As Long Dim Lnd2 As Long wsdestino.Unprotect "sic" 'Lnd = wsdestino.Range("A9").CurrentRegion.Rows.Count + 1 Lnd = wsdestino.UsedRange.Rows.Count - 1 Lnd2 = Lnd + 1 wsdestino.Range("A" & Lnd2).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Call atualizar_planilha Plan1.Unprotect "sic" Dim intervalo As Range Dim Regs As Long Regs = Plan1.Range("A9").CurrentRegion.End(xlDown).Rows.Count Set intervalo = ThisWorkbook.Worksheets("Casos").Range("B9:Z" & Regs) intervalo.Sort Key1:=Range("D9:D" & Regs), Order1:=xlAscending, Header:=xlYes, MatchCase:=xlNo, _ Orientation:=xlSortColumns, SortMethod:=xlPinYin, DataOption1:=xlSortNormal wsdestino.Sort.Apply Plan1.Range("B8:Z8").Interior.ColorIndex = 49 Plan1.Range("B8:Z8").Font.ColorIndex = 49 Plan1.Range("B8:Z8").Font.Bold = True Plan1.Range("B8:Z8").Font.Size = 18 Plan1.Range("A:C").HorizontalAlignment = xlCenter Plan1.Range("A:C").VerticalAlignment = xlCenter Plan1.Range("D:D").HorizontalAlignment = xlLeft Plan1.Range("D:D").VerticalAlignment = xlCenter Plan1.Range("E:E").HorizontalAlignment = xlLeft Plan1.Range("E:E").VerticalAlignment = xlCenter Plan1.Range("F:H").HorizontalAlignment = xlCenter Plan1.Range("F:H").VerticalAlignment = xlCenter Plan1.Range("I:I").HorizontalAlignment = xlLeft Plan1.Range("I:I").VerticalAlignment = xlCenter Plan1.Range("J:R").HorizontalAlignment = xlCenter Plan1.Range("J:R").VerticalAlignment = xlCenter Plan1.Range("S:W").HorizontalAlignment = xlLeft Plan1.Range("S:W").VerticalAlignment = xlCenter Plan1.Range("X:Z").HorizontalAlignment = xlLeft Plan1.Range("X:Z").VerticalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("A:C").HorizontalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("A:C").VerticalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("D:D").HorizontalAlignment = xlLeft 'ThisWorkbook.ActiveSheet.Range("D:D").VerticalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("E:E").HorizontalAlignment = xlLeft 'ThisWorkbook.ActiveSheet.Range("E:E").VerticalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("F:H").HorizontalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("F:H").VerticalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("I:I").HorizontalAlignment = xlLeft 'ThisWorkbook.ActiveSheet.Range("I:I").VerticalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("J:R").HorizontalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("J:R").VerticalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("S:W").HorizontalAlignment = xlLeft 'ThisWorkbook.ActiveSheet.Range("S:W").VerticalAlignment = xlCenter 'ThisWorkbook.ActiveSheet.Range("X:Z").HorizontalAlignment = xlLeft 'ThisWorkbook.ActiveSheet.Range("X:Z").VerticalAlignment = xlCenter Plan1.Unprotect "sic" 'ThisWorkbook.ActiveSheet.Rows.WrapText = True 'ThisWorkbook.ActiveSheet.Columns("A:Z").AutoFit 'ThisWorkbook.ActiveSheet.Range("A8").CurrentRegion.RowHeight = 150 'Plan1.Rows.WrapText = True Plan1.Columns("A:Z").AutoFit Plan1.Range("B8").CurrentRegion.End(xlDown).RowHeight = 150 ActiveWorkbook.Save MsgBox "Importação de Dados Concluída" Plan1.Protect "sic" '******************************************************************************* End Sub