Adaptar Código

Vejabem

20-01-2018 02:20:59

Pessoal, boa noite!
O código abaixo serve para gerar um relatório a partir de uma ListView. Gostaria de adaptar esse mesmo código para gerar um relatório a partir de uma ListBox. Será que é possível? Alguém pode me ajudar nesse sentido, por gentileza?

Desde já, obrigado!

----------------------

On Error Resume Next

Dim i As Integer
Dim j As Integer
Dim Regs As Integer
Dim linha As Long
Dim caminho As String

Application.ScreenUpdating = False
Regs = Me.ListView1.ListItems.Count
Sheets("Imprimir").Select
Range("A2:W30000").Select
Selection.ClearContents

With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With

Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A2").Select

'Laço das linhas e colunas para extrair os valores da listview e passar para o planilha
For i = 1 To Regs
linha = i + 1
Cells(linha, 1) = Me.ListView1.ListItems(i)
For j = 1 To Me.ListView1.ColumnHeaders.Count - 1
Cells(linha, j + 1) = Me.ListView1.ListItems(i).ListSubItems(j)
Next
Next



'Formata área de impressão
Range("A1:W" & linha).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With

Range("A" & linha & ":W" & linha).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -4.99893185216834E-02
.PatternTintAndShade = 0
End With


' Define área de impressão
Range("A1:W" & linha).Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$W$" & linha
Application.ScreenUpdating = True
Range("A2").Select

MsgBox "Relatório gerado com sucesso!", vbInformation, "GERAR RELATÓRIO"

srobles

20-01-2018 16:33:45

VejaBem,

Experimente o código já alterado abaixo :

Código: Selecionar todos

    On Error Resume Next
    
    Dim i As Integer
    Dim j As Integer
    Dim Regs As Integer
    Dim linha As Long
    Dim caminho As String
    
        Application.ScreenUpdating = False
        Regs = Me.ListBox1.ListCount - 1
        Sheets("Imprimir").Select
        Range("A2:W30000").Select
        Selection.ClearContents
        
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        
                Selection.Borders(xlDiagonalDown).LineStyle = xlNone
                Selection.Borders(xlDiagonalUp).LineStyle = xlNone
                Selection.Borders(xlEdgeLeft).LineStyle = xlNone
                Selection.Borders(xlEdgeTop).LineStyle = xlNone
                Selection.Borders(xlEdgeBottom).LineStyle = xlNone
                Selection.Borders(xlEdgeRight).LineStyle = xlNone
                Selection.Borders(xlInsideVertical).LineStyle = xlNone
                Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
                Range("A2").Select
        
                'Laço das linhas e colunas para extrair os valores da listview e passar para o planilha
                For i = 0 To Regs
                    linha = i + 1
                    Cells(linha, 1) = Me.ListBox1.List(i, 0)
                        For j = 1 To Me.ListBox1.ColumnCount
                            Cells(linha, j + 1) = Me.ListBox1.List(i, j)
                        Next
                Next
        
        
        
            'Formata área de impressão
            Range("A1:W" & linha).Select
            Selection.Borders(xlDiagonalDown).LineStyle = xlNone
            Selection.Borders(xlDiagonalUp).LineStyle = xlNone
                With Selection.Borders(xlEdgeLeft)
                    .LineStyle = xlContinuous
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                    .Weight = xlHairline
                End With
                    With Selection.Borders(xlEdgeTop)
                        .LineStyle = xlContinuous
                        .ColorIndex = xlAutomatic
                        .TintAndShade = 0
                        .Weight = xlHairline
                    End With
                        With Selection.Borders(xlEdgeBottom)
                            .LineStyle = xlContinuous
                            .ColorIndex = xlAutomatic
                            .TintAndShade = 0
                            .Weight = xlHairline
                        End With
                        With Selection.Borders(xlEdgeRight)
                            .LineStyle = xlContinuous
                            .ColorIndex = xlAutomatic
                            .TintAndShade = 0
                            .Weight = xlHairline
                        End With
                    With Selection.Borders(xlInsideVertical)
                        .LineStyle = xlContinuous
                        .ColorIndex = xlAutomatic
                        .TintAndShade = 0
                        .Weight = xlHairline
                    End With
                    With Selection.Borders(xlInsideHorizontal)
                    .LineStyle = xlContinuous
                    .ColorIndex = xlAutomatic
                    .TintAndShade = 0
                    .Weight = xlHairline
                    End With
            
            Range("A" & linha & ":W" & linha).Select
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = -4.99893185216834E-02
                .PatternTintAndShade = 0
            End With
        
        
        ' Define área de impressão
        Range("A1:W" & linha).Select
        ActiveSheet.PageSetup.PrintArea = "$A$1:$W$" & linha
        Application.ScreenUpdating = True
        Range("A2").Select
    
    MsgBox "Relatório gerado com sucesso!", vbInformation, "GERAR RELATÓRIO"
Espero ter ajudado.

Abs

Vejabem

20-01-2018 18:54:23

Boa tarde, Saulo!

Muito obrigado! Deu certo sim.
Contudo, o código não está deixando a primeira linha da planilha para eu inserir o cabeçalho.

Vejabem

22-01-2018 11:43:43

Saulo, já resolvi o problema do cabeçalho!

Muito obrigado pela ajuda!