convert code to search data horizontally and show data vertically

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
240
Office Version
  1. 2019
  2. 2010
hi
I have this code works with vertical data in sheet and show in listbox based on search into textbox1 based on column D . now I want change the code based on horizontal data in sheet but show the data should be vertical in listbox based on search into textbox based on first row contain CODE as in A1 I take the example in sheet example how should show the data in listbox on userform when search for CCS-1

orginal data

VR.xlsm
ABCDE
1CODECCS-1CCS-2CCS-3CCS-4
2CLIENT NOCC2-TRB-1CC2-TRB-2CC2-TRB-3CC2-TRB-4
3INVOICE NOINV-TRGG-1INV-TRGG-2INV-TRGG-3INV-TRGG-4
4ID-CCIDTR-100IDTR-101IDTR-102IDTR-103
5BB-RFFDOOD-1FFDOOD-2FFDOOD-3FFDOOD-4
6TT-YPACK-2PACK-3PACK-4PACK-5
7OR-GGTTRBBRLLRMMR
8QTY60.0010510
9PRICE$120.00$130.00$100.00$289.00
10TOTAL$7,200.00$1,300.00$500.00$2,890.00
PURCHASE


this is example should be show in listbox when search
VR.xlsm
ABCDEFGHIJ
1CODECLIENT NOINVOICE NOID-CCBB-RTT-YOR-GGQTYPRICETOTAL
2CCS-1CC2-TRB-1INV-TRGG-1IDTR-100FFDOOD-1PACK-2TTR60.00$120.00$7,200.00
EXAMPLE


VBA Code:
Option Explicit

Dim a As Variant

Private Sub TextBox1_Change()
  Call FilterData
End Sub

Private Sub TextBox2_Change()
  Call FilterData
End Sub

Private Sub TextBox3_Change()
  Call FilterData
End Sub

Sub FilterData()
    Dim i As Long, ii As Long, n As Long
    Me.ListBox1.List = a
    If Me.TextBox1 = "" Then Exit Sub
    With Me.ListBox1
        .Clear
        For i = 0 To UBound(a, 1)
            If UCase$(a(i, 3)) Like UCase$(Me.TextBox1) & "*" Then
                .AddItem
                .List(n, 0) = n + 1
                For ii = 1 To UBound(a, 2)
                    .List(n, ii) = a(i, ii)
                Next
                n = n + 1
            End If
        Next
    End With
End Sub
Private Sub UserForm_Initialize()
    Dim lindex&
    Dim rngDB As Range, rng As Range
    Dim i, myFormat(1) As String
    Dim sWidth As String
    Dim vR() As Variant
    Dim n As Integer
    Dim myMax As Single
    Set rngDB = Range("A2:E20")
    For Each rng In rngDB
        n = n + 1
        ReDim Preserve vR(1 To n)
        vR(n) = rng.EntireColumn.Width
    Next rng
    myMax = WorksheetFunction.Max(vR)
    For i = 1 To n
        vR(i) = myMax
    Next i
    With Sheets("purchase").Cells(1).CurrentRegion
        myFormat(0) = .Cells(2, 8).NumberFormatLocal
        myFormat(1) = .Cells(2, 9).NumberFormatLocal
        Set rng = .Offset(1).Resize(.Rows.Count - 1)
        a = .Cells(1).CurrentRegion.Value
    End With

    sWidth = Join(vR, ";")
    Debug.Print sWidth
    With ListBox1
        .ColumnCount = 10
        .ColumnWidths = sWidth '<~~ 63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63
        .List = rng.Value
        .BorderStyle = fmBorderStyleSingle
        For lindex = 0 To .ListCount - 1
            '.List(lindex, 0) = (Format((.List(lindex, 0)), "dd/mm/yyyy"))   ' BL = dates
            .List(lindex, 0) = lindex + 1
            .List(lindex, 7) = Format$(.List(lindex, 7), myFormat(0))
            .List(lindex, 8) = Format$(.List(lindex, 8), myFormat(1))
            .List(lindex, 9) = Format$(.List(lindex, 9), myFormat(1))
        Next
        a = .List  '<--- this line
    End With
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
An alternative is to transpose your data with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Transposed Table" = Table.Transpose(Source),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true])
in
    #"Promoted Headers"
 
Upvote 0
thanks but I would do that directly in listbox . not depends on sheet . just to understand how should show data in listbox
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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