Can you help me modify this code?
How to control the number of columns and the display of columns and column numbers in the search code in ListBox using TextBox, assuming that TextBox1 is for the number of columns, TextBox2 is for column numbers, and TextBox3 is for column width
How to control the number of columns and the display of columns and column numbers in the search code in ListBox using TextBox, assuming that TextBox1 is for the number of columns, TextBox2 is for column numbers, and TextBox3 is for column width
VBA Code:
Dim X, ws As Worksheet, i As Long, j As Long, lastRow As Long
With Me.ListBox2
.Clear
.ColumnCount = 13
.ColumnWidths = "64 pt;150 pt;63 pt;63 pt;62 pt;62 pt;62 pt;62 pt;62 pt;62 pt;62 pt;62 pt;62 pt;62"
Set ws = ThisWorkbook.Sheets("data")
X = Application.Match(ComboBox1.Value, ws.Rows(2), 0)
If Not IsError(X) Then
lastRow = ws.Cells(Rows.Count, "g").End(xlUp).Row
Dim a, myCols, ii As Long
ReDim a(1 To 13, 1 To lastRow)
myCols = Array(8, 7, 46, 24, 20, 27, 131, 44, 47, 48, 49, 50, 52)
For i = 1 To lastRow
If Tx1 <> "" And InStr(ws.Cells(i, X), Tx1) <> 0 Then
j = j + 1
For ii = 0 To UBound(myCols)
a(ii + 1, j) = ws.Cells(i, myCols(ii)).Value
Next
End If
Next i
For i = 1 To lastRow
If Tx1 = "" Then
j = j + 1
For ii = 0 To UBound(myCols)
a(ii + 1, j) = ws.Cells(i, myCols(ii)).Value
Next
End If
Next i
ReDim Preserve a(1 To UBound(a, 1), 1 To j)
.Column = a
End If
End With