Can you help me solve these challenges in the search code in the list box?

saftawy1

Board Regular
Joined
Oct 12, 2021
Messages
65
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
This code works fine in the listbox, But when you try two scenarios, the following happens:
1) when replacing a line (myCols = Array(1, 3,4, 5, 7, 10) with TextBox and let TextBox 2 written inside it (1,3,4,5,7,10) it gives me an error message (application defined or object defined error
It is marked on the next line in the code ( a(ii + 1, j) = ws.Cells(i, myCols(ii)).Value) . What is the solution to this challenge?

2) when replacing a line (myCols = Array(1, 3,4, 5, 7, 10) with Textboxes containing separate column numbers are written as follows (myCols = Array( TextBox,3TextBox4, TextBox5,TextBox6 ,TextBox7, TextBox8) It works fine, but if one of the textboxes is empty, it gives me the following error message type mismatch
It is marked on the next line in the code ( a(ii + 1, j) = ws.Cells(i, myCols(ii)).Value) . What is the solution to this challenge?
VBA Code:
    Dim X, ws As Worksheet, i As Long, j As Long, lastRow As Long, y As Long
    With Me.ListBox1
        .Clear
        .ColumnCount = 6
        .ColumnWidths = ""
        Set ws = ThisWorkbook.Sheets("data")
        X = Application.Match(ComboBox1.Value, ws.Rows(1), 0)
        If Not IsError(X) Then
            lastRow = ws.Cells(Rows.Count, "e").End(xlUp).Row
                        Dim a, myCols, ii As Long
            ReDim a(1 To 6, 1 To lastRow)
            myCols = Array(1, 3, 4, 5, 7, 10)
            For i = 1 To lastRow
                If TextBox1 <> "" And InStr(ws.Cells(i, X), TextBox1) <> 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 TextBox1 = "" 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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Scenario 1:
VBA Code:
myCols = Split(TextBox2, ",")

Scenario 2:
VBA Code:
Dim s As String
s = TextBox3 & "," & TextBox4 & "," & TextBox5 & "," & TextBox6 & "," & TextBox7 & "," & TextBox8
Do While InStr(1, s, ",,") > 0
    s = Replace(s, ",,", ",")
Loop
myCols = Split(s, ",")
 
Upvote 0
Scenario 1:
VBA Code:
myCols = Split(TextBox2, ",")

Scenario 2:
VBA Code:
Dim s As String
s = TextBox3 & "," & TextBox4 & "," & TextBox5 & "," & TextBox6 & "," & TextBox7 & "," & TextBox8
Do While InStr(1, s, ",,") > 0
    s = Replace(s, ",,", ",")
Loop
myCols = Split(s, ",")
in two scenarios :
it gives me an error message (application defined or object defined error
It is marked on the next line in the code ( a(ii + 1, j) = ws.Cells(i, myCols(ii)).Value)
 
Upvote 0

Forum statistics

Threads
1,223,155
Messages
6,170,405
Members
452,325
Latest member
BlahQz

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