Can you help me modify this code?

saftawy1

Board Regular
Joined
Oct 12, 2021
Messages
65
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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
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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
When replacing the column numbers with the name TextBox2, the following error appears, what is the solution?
 

Attachments

  • Capture.JPG
    Capture.JPG
    35 KB · Views: 8
  • Capture2.JPG
    Capture2.JPG
    17.1 KB · Views: 8
Upvote 0
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
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
crossposted
 
Upvote 0
I could not replicate your error. Check that you are not increasing "j" beyond your defined ubound "a" 2nd Dim.
On your error, check your value of "j" in your immediate window and see how it compares to value of "lastrow" that you defined "a" array. My guess is the error is "j" is greater.
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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