myFind Range not working

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
Please can someone say why this myFindRng goes red in the code?

VBA Code:
Private Sub Model_Chose_Change()

With ThisWorkbook.Worksheets("Job Card Master")

Me.Quote_Details.ListIndex = Me.Model_Chose.ListIndex
myValToFind = Make_Chose.Value
Set mySearchRng = .Columns("A")
End With

Set myFindRng = mySearchRng.Find(What:=myValToFind, _
           LookIn:=xlFormulas, _
           LookAt:=xlWhole, _
           SearchOrder:=xlByRows, _
           SearchDirection:=xlNext, _
           MatchCase:=False)
 
Last edited:
And how does the data get from the sheet to the listbox?
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Darren

If you are setting ColumnHeads to True it's more than likely you are using RowSource to populate the listbox.

Do you have any code in the UserForm_Initialize event or are you populating the listbox from other code?
 
Upvote 0
And how does the data get from the sheet to the listbox?
Through this code below

VBA Code:
Private Sub UserForm_Initialize()

Dim QDetails As MSForms.ListBox
Dim ws       As Worksheet
Dim RngData  As Range

Set QDetails = Body_And_Vehicle_Type_Form.Quote_Details

Set ws = ThisWorkbook.Sheets("Quote Detail")
With ws
    Set RngData = .Range("A1:J" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With

With QDetails
    .ColumnHeads = True
    .ColumnCount = RngData.Columns.Count
   
    Set RngData = RngData.Resize(RngData.Rows.Count - 1).Offset(1) 
    .RowSource = RngData.Address(0, 0, xlA1, xlExternal) 
   
    .ColumnWidths = "90;60;100;150;90;80;100;95;60"
   
End With

End Sub
 
Upvote 0
Darren

If you are setting ColumnHeads to True it's more than likely you are using RowSource to populate the listbox.

Do you have any code in the UserForm_Initialize event or are you populating the listbox from other code?
Your questions are answered above
 
Upvote 0
Darren

As Rory already mentioned you can't use AddItem when you populate a listbox using RowSource.

You have 2 options really.

1 Use List to initially populate the listbox.

2 When 'filtering' put the data you want in the listbox on a sheet somewhere and populate from there.

With option 1 you wouldn't be able to have column headers in the listbox, option 2 would involve a fair amount of extra code to move data about.
 
Upvote 0
Darren

As Rory already mentioned you can't use AddItem when you populate a listbox using RowSource.

You have 2 options really.

1 Use List to initially populate the listbox.

2 When 'filtering' put the data you want in the listbox on a sheet somewhere and populate from there.

With option 1 you wouldn't be able to have column headers in the listbox, option 2 would involve a fair amount of extra code to move data about.
I think number 2 is what I have.
I take the values from a worksheet??
What could I use instead of add to match Combo & List box?
 
Upvote 0
You aren't currently using either option, you are populating the listbox with a static range when the userform opens.

To use option 2 you would need to add code to copy data from that range to another range based on your criteria and then populate the listbox from the range you've copied to.

To use option 1 you would need to change the code in the userform Initialize event to use List instead of RowSource.
VBA Code:
With QDetails
    .ColumnCount = RngData.Columns.Count
    Set RngData = RngData.Resize(RngData.Rows.Count - 1).Offset(1)
    .List= RngData.Value
    .ColumnWidths = "90;60;100;150;90;80;100;95;60"
End With

You should then be able to use AddItem to populate the listbox with the result of you search, but you will need to clear it first.
 
Upvote 0
I think what you said above has worked.
Seems to be this code below goes Yellow now.
Don`t think the List count coding is very happy?:(
The error says "Object variable or with block variable not set"???

.List(.ListCount - 1, 0) = myFindRng.Value
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,447
Latest member
M V Arun

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