Notifications
Clear all

Copiar e colar em sheet diferente com linhas de intervalo

2 Posts
2 Usuários
0 Reactions
844 Visualizações
(@miguexcel)
Posts: 167
Estimable Member
Topic starter
 

Olá,

Tenho tido dificuldade em fazer copiar colar para uma folha diferente mantendo uma separação lógica com linhas. Alguém me consegue ajudar?

Tenho na folha 1 uma lista de produtos, organizados por classe. Pretendo copiar essa lista para outra folha e organizar por classe.

Envio o xlsm.

Tenho dificuldade em conseguir isto de forma dinâmica.

 
Postado : 19/04/2015 10:31 am
Fernando Fernandes
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Bom dia!!

Tente assim.

Sub AleVBA_15467()
   Dim ws As Worksheet, ws1 As Worksheet
   Dim cel          As Range
   Dim LR           As Long
   Dim LR1          As Long
   Dim LC           As Long

   Set ws = Sheets("Folha1")
   Set ws1 = Sheets("Folha2")

   Application.ScreenUpdating = False
   If Not Evaluate("ISREF(Listas!A1)") Then
      Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Listas"
   Else
      Sheets("Listas").Cells.Clear
   End If

   With ws
      LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                       SearchDirection:=xlPrevious).Row
      LC = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, _
                       SearchDirection:=xlPrevious).Column
      .Sort.SortFields.Clear
      .Sort.SortFields.Add Key:=Range("A2:A" & LR), _
                           SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
      .Sort.SortFields.Add Key:=Range("D2:D" & LR), _
                           SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
      With .Sort
         .SetRange Range(Cells(1, 1), Cells(LR, LC))
         .Header = xlYes
         .MatchCase = False
         .Orientation = xlTopToBottom
         .SortMethod = xlPinYin
         .Apply
      End With

      .Columns("A:A").AdvancedFilter Action:=xlFilterCopy, _
                                     CopyToRange:=Sheets("Listas").Range("A1"), Unique:=True
      ActiveWorkbook.Names.Add Name:="Classe", RefersTo:= _
                               "=OFFSET(Listas!$A$2,0,0,(COUNTA(Listas!$A:$A)-1),1)"
      ws1.Cells.ClearContents
      For Each cel In Range("Classe")
         .AutoFilterMode = False
         .Range(.Cells(1, 1), .Cells(LR, LC)).AutoFilter Field:=1, Criteria1:=cel.Value
         With ws1
            On Error Resume Next
            LR1 = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                              SearchDirection:=xlPrevious).Row + 2
            If Err.Number = 91 Then
               LR1 = 1
            End If
            ws.AutoFilter.Range.Copy
            .Cells(LR1, 1).PasteSpecial
         End With
         .ShowAllData
      Next cel
      .AutoFilterMode = False
   End With
   Application.DisplayAlerts = False
   Sheets("Listas").Delete
   Application.DisplayAlerts = True
   Application.ScreenUpdating = True
End Sub

Att

Existem mil maneiras de preparar Neston. Invente a sua!
http://www.youtube.com/ExpressoExcel

 
Postado : 22/04/2015 8:14 am