PC Review


Reply
Thread Tools Rate Thread

Code to import data based on selection in two listboxes

 
 
Steven Taylor
Guest
Posts: n/a
 
      22nd Jun 2007
Hi All,



I would appreciate some help with the following:



1. I have a user form with two list boxes.

2. The first list box displays the name of all open excel workbooks.

3. The second list box will display the sheets of the workbook the user
selects

from the first list box.

4. I would like to add an import button to my user form.

5. The import button will import the data from the workbook selected in

the first list box and from the sheet selected in the second list box into

sheet1 of the template workbook where the user form is displayed.



All comments and suggestion welcome.



Thanks,



Steve



PS I am using excel 2002 and windows XP.



 
Reply With Quote
 
 
 
 
=?Utf-8?B?SGFsaW0=?=
Guest
Posts: n/a
 
      22nd Jun 2007
Hi,

Try to add these following codes to your prject:

'1. I have a user form with two list boxes.
Sub RetreiveShtName(BookSource As Workbook, _
BookDestination As Workbook)
Dim Wks As Worksheet

'2. The first list box displays the name of all open excel workbooks.
For Each Wks In BookSource.Sheets
ListBox1.AddItem BookSource.Name & "-" & Wks.Name
Next Wks

'3. The second list box will display the sheets of the workbook
'the user selects
For Each Wks In BookDestination.Sheets
ListBox2.AddItem BookDestination.Name & "-" & Wks.Name
Next Wks

'to avoid Null for listbox value that error may occured when
'commandbutton1_click (if null)
ListBox1.ListIndex = 0
ListBox2.ListIndex = 0

End Sub

'4. I would like to add an import button to my user form.
Sub RetreiveData(dSource As Range, dDest As Range)
dSource.Copy Destination:=dDest
End Sub

'5. The import button will import the data from the workbook
'selected in
'the first list box and from the sheet selected in the second list
'box into
'sheet1 of the template workbook where the user form is displayed.

Private Sub CommandButton1_Click()
Dim SpltName1 As Variant
Dim SpltName2 As Variant

SpltName1 = Split(ListBox1, "-")
SpltName2 = Split(ListBox2, "-")

RetreiveData _
Workbooks(SpltName1(0)).Sheets(SpltName1(1)).Range("A1"), _
Workbooks(SpltName2(0)).Sheets(SpltName2(1)).Range("A1")

End Sub

Private Sub UserForm_Initialize()
RetreiveShtName Workbooks("book1.xls"), Workbooks("book2.xls")
End Sub

I hope the help you...

--

Regards,

Halim


"Steven Taylor" wrote:

> Hi All,
>
>
>
> I would appreciate some help with the following:
>
>
>
> 1. I have a user form with two list boxes.
>
> 2. The first list box displays the name of all open excel workbooks.
>
> 3. The second list box will display the sheets of the workbook the user
> selects
>
> from the first list box.
>
> 4. I would like to add an import button to my user form.
>
> 5. The import button will import the data from the workbook selected in
>
> the first list box and from the sheet selected in the second list box into
>
> sheet1 of the template workbook where the user form is displayed.
>
>
>
> All comments and suggestion welcome.
>
>
>
> Thanks,
>
>
>
> Steve
>
>
>
> PS I am using excel 2002 and windows XP.
>
>
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Code to Populate text field based on checkbox selection JoMoe Microsoft Access Forms 2 17th Sep 2009 01:33 PM
Re: UK Based data format in ListBoxes Bob Phillips Microsoft Excel Programming 1 29th Aug 2008 04:11 PM
import worksheet based on a selection =?Utf-8?B?Sm9obiBDYXN0ZWVs?= Microsoft Excel Misc 0 15th Jun 2006 06:48 AM
Single Selection ListBoxes jacqui Microsoft Excel Programming 4 10th Feb 2004 03:16 PM
Highlighted Selection in Listboxes David J. Microsoft Access Form Coding 2 30th Nov 2003 07:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:04 PM.