Ajuda com Código em Listview

PauloD

27-12-2017 11:14:20

Qual a possibilidade de me ajudarem neste código que me carrega uma listview com os dados que pretendo

Private Sub PreencherColunasPreDterminadas()

Dim ws As Worksheet
Dim coluna As Integer
Dim Linha As Integer
Dim itm As ListItem, n As Long, lngCol As Long
Dim vardata As Variant
Dim lastRow As Long


Set ws = ThisWorkbook.Worksheets("Tempo")

coluna = 1
Linha = 7
Me.ListView1.ListItems.Clear

Me.ListView1.ColumnHeaders.Clear


vardata = Range("A7").CurrentRegion.Value
'vardata = Range("A7").CurrentRegion.Value

With ws
While coluna < 15 'Preenche o ListView com as 3 primeiras colunas
With ListView1
.Gridlines = True
With .ColumnHeaders
.Clear
'adiciona colunas
.Add , , "EQUIPA", 40
.Add , , "DATA DISTRIBUIÇÃO", 90, lvwColumnCenter 'lvwColumnCenter
.Add , , "DATA COMUNICAÇÃO", 90, lvwColumnCenter 'lvwColumnRight
.Add , , "TEMPO DE DEMORA", 125, lvwColumnCenter 'lvwColumnCenter
.Add , , "DATA COMUNICAÇÃO", 90, lvwColumnCenter
.Add , , "DATA DE ENTREGA", 80, lvwColumnCenter
.Add , , "TEMPO FINALIZAÇÃO", 125, lvwColumnCenter
.Add , , "TEMPO ENTREGA FINALIZAÇÃO", 125, lvwColumnCenter
.Add , , "RELATÓRIO", 60, lvwColumnCenter
.Add , , "DISTR/COM UTEIS", 80, lvwColumnCenter
.Add , , "COM/ENTR UTEIS", 80, lvwColumnCenter
.Add , , "DISTR/ENTR UTEIS", 80, lvwColumnCenter
End With

.View = lvwReport 'exibir em modo de relatório
End With

coluna = coluna + 1

Wend

'Preenche as Linhas
With ListView1
For n = 2 To UBound(vardata)

Set itm = .ListItems.Add(n - 1, , vardata(n, 1))
For lngCol = 2 To UBound(vardata, 2)
itm.ListSubItems.Add , , vardata(n, lngCol)
Next lngCol
Next n
End With

End With

End Sub

O que pretendo e não estou a conseguir é o seguinte:
quero que a informação seja carregada até á ultmia linha da coluna "A" que tenha informação
e sempre que adiciono nova informação na coluna "A" vá acrescentando as linhas na listview
obrigado e aguardo ajuda

srobles

27-12-2017 15:44:41

PauloD,

Veja se a alteração na rotina atende sua necessidade.
Todavia, deixo abaixo um modelo para estudo.

Código: Selecionar todos

Dim ws As Worksheet
Dim coluna As Integer
Dim Linha As Integer
Dim itm As ListItem, n As Long, lngCol As Long
Dim lastRow As Long

Sub preencheListView()
    Set ws = ThisWorkbook.Worksheets("Plan1")

    With UserForm1
        With .ListView1
            .AllowColumnReorder = False
            .ColumnHeaders.Clear
            .FullRowSelect = True
            .Gridlines = True
            .ListItems.Clear
            .LabelEdit = lvwManual
            .LabelWrap = False
            .View = lvwReport
            
            With .ColumnHeaders
                .Add , , "EQUIPA", 40
                .Add , , "DATA DISTRIBUIÇÃO", 90, lvwColumnCenter 'lvwColumnCenter
                .Add , , "DATA COMUNICAÇÃO", 90, lvwColumnCenter 'lvwColumnRight
                .Add , , "TEMPO DE DEMORA", 125, lvwColumnCenter 'lvwColumnCenter
                .Add , , "DATA COMUNICAÇÃO", 90, lvwColumnCenter
                .Add , , "DATA DE ENTREGA", 80, lvwColumnCenter
                .Add , , "TEMPO FINALIZAÇÃO", 125, lvwColumnCenter
                .Add , , "TEMPO ENTREGA FINALIZAÇÃO", 125, lvwColumnCenter
                .Add , , "RELATÓRIO", 60, lvwColumnCenter
                .Add , , "DISTR/COM UTEIS", 80, lvwColumnCenter
                .Add , , "COM/ENTR UTEIS", 80, lvwColumnCenter
                .Add , , "DISTR/ENTR UTEIS", 80, lvwColumnCenter
            End With
            
            For n = 2 To ws.UsedRange.Rows.Count ' Adicionamos as linhas a iniciar pela linha 2 até a última preenchida
                'Adiciona o novo item
                Set itm = .ListItems.Add(n - 1, , ws.Cells(n, 1))
                    'Adicionamos os itens de cada coluna a iniciar pela coluna 2 até a ultima coluna preenchida
                    For lngCol = 2 To ws.UsedRange.Columns.Count
                        'Adiciona o valor da coluna de incide lngCol
                        itm.ListSubItems.Add , , ws.Cells(n, lngCol)
                    Next lngCol
            Next n
            
        End With
    End With
End Sub
Abs

PauloD

28-12-2017 09:56:25

Boas e desde já muito obrigado mesmo pela ajuda
mas não me devo ter explicado bem
vou tentar me explicar melhor
o que pretendo é o seguinte se não existir informação nas 2 primeiras colunas não quero que carregue nada no resto das linhas e colunas para baixo ( mesmo que as outras colunas e linhas tenham dados carregados)
e sempre que preenchemos as 2 primeiras colunas então sim vai adicionando na list view quando a abrimos


1 Equipa Data Data1 Data2 Data3 Data4
2 Equipa 1 20-11-2017 20-11-2017 20-11-2017 20-11-2017 20-11-2017
3 Equipa 1 20-11-2017 20-11-2017 20-11-2017 20-11-2017 20-11-2017
4 Equipa 1 20-11-2017 20-11-2017 20-11-2017 20-11-2017 20-11-2017
5 Equipa 1 20-11-2017 20-11-2017 20-11-2017 20-11-2017 20-11-2017
6 xxxxx xxxxx 20-11-2017 20-11-2017 20-11-2017
7 xxxxx xxxxx 20-11-2017 20-11-2017

Ou seja as linhas 6 e 7 não carregam na listview
Mas se as preencher a coluna equipa e data assim sim vao carregar na list view

Mais uma vez muito obrigado pela ajuda

srobles

28-12-2017 14:30:51

PauloD,

Experimente substituir o exemplo que deixei pelo novo que segue abaixo:

Código: Selecionar todos

Dim ws As Worksheet
Dim coluna As Integer
Dim Linha As Integer
Dim itm As ListItem, n As Long, lngCol As Long
Dim lastRow As Long

Sub preencheListView()
    Set ws = ThisWorkbook.Worksheets("Plan1")

    With UserForm1
        With .ListView1
            .AllowColumnReorder = False
            .ColumnHeaders.Clear
            .FullRowSelect = True
            .Gridlines = True
            .ListItems.Clear
            .LabelEdit = lvwManual
            .LabelWrap = False
            .View = lvwReport
            
            With .ColumnHeaders
                .Add , , "EQUIPA", 40
                .Add , , "DATA DISTRIBUIÇÃO", 90, lvwColumnCenter 'lvwColumnCenter
                .Add , , "DATA COMUNICAÇÃO", 90, lvwColumnCenter 'lvwColumnRight
                .Add , , "TEMPO DE DEMORA", 125, lvwColumnCenter 'lvwColumnCenter
                .Add , , "DATA COMUNICAÇÃO", 90, lvwColumnCenter
                .Add , , "DATA DE ENTREGA", 80, lvwColumnCenter
                .Add , , "TEMPO FINALIZAÇÃO", 125, lvwColumnCenter
                .Add , , "TEMPO ENTREGA FINALIZAÇÃO", 125, lvwColumnCenter
                .Add , , "RELATÓRIO", 60, lvwColumnCenter
                .Add , , "DISTR/COM UTEIS", 80, lvwColumnCenter
                .Add , , "COM/ENTR UTEIS", 80, lvwColumnCenter
                .Add , , "DISTR/ENTR UTEIS", 80, lvwColumnCenter
            End With
            
            For n = 2 To ws.UsedRange.Rows.Count ' Adicionamos as linhas a iniciar pela linha 2 até a última preenchida
                'Se as colunas A e B tiverem valores diferente de nulo
                If ws.Cells(n, 1) <> "" And ws.Cells(n, 2) <> "" Then
                    'Adiciona o novo item
                    Set itm = .ListItems.Add(, , ws.Cells(n, 1))
                        'Adicionamos os itens de cada coluna a iniciar pela coluna 2 até a ultima coluna preenchida
                        For lngCol = 2 To ws.UsedRange.Columns.Count
                            'Adiciona o valor da coluna de incide lngCol
                            itm.ListSubItems.Add , , ws.Cells(n, lngCol)
                        Next lngCol
                End If
            Next n            
        End With
    End With
End Sub
Segue abaixo, modelo editado com a rotina acima.

Espero que te ajude.

Abs

Reinaldo

28-12-2017 14:43:35

Uma outra possibilidade e alterar, no codigo inicial onde está :
UsedRange.Rows.Count (ws.UsedRange.Rows.count)
Para
Cells(Cells.Rows.Count, "A").End(xlUp).Row (ws.Cells(Cells.Rows.Count, "A").End(xlUp).Row)
Assim será identificado a ultima linha com dados na coluna "A", independentemente de haver dados em outra coluna

PauloD

02-01-2018 09:23:13

em primeiro lugar
Um Bom Ano de 2018
em Segundo muito obrigado pela ajuda era mesmo o que eu pretendia
muito obrigado mesmo