listview (Resolvido)

Anonymous

28-02-2012 14:02:57

Pessoal preciso de ajuda em duas coisas:
1º) no listview abaixo me mostra primeiro a última linha e depois a penúltima, sucessivamente. Quero que me mostre em ordem de registro. o que mudar?
2º) onde altero a linha cabeçalho? Mostra nomes na linha de cabeçalho que não consigo ver onde estão.... no exemplo baixado mostra na linha cabeçalho (furo, nomemina, etc...) não sei onde alterar.
Obrigado


Private Const NomePlanilha As String = "Fornecedores"
Private Const LinhaCabecalho As Integer = 1


Private Sub ComboBoxCampos_Change()
Me.TextBoxFiltro.SetFocus
End Sub


Private Sub UserForm_Initialize()

Call PreencheCampos
Me.ComboBoxCampos.ListIndex = 1
' Adiciona as colunas
'adicionei em propriedades>personalizar>ColumnHeaders
With ListView1
.FullRowSelect = True 'seleciona linha
.View = lvwReport
' .ColumnHeaders.Add Text:="Id", Width:=40
' .ColumnHeaders.Add Text:="Nome", Width:=80
' .ColumnHeaders.Add Text:="Endereço", Width:=80
' .ColumnHeaders.Add Text:="Valor", Width:=70
' .ColumnHeaders.Add Text:="Index", Width:=40
.Gridlines = True 'efeito grade

End With
'...................................................................
' Adiciona itens
Dim i As Integer
lastRow = PlanFornecedores.Cells(Cells.Rows.Count, "a").End(xlUp).Row
For i = 2 To lastRow
ListView1.ListItems.Add 1, , Format(PlanFornecedores.Range("A" & i).Value, "000")
ListView1.ListItems(1).ListSubItems.Add 1, , PlanFornecedores.Range("B" & i).Value
ListView1.ListItems(1).ListSubItems.Add 2, , PlanFornecedores.Range("C" & i).Value
ListView1.ListItems(1).ListSubItems.Add 3, , PlanFornecedores.Range("D" & i).Value
ListView1.ListItems(1).ListSubItems.Add 4, , PlanFornecedores.Range("E" & i).Value
ListView1.ListItems(1).ListSubItems.Add 5, , PlanFornecedores.Range("F" & i).Value
ListView1.ListItems(1).ListSubItems.Add 6, , PlanFornecedores.Range("G" & i).Value
ListView1.ListItems(1).ListSubItems.Add 7, , PlanFornecedores.Range("H" & i).Value
ListView1.ListItems(1).ListSubItems.Add 8, , PlanFornecedores.Range("I" & i).Value
ListView1.ListItems(1).ListSubItems.Add 9, , PlanFornecedores.Range("J" & i).Value
ListView1.ListItems(1).ListSubItems.Add 10, , PlanFornecedores.Range("K" & i).Value
ListView1.ListItems(1).ListSubItems.Add 11, , PlanFornecedores.Range("L" & i).Value
ListView1.ListItems(1).ListSubItems.Add 12, , PlanFornecedores.Range("M" & i).Value
Next i
'teste





End Sub


Private Sub ListView1_dblClick()
Dim j As Integer
j = ListView1.SelectedItem.ListSubItems.Item(12).Text
frmCadastro.txtCodigo = PlanFornecedores.Range("A" & j).Value
'Me.TextBox3.Text = PlanFornecedores.Range("C" & j).Value
'Me.TextBox4.Text = Format(PlanFornecedores.Range("D" & j).Value, "#,##0.00")
'Me.TextBox2.Text = PlanFornecedores.Range("B" & j).Value
'Me.lblIndex.Caption = PlanFornecedores.Range("M" & j).Value
frmCadastro.PESQUISA
Unload Me
End Sub
Private Sub TextBoxFiltro_Change()
Dim strObjetoBuscar As String
Dim lngResultado As Long
Dim lngColumna As Long, lngFila As Long
Dim X As Integer, n As Integer
Dim col
coluna = Me.ComboBoxCampos.ListIndex + 1
ListView1.ListItems.Clear
strObjetoBuscar = TextBoxFiltro.Value
If strObjetoBuscar = "" Then GoTo 99
strObjetoBuscar = LCase(strObjetoBuscar)
For a = 2 To 2010
'lngResultado = InStr(1, PlanFornecedores.Cells(a, 2), strObjetoBuscar, vbTextCompare)
lngResultado = InStr(1, PlanFornecedores.Cells(a, coluna), strObjetoBuscar, vbTextCompare)
If lngResultado > 0 Then
ListView1.ListItems.Add 1, , Format(PlanFornecedores.Range("A" & a).Value, "000")
ListView1.ListItems(1).ListSubItems.Add 1, , PlanFornecedores.Range("B" & a).Value
ListView1.ListItems(1).ListSubItems.Add 2, , PlanFornecedores.Range("C" & a).Value
ListView1.ListItems(1).ListSubItems.Add 3, , PlanFornecedores.Range("D" & a).Value
ListView1.ListItems(1).ListSubItems.Add 4, , PlanFornecedores.Range("E" & a).Value
ListView1.ListItems(1).ListSubItems.Add 5, , PlanFornecedores.Range("F" & a).Value
ListView1.ListItems(1).ListSubItems.Add 6, , PlanFornecedores.Range("G" & a).Value
ListView1.ListItems(1).ListSubItems.Add 7, , PlanFornecedores.Range("H" & a).Value
ListView1.ListItems(1).ListSubItems.Add 8, , PlanFornecedores.Range("I" & a).Value
ListView1.ListItems(1).ListSubItems.Add 9, , PlanFornecedores.Range("J" & a).Value
ListView1.ListItems(1).ListSubItems.Add 10, , PlanFornecedores.Range("K" & a).Value
ListView1.ListItems(1).ListSubItems.Add 11, , PlanFornecedores.Range("L" & a).Value
ListView1.ListItems(1).ListSubItems.Add 12, , PlanFornecedores.Range("M" & a).Value



End If
Next a
99:
End Sub
'######################################################
Private Sub PreencheCampos()
Dim ws As Worksheet
Dim coluna As Integer
Dim linha As Integer
Set ws = ThisWorkbook.Worksheets(NomePlanilha)
coluna = 1 'de 1 para 2
linha = LinhaCabecalho

With ws
While .Cells(linha, coluna).Value <> Empty
Me.ComboBoxCampos.AddItem .Cells(linha, coluna)
coluna = coluna + 1
If coluna = 13 Then Exit Sub
Wend
End With
End Sub

Private Sub PreencheCabecalho(ByRef Lista())
Dim ws As Worksheet
Dim coluna As Integer
Dim linha As Integer
Set ws = ThisWorkbook.Worksheets(NomePlanilha)
coluna = 1 'de 1 p 2
linha = LinhaCabecalho

With ws
While .Cells(linha, coluna).Value <> Empty
Lista(0, coluna - 1) = .Cells(linha, coluna) 'de -1 p -2
coluna = coluna + 1
Wend
End With
End Sub

Anonymous

28-02-2012 16:24:45

Para classificar achei um comando muito melhor, para variar do no fórum do Tomás, feito pelo amigo Mauro Coutinho.
Com este comando ele ordena em ordem crescente ou decrescente, independente da coluna. Isto é ótimo.


Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
' ordena a coluna ao clicar no título da mesma
ListView1.Sorted = False
ListView1.SortKey = ColumnHeader.Index - 1

If ListView1.SortOrder = lvwAscending Then
ListView1.SortOrder = lvwDescending
Else
ListView1.SortOrder = lvwAscending
End If

ListView1.Sorted = True
End Sub

Muito obrigado... Preciso achar o comando para alterar o cabeçalho

joseA

29-02-2012 12:13:04

A melhor explicação seria o arquivo, ainda que resumido.

Mauro Coutinho

29-02-2012 14:05:42

O Cabeçalho é formado pela rotina Private Sub PreencheCabecalho :

Private Sub PreencheCabecalho(ByRef Lista())
Dim ws As Worksheet
Dim coluna As Integer
Dim linha As Integer
'Define a o Nome da Aba onde estão o cabeçalgo e dados
Set ws = ThisWorkbook.Worksheets(NomePlanilha)
coluna = 1 'de 1 p 2
linha = LinhaCabecalho

With ws
While .Cells(linha, coluna).Value <> Empty 'Enquanto a Linha e Coluna não estiverem vazias
'Preenche o Cabeçalho do ListView que está nomeado como Lista
Lista(0, coluna - 1) = .Cells(linha, coluna) 'de -1 p -2
coluna = coluna + 1
Wend
End With
End Sub

[]s

Anonymous

29-02-2012 16:17:38

Boa tarde Mauro
Veja se consegue me ajudar.... no listview realmente não consigo tirar o cabeçalho e pode colocar ordem crescente dos dados tb.
Abraço

Mauro Coutinho

01-03-2012 00:12:58

Neri, deixa eu ver se entendi :

não consigo tirar o cabeçalho : Você quer que não apareça o cabeçalho ?
Se for isto, acrescente na rotina Private Sub UserForm_Initialize(), após a linha :
.View = lvwReport
'Esta instrução :
.HideColumnHeaders = True 'IráOcultar a Linha de Cabeçalho

Para Carrega o ListView classificado por ordem Crescente baseado na Primeira Coluna (COD), é só acrescentar ao final desta mesma rotina, apos Next i as instruções:
ListView1.Sorted = True
ListView1.SortOrder = lvwAscending


Espero que eu tenhaentendido corretamente.

Qualquer duvida, retorne.

[]s

Anonymous

01-03-2012 10:34:19

bom dia Mauro
Sempre pronto a ajudar. Muito obrigado

Quanto a colocar ordem alfabética ficou ótimo.

Quanto ao cabecalho não quero ocultar ele, porém alterar o cabeçalho. Nele aparecem os nomes como "mina; nome da mina, etc" e na verdade meu relatorio tem outros dados. Preciso saber onde alterar este cabeçalho. Já dancei muito com esta alteração (hehehee)
Att.

joseA

01-03-2012 12:27:56

Acho que vc adaptou um modelo antigo que havia criado. Acrescentava o nome das colunas em propriedades>Personalizado, no 2003 abria essa propriedade, agora tento (2007) e quando clico ocorre o aviso "Classe não registrada".

Se for isso, :? vc tem duas opções: delete o ListView1 e acrescente outro ListView1 ou acrescente uma linha para limpar os nomes da colunas antes do preenchimento.

Código: Selecionar todos

Me.ListView1.ColumnHeaders.Clear

Anonymous

01-03-2012 13:55:20

José
Peguei tantos exemplos que não lembrava de quem eram.... pode ser que seja o seu. Mas ficou ótimo. Atendeu a minha necessidade.
Muito obrigado pelo exemplo disponibilizado e pela informação que serviu para "apagar" os nomes do cabeçalho que estavam pré-definidos.
Com as colaborações de todos, temos trabalhos muito bons.

Anonymous

01-03-2012 14:54:42

Mauro
Ao colocar o comando abaixo funciona certinho. Porém ao utilizar o filtro, ele preenche a coluna corretamente, porém da B em diante somente a primeira linha....

Para Carrega o ListView classificado por ordem Crescente baseado na Primeira Coluna (COD), é só acrescentar ao final desta mesma rotina, apos Next i as instruções:
ListView1.Sorted = True
ListView1.SortOrder = lvwAscending

att.

joseA

01-03-2012 21:35:43

Esse Filtro "desengonçado" :lol: é quando ainda estava aprendendo sobre o controle.

No evento Initialize, troque as linhas que carregam os itens por essa:

Código: Selecionar todos

' Adiciona itens
'limpar
ListView1.ListItems.Clear
    
    lastRow = PlanFornecedores.Cells(PlanFornecedores.Cells.Rows.Count, "a").End(xlUp).Row
    
    ' Adiciona itens
   For i = 2 To lastRow
        Set li = ListView1.ListItems.Add(Text:=Format(PlanFornecedores.Cells(i, "a").Value, "00"))
        li.ListSubItems.Add Text:=Format(PlanFornecedores.Cells(i, "b").Value, "DD/MM/YYYY")
        li.ListSubItems.Add Text:=PlanFornecedores.Cells(i, "c").Value
        li.ListSubItems.Add Text:=PlanFornecedores.Cells(i, "d").Value
        li.ListSubItems.Add Text:=Format(PlanFornecedores.Cells(i, "e").Value, "@@@@@@@")
        li.ListSubItems.Add Text:=PlanFornecedores.Cells(i, "f").Value
        li.ListSubItems.Add Text:=PlanFornecedores.Cells(i, "g").Value
        li.ListSubItems.Add Text:=PlanFornecedores.Cells(i, "h").Value
        li.ListSubItems.Add Text:=PlanFornecedores.Cells(i, "i").Value
        li.ListSubItems.Add Text:=PlanFornecedores.Cells(i, "j").Value
        li.ListSubItems.Add Text:=Format(PlanFornecedores.Cells(i, "k").Value, "DD/MM/YYYY")
        li.ListSubItems.Add Text:=Format(PlanFornecedores.Cells(i, "l").Value, "currency")
        li.ListSubItems.Add Text:=Format(PlanFornecedores.Cells(i, "M").Value, "currency")
        li.ListSubItems.Add Text:=Format(PlanFornecedores.Cells(i, "N").Value, "currency")
        li.ListSubItems.Add Text:=Format(PlanFornecedores.Cells(i, "O").Value, "currency")
        li.ListSubItems.Add Text:=PlanFornecedores.Cells(i, "P").Value
        
        Next i

joseA

01-03-2012 21:40:13

Para não ficar bagunçado estou colocando separado.

No evento TextBoxFiltro_Change, troque:

Código: Selecionar todos

Private Sub TextBoxFiltro_Change()
Dim strObjetoBuscar As String
Dim lngResultado As Long
Dim lngColumna As Long, lngFila As Long
Dim x As Integer, n As Integer
Dim col
coluna = Me.ComboBoxCampos.ListIndex + 1
ListView1.ListItems.Clear
strObjetoBuscar = TextBoxFiltro.Value
If strObjetoBuscar = "" Then GoTo 99
strObjetoBuscar = LCase(strObjetoBuscar)
    For a = 2 To PlanFornecedores.Cells(PlanFornecedores.Cells.Rows.Count, "a").End(xlUp).Row
        lngResultado = InStr(1, PlanFornecedores.Cells(a, coluna), strObjetoBuscar, vbTextCompare)
            If lngResultado > 0 Then
 Set li = ListView1.ListItems.Add(Text:=Format(PlanFornecedores.Range("A" & a).Value, "00"))
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("B" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("C" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("D" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("E" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("F" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("G" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("H" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("I" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("J" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("K" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("L" & a).Value
                 li.ListSubItems.Add Text:=PlanFornecedores.Range("L" & a).Value
            End If
    Next a
99:

End Sub
Vai carregar em ordem. ;)