Listbox - defining columns

jeribeiro

New Member
Joined
Mar 22, 2023
Messages
1
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have this listbox to pull the information and filter it.

However, I put information from 3 columns starting with column B, and defined that it would pull through the offset the information of a column before and a column after.

It's working... but I'd like to filter the information from column F and that the information from column A and column H also appear in the listbox.

So what I'm looking for is for my listbox to appear as follows:

Code (Column A) | City with filter option (Column F) | Country (Column H)

I really appreciate anyone who can help me :D



listboxtest.xlsm
ABCDEFGH
1CodigoNomeTelefoneEnderecoBairroCidadeUFPais
21Joaquim7654821455Jardim Santana 12Joaquim de foraNilopolisAMBrasil
32Jessica7654821465Jardim Santana 55Felicidade do PaiFelicidade do PaiPAMexico
43Bernardo7654821475Jardim Santana 98MajuçaraCajueiro do NorteRJUSA
54Julia7654821485Jardim Santana 141Cajueiro do NorteCajueiro do NorteRJBrasil
65Caio7654821495Jardim Santana 184Morango do NordesteMajuçaraSPAfrica
76Dominique7654821505Jardim Santana 227BrasilMajuçaraMGFranca
87Karina7654821515Jardim Santana 270Estados separadosFelicidade do PaiESUSA
98Matheus7654821525Jardim Santana 313Estados unidosMorango do NordesteTOJapao
109Pedro7654821535Jardim Santana 356Jardim Santana 270Cajueiro do NorteRSBrasil
1110Raissa7654821545Jardim Santana 399BrasilCajueiro do NorteREBrasil
Teste1



code:



VBA Code:
Option Explicit

Dim wPlan As Worksheet



Private Sub cmdfechar_click()
Unload Me

End Sub


Private Sub listaClientes_Dblclick(ByVal Cancel As MSForms.ReturnBoolean)
   
On Error Resume Next

Dim n As Long
Dim ncliente As String

n = Format(Me.listaClientes.List(listaClientes.ListIndex, 0), "0")
ncliente = Me.listaClientes.List(listaClientes.ListIndex, 1)


Cadastroclientes.txtcodigo.Value = n

Unload Me


End Sub





Private Sub UserForm_Activate()
Call CriaCabecalhoLb(Me.listaClientes, Me.lbCab, Array("Cód.", "Cliente", "Celular"))

End Sub

Private Sub UserForm_Initialize()

    Dim rg      As Range
    Dim linf    As Integer
    Dim Cor01   As Variant
    Dim Cor02   As Variant
    Dim Cor03   As Variant
    Dim vLin    As Integer

    Cor01 = RGB(35, 207, 222) 'RGB(253, 6, 100)
    Cor02 = RGB(43, 46, 51) 'RGB(231, 232, 237)

    Set wPlan = Planilha1
    Set wPlan = Application.Worksheets("Teste1")
   
   
     vLin = Application.WorksheetFunction.CountA(wPlan.Range("B:B"))
       
        Set rg = wPlan.Range("B2:B" & vLin)
       
  
    listaClientes.ForeColor = Cor01
    txtCliente.ForeColor = Cor01
    listaClientes.BackColor = Cor02
   
    For linf = 1 To rg.Rows.Count
   
        With Me.listaClientes
       
            .ColumnWidths = "60;190;100"
            .ColumnCount = 3
            .AddItem Format(rg.Cells(linf, 0), "00000")
            .List(listaClientes.ListCount - 1, 1) = rg.Cells(linf, 1)
            .List(listaClientes.ListCount - 1, 2) = rg.Cells(linf, 2)
           
           
        End With
       
    Next
   
    Me.Contalbl.Caption = Me.listaClientes.ListCount & " clientes"

End Sub

Private Sub txtCliente_Change()

    Dim lstCli  As Range
    Dim vProc   As Range
    Dim vCod    As Range
    Dim vCel    As Range
    Dim vInic   As Range
    Dim vLin    As Integer
    Dim vteste  As Range
    Dim vtest   As Range
   
   
    On Error Resume Next
    Me.listaClientes.Clear
    If Len(Me.txtCliente) = 0 Then
        Call UserForm_Initialize
        lblPesq.Visible = True
    Else

    vLin = Application.WorksheetFunction.CountA(wPlan.Range("S:S"))
       
        Set lstCli = wPlan.Range("B2:B" & vLin)
        Set vProc = lstCli.Find(Me.txtCliente, , , xlPart)
        Set vCod = vProc.Offset(0, -1) 'valores do codigo, mudar a coluna para esquerda (0 = coluna S, 1 = coluna S - 1)
        Set vCel = vProc.Offset(0, 1) 'valores do celular, mudar a coluna para direita (0 = coluna S, 1 = coluna S + 1)
       
        lblPesq.Visible = False
       
        If Not vProc Is Nothing Then
       
            Set vInic = vProc
           
                Do
                    With Me.listaClientes

                        .ColumnWidths = "60;190;100"
                        .ColumnCount = 3
                        .AddItem Format(vCod, "00000")
                        .List(listaClientes.ListCount - 1, 1) = vProc
                        .List(listaClientes.ListCount - 1, 2) = vCel
                       
                       
                    End With
                   
                    Set vProc = lstCli.FindNext(vProc)
                    Set vCod = vProc.Offset(0, -1)
                    Set vCel = vProc.Offset(0, 1)
                   
                Loop Until vProc.Address = vInic.Address
           
        End If
        Me.Contalbl.Caption = Me.listaClientes.ListCount & " clientes"
    End If
   
End Sub



Public Sub CriaCabecalhoLb(LbPrincipal As MSForms.ListBox, LbCabecalho As MSForms.ListBox, cabecalho As Variant)
   
    With LbCabecalho ' cabecalho do userform

        'Iguala o numeros de colunas do ListBox Cabeçalho ao do ListBox Principal
        .ColumnCount = LbPrincipal.ColumnCount
        .ColumnWidths = LbPrincipal.ColumnWidths
       
        'Adiciona os elementos dos cabeçalhos
        .Clear
        .AddItem
        Dim i As Integer
        For i = 0 To UBound(cabecalho)
            .List(0, i) = cabecalho(i)
        Next i
       
        'Formata o visual
        .ZOrder (0)
        .Font.Size = 9
        .Font.Bold = True
        .SpecialEffect = fmSpecialEffectFlat
        .BackColor = RGB(35, 207, 222) 'RGB(229, 13, 90)
        .Height = 13
       
        'Alinha a posição e dimensões do ListBox Cabeçalho ao ListBox Principal
        .Width = LbPrincipal.Width
        .Left = LbPrincipal.Left
        .Top = LbPrincipal.Top - (.Height - 1)
   
    End With
   
    LbPrincipal.ZOrder (1)

End Sub
 

Attachments

  • 1.JPG
    1.JPG
    43.3 KB · Views: 5
Last edited by a moderator:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
To add Columns A, F, and H to your ListBox, try...

VBA Code:
    With Me.listaClientes
    
        .ColumnWidths = "60;190;100"
        .ColumnCount = 3
        
        For linf = 1 To rg.Rows.Count
            .AddItem Format(rg.Cells(linf, 1), "00000")
            .List(.ListCount - 1, 1) = rg.Cells(linf, 6)
            .List(.ListCount - 1, 2) = rg.Cells(linf, 8)
        Next linf
      
    End With

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,951
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top