Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default populate combo box from worksheet

Hi All,

I am new to excel vba coding. I hope you can help me out. I have created a
combo box in Userform. Now I need to populate the data. The data is stored in
sheet1 of service.xls file. How can I populate my combo box with the data in
the service.xls file? I would use the .AddItem but there are a lot of data.

Thanks,
Tracktraining
--
Learning
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default populate combo box from worksheet

Maybe...

Option Explicit
Private Sub UserForm_Initialize()
Dim myRng As Range
Dim myCell As Range

'Service.xls has to be open!
With Workbooks("service.xls").Worksheets("sheet1")
Set myRng = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With

'can you use the entire range of values?
Me.ComboBox1.List = myRng.Value

'or if you wanted to loop
For Each myCell In myRng.Cells
Me.ComboBox1.AddItem myCell.Value
Next myCell

'or if you wanted to check a value
'and add the item from a different column
For Each myCell In myRng.Cells
If LCase(myCell.Value) = "some value here" Then
Me.ComboBox1.AddItem myCell.Offset(0, 1).Value
Next myCell

End Sub




tracktraining wrote:

Hi All,

I am new to excel vba coding. I hope you can help me out. I have created a
combo box in Userform. Now I need to populate the data. The data is stored in
sheet1 of service.xls file. How can I populate my combo box with the data in
the service.xls file? I would use the .AddItem but there are a lot of data.

Thanks,
Tracktraining
--
Learning


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default populate combo box from worksheet

thanks Dave!

Is there a way to do it without having the service.xls open?

thanks again.


--
Learning


"Dave Peterson" wrote:

Maybe...

Option Explicit
Private Sub UserForm_Initialize()
Dim myRng As Range
Dim myCell As Range

'Service.xls has to be open!
With Workbooks("service.xls").Worksheets("sheet1")
Set myRng = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With

'can you use the entire range of values?
Me.ComboBox1.List = myRng.Value

'or if you wanted to loop
For Each myCell In myRng.Cells
Me.ComboBox1.AddItem myCell.Value
Next myCell

'or if you wanted to check a value
'and add the item from a different column
For Each myCell In myRng.Cells
If LCase(myCell.Value) = "some value here" Then
Me.ComboBox1.AddItem myCell.Offset(0, 1).Value
Next myCell

End Sub




tracktraining wrote:

Hi All,

I am new to excel vba coding. I hope you can help me out. I have created a
combo box in Userform. Now I need to populate the data. The data is stored in
sheet1 of service.xls file. How can I populate my combo box with the data in
the service.xls file? I would use the .AddItem but there are a lot of data.

Thanks,
Tracktraining
--
Learning


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default populate combo box from worksheet

You could use a bunch of formulas in a hidden sheet that point back at the cells
you want brought back.

There are other ways, but that seems easiest to me.

=======
On the other hand, your code could open the workbook, add the info to the
combobox, close the workbook.

Option Explicit
Private Sub UserForm_Initialize()
Dim myRng As Range
Dim myCell As Range
Dim ServWkbk as workbook

application.screenupdating = false 'hide it from the user

set servwkbk = nothing
on error resume next
set servwkbk = workbooks.open(filename:="C:\folder\services.xls", _
readonly:=true)
on error goto 0

if servwkbk is nothing then
msgbox "file not found"
else
'Service.xls has to be open!
With servwkbk.worksheets("Sheet1")
Set myRng = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With

'can you use the entire range of values?
Me.ComboBox1.List = myRng.Value

'or if you wanted to loop
For Each myCell In myRng.Cells
Me.ComboBox1.AddItem myCell.Value
Next myCell

'or if you wanted to check a value
'and add the item from a different column
For Each myCell In myRng.Cells
If LCase(myCell.Value) = "some value here" Then
Me.ComboBox1.AddItem myCell.Offset(0, 1).Value
Next myCell
end if

application.screenupdating = true

End Sub

(untested, uncompiled. watch for typos!)
tracktraining wrote:

thanks Dave!

Is there a way to do it without having the service.xls open?

thanks again.

--
Learning

"Dave Peterson" wrote:

Maybe...

Option Explicit
Private Sub UserForm_Initialize()
Dim myRng As Range
Dim myCell As Range

'Service.xls has to be open!
With Workbooks("service.xls").Worksheets("sheet1")
Set myRng = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With

'can you use the entire range of values?
Me.ComboBox1.List = myRng.Value

'or if you wanted to loop
For Each myCell In myRng.Cells
Me.ComboBox1.AddItem myCell.Value
Next myCell

'or if you wanted to check a value
'and add the item from a different column
For Each myCell In myRng.Cells
If LCase(myCell.Value) = "some value here" Then
Me.ComboBox1.AddItem myCell.Offset(0, 1).Value
Next myCell

End Sub




tracktraining wrote:

Hi All,

I am new to excel vba coding. I hope you can help me out. I have created a
combo box in Userform. Now I need to populate the data. The data is stored in
sheet1 of service.xls file. How can I populate my combo box with the data in
the service.xls file? I would use the .AddItem but there are a lot of data.

Thanks,
Tracktraining
--
Learning


--

Dave Peterson


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default populate combo box from worksheet

And I forgot the close statement in the code!

...all that code
servwkbk.close savechanges:=false '<--- added
application.screenupdating = true

End Sub

Dave Peterson wrote:

You could use a bunch of formulas in a hidden sheet that point back at the cells
you want brought back.

There are other ways, but that seems easiest to me.

=======
On the other hand, your code could open the workbook, add the info to the
combobox, close the workbook.

Option Explicit
Private Sub UserForm_Initialize()
Dim myRng As Range
Dim myCell As Range
Dim ServWkbk as workbook

application.screenupdating = false 'hide it from the user

set servwkbk = nothing
on error resume next
set servwkbk = workbooks.open(filename:="C:\folder\services.xls", _
readonly:=true)
on error goto 0

if servwkbk is nothing then
msgbox "file not found"
else
'Service.xls has to be open!
With servwkbk.worksheets("Sheet1")
Set myRng = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With

'can you use the entire range of values?
Me.ComboBox1.List = myRng.Value

'or if you wanted to loop
For Each myCell In myRng.Cells
Me.ComboBox1.AddItem myCell.Value
Next myCell

'or if you wanted to check a value
'and add the item from a different column
For Each myCell In myRng.Cells
If LCase(myCell.Value) = "some value here" Then
Me.ComboBox1.AddItem myCell.Offset(0, 1).Value
Next myCell
end if

application.screenupdating = true

End Sub

(untested, uncompiled. watch for typos!)
tracktraining wrote:

thanks Dave!

Is there a way to do it without having the service.xls open?

thanks again.

--
Learning

"Dave Peterson" wrote:

Maybe...

Option Explicit
Private Sub UserForm_Initialize()
Dim myRng As Range
Dim myCell As Range

'Service.xls has to be open!
With Workbooks("service.xls").Worksheets("sheet1")
Set myRng = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With

'can you use the entire range of values?
Me.ComboBox1.List = myRng.Value

'or if you wanted to loop
For Each myCell In myRng.Cells
Me.ComboBox1.AddItem myCell.Value
Next myCell

'or if you wanted to check a value
'and add the item from a different column
For Each myCell In myRng.Cells
If LCase(myCell.Value) = "some value here" Then
Me.ComboBox1.AddItem myCell.Offset(0, 1).Value
Next myCell

End Sub




tracktraining wrote:

Hi All,

I am new to excel vba coding. I hope you can help me out. I have created a
combo box in Userform. Now I need to populate the data. The data is stored in
sheet1 of service.xls file. How can I populate my combo box with the data in
the service.xls file? I would use the .AddItem but there are a lot of data.

Thanks,
Tracktraining
--
Learning

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto Populate Combo Box When Opening a Worksheet [email protected] Excel Programming 2 July 3rd 07 01:47 PM
Populate one combo box based on the selection of another combo box Alex Excel Programming 8 April 19th 07 06:40 PM
using a worksheet range to populate a combo box in excel Kev[_7_] Excel Programming 37 March 30th 07 07:56 PM
Best way to populate worksheet from 2 combo boxes jswasson Excel Worksheet Functions 0 July 7th 06 01:21 PM
Populate a combo box from a worksheet with VBA Jack Excel Programming 1 January 13th 06 12:44 PM


All times are GMT +1. The time now is 08:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"