listbox items to string with offset

skacutter

New Member
Joined
Dec 23, 2016
Messages
18
Hello Mr Excel,

I am trying to bounce each value in a listbox against a column in another sheet to match the value then extract email address from another column. I am using code borrowed from the web and adapting it slightly. We are getting (Runtime error 91; Object variable or with block variable not set) on the second line. we do not know how strEmail or strTo should be declared but we have tried with strEmail set as range and strTo set as string. The code is picking up the first item in the listbox and selecting the correct sheet and column to match against. what might we be doing wrong with " Set strEmail = ws.Range("A:A").Find(What:=ListBox1.List(i), LookAt:=xlWhole, MatchCase:=False, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)"



For i = 0 To ListBox1.ListCount - 1
Set strEmail = ws.Range("A:A").Find(What:=ListBox1.List(i), LookAt:=xlWhole, MatchCase:=False, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
strTo = strTo & ";" & strEmail.Offset(0, 2).Value
Next i


we used this post to construct >> Excel VBA - How to insert ListBox Values to a string? - Stack Overflow
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
At a quick glance, is ws. defined further up in the code? ws. is either a sheet or a workbook right? (thus an Object)
Secondly, a form is also an object
ListBox1.List may need to have the userform included, such as Userform1.Listbox1.List
 
Upvote 0

Forum statistics

Threads
1,214,901
Messages
6,122,157
Members
449,068
Latest member
shiz11713

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