?
Solved

Select from two List boxes and send to workbook

Posted on 2012-03-21
16
Medium Priority
?
230 Views
Last Modified: 2012-03-21
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
0
Comment
Question by:Seamus2626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 3

Expert Comment

by:Frank White
ID: 37747340
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
 

Author Comment

by:Seamus2626
ID: 37747371
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
 
LVL 34

Expert Comment

by:Norie
ID: 37747456
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Seamus2626
ID: 37747741
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
 
LVL 34

Expert Comment

by:Norie
ID: 37747833
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
 
LVL 34

Expert Comment

by:Norie
ID: 37747998
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
 

Author Comment

by:Seamus2626
ID: 37748065
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
 
LVL 34

Expert Comment

by:Norie
ID: 37748227
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
 

Author Comment

by:Seamus2626
ID: 37748298
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
 
LVL 34

Expert Comment

by:Norie
ID: 37748312
Seamus

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

Open in new window

0
 

Author Comment

by:Seamus2626
ID: 37748383
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
 
LVL 34

Expert Comment

by:Norie
ID: 37748461
In the rest of the code for the button click to you use NextRow?
0
 

Author Comment

by:Seamus2626
ID: 37748637
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
 
LVL 34

Accepted Solution

by:
Norie earned 2000 total points
ID: 37748734
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
 

Author Closing Comment

by:Seamus2626
ID: 37748880
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
 
LVL 34

Expert Comment

by:Norie
ID: 37749072
Seamus

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

Might help clarify things.

Obviously remove any sensitive data.:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question