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
Seamus2626Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Frank WhiteCommented:
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)?
0
Seamus2626Author Commented:
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
0
NorieAnalyst Assistant Commented:
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
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Seamus2626Author Commented:
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
0
NorieAnalyst Assistant Commented:
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

0
NorieAnalyst Assistant Commented:
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

0
Seamus2626Author Commented:
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
0
NorieAnalyst Assistant Commented:
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

0
Seamus2626Author Commented:
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
0
NorieAnalyst Assistant Commented:
Seamus

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

Open in new window

0
Seamus2626Author Commented:
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
0
NorieAnalyst Assistant Commented:
In the rest of the code for the button click to you use NextRow?
0
Seamus2626Author Commented:
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
0
NorieAnalyst Assistant Commented:
Seamus

I'm totally confused, you now only want data from Listbox2 to be written to the worksheet.

The code for that would be this.
Private Sub SendingData_Click()
Dim NextCell As Range
Dim arrList

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

     arrList = Listbox2.List

     NextCell.Resize(UBound(arrlist)).Value = arrList

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Seamus2626Author Commented:
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
0
NorieAnalyst Assistant Commented:
Seamus

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

Might help clarify things.

Obviously remove any sensitive data.:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.