Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Populate ListBox with Filtered Records

Using Excel 2003.

I have a database worksheet named Projects, with three columns of
information: Id, Name, and Location. The worksheet currently has over 200
records.

My VBA form (frmMainMenu) has a ComboBox (cboSelectLocation), CommandButton
(cmdListProjects), and a ListBox (lstProjectsByLocation).

Using the VBA code example from http://j-walk.com/ss/excel/tips/tip47.htm, I
am able to populate the ComboBox with a list of unique, nonduplicated
Locations from the Projects worksheet.

What I am getting stuck on is when the user selects the Location from the
ComboBox, clicking the CommandButton should populate the ListBox with all the
records that match the Location (a filtered list by Location, if you will).

Can anyone help with the VBA code that would sort through the records of the
worksheet to select only the ones that match the Location?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 182
Default Populate ListBox with Filtered Records

Hi,

I've created this one:
Sub FindTxt()
Dim FindX As Object
Dim FindTxt As String
Dim FindLoop As Object
Dim FindNextx As Object
Dim FindLoopAddr As Object
Dim Teks As String, R As Long
ListBox1.ColumnCount = 2
ListBox1.Clear
FindTxt = TextBox1.Text
'On Error GoTo Err

Set FindLoopAddr = Cells
Set FindX = FindLoopAddr.Find(FindTxt, LookIn:=xlValues, LookAt:=xlPart,
SearchOrder:=xlByRows)

If Not FindX Is Nothing Then
ListBox1.AddItem FindX.Address
ListBox1.List(0, 1) = FindX.Value
'To handle next search ... until looped to the begining search :
Set FindNextx = FindX
Do
Set FindLoop = FindLoopAddr.FindNext(After:=FindNextx)
If Not FindLoop Is Nothing Then
ListBox1.AddItem FindLoop.Address
R = R + 1
ListBox1.List(R, 1) = FindLoop.Value
If FindLoop.Offset(0, -(FindLoop.Column - 1)).Value < ""
Then _
ListBox1.List(R, 2) = _
FindLoop.Offset(0, -(FindLoop.Column - 1)).Value

'To handle if trapped in loop :
If FindLoop.Value = "" Then
ListBox1.Clear
Exit Do
End If

End If
Set FindNextx = FindLoop
Loop Until FindNextx.Address = FindX.Address

'To handle if loop is get the same value as begining :
If ListBox1.ListCount 0 Then
ListBox1.RemoveItem ListBox1.ListCount - 1
ListBox1.ListIndex = 0
End If

End If
Err:
End Sub

just change FindTxt = TextBox1.Text
to FindTxt = ComboBox1.Text

and note ListBox1 is avalilabe

--

Regards,

Halim


"mikeg710" wrote:

Using Excel 2003.

I have a database worksheet named Projects, with three columns of
information: Id, Name, and Location. The worksheet currently has over 200
records.

My VBA form (frmMainMenu) has a ComboBox (cboSelectLocation), CommandButton
(cmdListProjects), and a ListBox (lstProjectsByLocation).

Using the VBA code example from http://j-walk.com/ss/excel/tips/tip47.htm, I
am able to populate the ComboBox with a list of unique, nonduplicated
Locations from the Projects worksheet.

What I am getting stuck on is when the user selects the Location from the
ComboBox, clicking the CommandButton should populate the ListBox with all the
records that match the Location (a filtered list by Location, if you will).

Can anyone help with the VBA code that would sort through the records of the
worksheet to select only the ones that match the Location?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Populate ListBox with Filtered Records

Thank you much!

"Halim" wrote:

Hi,

I've created this one:
Sub FindTxt()
Dim FindX As Object
Dim FindTxt As String
Dim FindLoop As Object
Dim FindNextx As Object
Dim FindLoopAddr As Object
Dim Teks As String, R As Long
ListBox1.ColumnCount = 2
ListBox1.Clear
FindTxt = TextBox1.Text
'On Error GoTo Err

Set FindLoopAddr = Cells
Set FindX = FindLoopAddr.Find(FindTxt, LookIn:=xlValues, LookAt:=xlPart,
SearchOrder:=xlByRows)

If Not FindX Is Nothing Then
ListBox1.AddItem FindX.Address
ListBox1.List(0, 1) = FindX.Value
'To handle next search ... until looped to the begining search :
Set FindNextx = FindX
Do
Set FindLoop = FindLoopAddr.FindNext(After:=FindNextx)
If Not FindLoop Is Nothing Then
ListBox1.AddItem FindLoop.Address
R = R + 1
ListBox1.List(R, 1) = FindLoop.Value
If FindLoop.Offset(0, -(FindLoop.Column - 1)).Value < ""
Then _
ListBox1.List(R, 2) = _
FindLoop.Offset(0, -(FindLoop.Column - 1)).Value

'To handle if trapped in loop :
If FindLoop.Value = "" Then
ListBox1.Clear
Exit Do
End If

End If
Set FindNextx = FindLoop
Loop Until FindNextx.Address = FindX.Address

'To handle if loop is get the same value as begining :
If ListBox1.ListCount 0 Then
ListBox1.RemoveItem ListBox1.ListCount - 1
ListBox1.ListIndex = 0
End If

End If
Err:
End Sub

just change FindTxt = TextBox1.Text
to FindTxt = ComboBox1.Text

and note ListBox1 is avalilabe

--

Regards,

Halim


"mikeg710" wrote:

Using Excel 2003.

I have a database worksheet named Projects, with three columns of
information: Id, Name, and Location. The worksheet currently has over 200
records.

My VBA form (frmMainMenu) has a ComboBox (cboSelectLocation), CommandButton
(cmdListProjects), and a ListBox (lstProjectsByLocation).

Using the VBA code example from http://j-walk.com/ss/excel/tips/tip47.htm, I
am able to populate the ComboBox with a list of unique, nonduplicated
Locations from the Projects worksheet.

What I am getting stuck on is when the user selects the Location from the
ComboBox, clicking the CommandButton should populate the ListBox with all the
records that match the Location (a filtered list by Location, if you will).

Can anyone help with the VBA code that would sort through the records of the
worksheet to select only the ones that match the Location?

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
Delete the filtered records VCKW Excel Discussion (Misc queries) 4 October 15th 07 03:39 PM
Populate a Multicolumn ComboBox with filtered range [email protected] Excel Programming 3 February 16th 07 01:52 AM
returning value from filtered records JulieD Excel Discussion (Misc queries) 3 April 29th 05 03:39 PM
how do I see more than 1000 records filtered ml Excel Discussion (Misc queries) 1 April 13th 05 08:47 PM
Populate Combo Box With Filtered List Kinne Excel Programming 2 August 11th 03 12:23 PM


All times are GMT +1. The time now is 11:25 PM.

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

About Us

"It's about Microsoft Excel"