Problemas Exportar com VBA [RESOLVIDO]

Fabio Trindade

11-01-2016 13:25:59

Bom dia, Galera.

Depois de algum tempo estou de volta no fórum e com uma dúvida lascada.
Tenho uma aplicação que realiza a exportação dos dados para outra Plan, minha dúvida é a seguinte:
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 o mês de Janeiro, quero exportar os dados referente a todos os Janeiro.


Segue um exemplo de arquivo que está funcionando.
Exportar.zip
O filtro que vou precisar realizar é conforme a imagem:
Exportar.zip
Ao selecionar o filtro, está visível o Janeiro de todos os anos do arquivo, preciso exportar somente essa informação.

Estou utilizando o código:

Código: Selecionar todos

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.

Reinaldo

11-01-2016 14:26:36

Segue uma possibilidade

Código: Selecionar todos

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

Fabio Trindade

11-01-2016 17:33:29

Boa tarde, Reinaldo.

Realizei teste com o código era isso mesmo que eu estava quebrando cabeça.
Ficou muito bom.
Mais uma vez, obrigado pela paciência.