Link to home
Start Free TrialLog in
Avatar of Seamus2626
Seamus2626Flag for Ireland

asked on

Select from two List boxes and send to workbook

Hi,

I have attached a screenshot of a listbox i have created. I have populated the data, now i need code that allows the user to click on the book and click on then a book home and press "Enter Map"

On pressing "Enter Map" i want the book and book home to be sent to a workbook called ("Match.xl") sheet is called "Mapping Table" and the range it needs to be sent is K2:L2 to how many rows needed

So i need the user to press ctrl and select the book and book home and then the data to be sent

I am open to sending this data differently if this is not the most efficient way of sending the data

Thanks
Seamus
Doc4.doc
Avatar of Frank White
Frank White
Flag of Canada image

Seems you created a second question. Might want to call for a moderator to delete one of them.

Important detail: What language/software was this listbox window you created made in? Is it a VBA window that appears upon running a macro in Excel or Word, or is it a completely independent/separate C#/C++/VB.NET/.NET/etc. program that happens to contain that window? Is it run from the same workbook you want to send the data to (i.e. just send data from one worksheet to another?), or is it two different Excel workbooks (files)?
Avatar of Seamus2626

ASKER

Ive requested the delete

Its excel VBA. Its running through Excel only.

It picks data from a temporary sheet and fills the first list out

bolix
shite (test data!)

The second part

Paris
Hong kong etc

is held on a static table in the same WB the code is running from (match.xls)

So what im trying to do is union the test data with the static list and send them to a sheet in Match.xls

The whole point of the exercise is to fill a mapping table before some code is run.

Hope thats clearer!

Thanks
Seamus
Avatar of Norie
Norie

Seamus

Is this a userform?

If it is what are the names of the listboxes?

What do you mean exactly by a 'mapping' table?

Do you want to transfer the data from one listbox to one column, with the data from the other listbox in the column to the right.

Something like this.

Column A                        Column B
      
ListBox1Item1      Listbox2Item1
ListBox1Item2      Listbox2Item2
ListBox1Item3      Listbox2Item3

etc
Hi imnorie,

It is a userform and there is ListBox1+Listbox2

The mapping table is used in another part of the code and basically identifys trade books to areas, so "bolix" to "Paris"

Yes, exactly. I would like to drop them into two columns exactly as you have above

Thanks
Seamus
Try this, it assumes both listboxes have the same no of rows.
Dim I As Long

For I = 0 To Listbox1.Count-1
       Worksheets("Mapping Table").Range("K2").offset(I).Value = Listbox1.List(I)
       Worksheets("Mapping Table").Range("L2").Offset(I).Value = Listbox2.List(I)
Next I

Open in new window

Seamus

Here's something that doesn't use a loop.
Dim arrList

arrList = Listbox1.List

Range("K2").Resize(ubound(arrlist)).Value = arrList

arrList = Listbox2.List

Range("L2").Resize(ubound(arrlist)).Value = arrList

Open in new window

Hey Imnorie,

Ive just been thinking, i store the first list in the mapping table already In Column A, if the user selected the top list and then the appropriate area and pressed the button, if the selected data in the second list was send to

Mapping Table, Range("B1").Xlenddown.offset(1,0)

then it would line up with the area and be exactly what i needed

I know the syntax for the above is completely wrong!

But basically, once List2 was selected, they hit the button and it was sent to the next empty cell in Column B, it would line up!

Thanks
Seamus
Seamus

So you don't need the listboxes?

By the way, don't use xlDown to try and find the next empty row - it will work sometimes but there's also a chance it could cause problems.

Use xlDown instead:
' find next empty cell in column A
Set NextCell = Worksheets("Mapping Table").Range("A" & Rows.Count).End(xlUp).Offset(1)

Open in new window

Ya i need the Listboxes, i dont want the users to be able to get at the mapping table or the lists.

Set NextCell = Worksheets("Mapping Table").Range("A" & Rows.Count).End(xlUp).Offset(1)

I get a subscript out of range error here because the tab mapping table is in a wb called match.xls and i am running this out of spreadsheet called aspa.xls

Can you amend the above?

Thanks
Seamus
Seamus

Just add a workbook reference in front of Worksheets.
Workbooks("aspa.xls").Worksheets...

Open in new window

Thanks again Inmorie, on the command button i have this code

Set NextCell = Workbooks("Match.xls").Worksheets("Mapping Table").Range("B" & Rows.Count).End(xlUp).Offset(1)

this selects the correct cell in the mapping table but does not send the item from Listbox2 to that cell.
Am i missing a line?

Thanks
Seamus
In the rest of the code for the button click to you use NextRow?
Ok, so when the list boxes have been filled out (like on the word doc) the user presses the button

"Enter Map"

For that button i have your code


Private Sub SendingData_Click()



Set NextCell = Workbooks("Match.xls").Worksheets("Mapping Table").Range("B" & Rows.Count).End(xlUp).Offset(1)

End Sub


This correctly selects the cell in the sheet "Mapping Table"

What i want is for it also to send the piece of data in the second list box that the user has selected e.g. New York

Thanks
Seamus
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank inmorie,

Your code is working and sending the data.


I have other issues with my userform which il post tomorrow

Thanks fo your patience today!

Seamus
Seamus

Why not attach a workbook with the userform instead of just pictures?

Might help clarify things.

Obviously remove any sensitive data.:)