Notifications
Clear all

Exportar Itens conforme filtro

4 Posts
2 Usuários
0 Reactions
664 Visualizações
(@trindade)
Posts: 0
New Member
Topic starter
 

Boa noite, galera do fórum.

Me deparei com o seguinte problema, tenho uma base de dados onde realizo filtro em uma determinada coluna.
Essa coluna recebe valores de data, o que estou tentando realizar é a exportação destes dados conforme o filtro realizado.

Exemplo
Filtro esta selecionado Janeiro, exportar os dados referente a Janeiro.

O código que estou utilizando:

Sub Exportar_xls()

Dim Count, CountLocal As Integer
Dim fApp As Excel.Application
Dim fBook As Excel.Workbook
Dim fSheet As Excel.Worksheet
Dim Arquivo As String
Dim Resultado As VbMsgBoxResult
Dim UltimoRegistro As Integer

    Application.ScreenUpdating = False
    
        Arquivo = Application.GetSaveAsFilename(InitialFileName:="CatMV_", _
            FileFilter:="Pasta de Trabalho do Excel 97-2003 (*.xls), *.xls", _
            Title:="Especifique o nome do arquivo")
            
        If LCase(Arquivo) = "falso" Then Exit Sub
           
        Count = 9
        CountLocal = 4
       
        Set fApp = CreateObject("Excel.Application")
        Set fBook = fApp.Workbooks.Add
    
        Set fSheet = fApp.ActiveWorkbook.Sheets.Add
        fSheet.Name = "Export"
        fApp.Visible = False
        fSheet.Visible = False
        
               
                With fSheet                    
                    .Range("A" & 4).Value = "Id"
                    .Range("B" & 4).Value = "Data"
                    .Range("C" & 4).Value = "Total"
                    .Range("D" & 4).Value = "Total_Somado"
                End With
                
                While Not IsEmpty(Plan2.Range("C" & Count))
            
                    CountLocal = CountLocal + 1
                    
                        With fSheet
                             .Range("A" & CountLocal).Value = Plan2.Range("C" & Count).Value
                             .Range("B" & CountLocal).Value = Plan2.Range("E" & Count).Value
                             .Range("C" & CountLocal).Value = Plan2.Range("K" & Count).Value
                             .Range("D" & CountLocal).Value = Replace(Replace(Plan2.Range("L" & Count).Value, "mv", ""), "nan", "")
                        End With
                         
                    Count = Count + 1
                    
                Wend
    
            fSheet.SaveAs Arquivo
            
            fApp.Workbooks.Close
            fApp.Quit
       
        Set fSheet = Nothing
        Set fBook = Nothing
        Set fApp = Nothing
    
    Application.ScreenUpdating = True
        
End Sub

Gostaria de uma ajuda dos senhores para resolver esse problema, vale lembrar que qualquer ideia sempre é bem vinda.

 
Postado : 05/01/2016 9:33 pm
(@mprudencio)
Posts: 0
New Member
 

Um arquivo de exemplo (planilha), ajuda bastante .... a obter a solução esperada

 
Postado : 06/01/2016 4:50 am
(@trindade)
Posts: 0
New Member
Topic starter
 

Bom dia, MPrudencio.

Segue um exemplo de arquivo que esta funcionando.

O filtro que vou precisar realizar é conforme a imagem:

Ao selecionar o filtro, esta visível o Janeiro de todos os anos do arquivo, preciso exportar somente essa informação.

 
Postado : 06/01/2016 6:52 am
(@trindade)
Posts: 0
New Member
Topic starter
 

Boa tarde, Srs.

Com ajuda do Reinaldo, consegui resolver meu problema, segue código:

Sub Exportar_xlsxFiltro()

Dim iCount As Integer, CountLocal As Integer
Dim fApp As Excel.Application
Dim fBook As Excel.Workbook
Dim fSheet As Excel.Worksheet
Dim Arquivo As String

Dim tt As Integer
For iCount = 9 To 44
    If Cells(iCount, "C").EntireRow.Hidden = False Then tt = tt + 1
Next
MsgBox tt
Application.ScreenUpdating = False
    
Arquivo = Application.GetSaveAsFilename(InitialFileName:="Arquivo_", _
          FileFilter:="ExcelFiles(*.xlsx),*.xlsx", _
          Title:="Especifique o nome do arquivo")
            
If LCase(Arquivo) = "falso" Then Exit Sub
           
iCount = 9
CountLocal = 4
       
Set fApp = CreateObject("Excel.Application")
Set fBook = fApp.Workbooks.Add
Set fSheet = fApp.ActiveWorkbook.Sheets.Add
        
fSheet.Name = "Export"
fApp.Visible = False
fSheet.Visible = False
        
    With fSheet
        .Range("A" & 4).Value = "Estacao"
        .Range("B" & 4).Value = "Data"
        .Range("C" & 4).Value = "Total"
        .Range("D" & 4).Value = "Total_Somado"
    End With
                
While Not IsEmpty(Plan1.Range("C" & iCount))
 If Cells(iCount, "C").EntireRow.Hidden = False Then
    CountLocal = CountLocal + 1
    With fSheet
        .Range("A" & CountLocal).Value = Plan1.Range("C" & iCount).Value
        .Range("B" & CountLocal).Value = Plan1.Range("D" & iCount).Value
        .Range("C" & CountLocal).Value = Plan1.Range("E" & iCount).Value
        .Range("D" & CountLocal).Value = Replace(Replace(Plan1.Range("F" & iCount).Value, "mv", ""), "nan", "")
    End With
   End If
    iCount = iCount + 1
Wend
    
fSheet.SaveAs Arquivo
fApp.Workbooks.Close
fApp.Quit
       
Set fSheet = Nothing
Set fBook = Nothing
Set fApp = Nothing
Application.ScreenUpdating = True
MsgBox "Feito"
End Sub
 
Postado : 12/01/2016 11:12 am