Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Filtering data to a userform listbox

Post: Autofilter and Listbox how to acomplish?
-----------
I used the listbox from the control toolbox toolbar (ActiveX controls) and
put it on sheet1. Then I used this code behind the worksheet.
Option Explicit
Private Sub Worksheet_Activate()
Dim wks As Worksheet
Dim rng As Range
Dim rngF As Range
Dim myCell As Range
Dim iCtr As Long
Set wks = Worksheets("sheet2")
Set rng = wks.AutoFilter.Range
With rng
Set rngF = .Resize(.Rows.Count - 1, 1).Offset(1, 0) _
..Cells.SpecialCells(xlCellTypeVisible)
End With
With Me.ListBox1
..Clear
..ColumnCount = rng.Columns.Count
For Each myCell In rngF.Cells
.AddItem (myCell.Value)
For iCtr = 1 To rng.Columns.Count - 1
.List(.ListCount - 1, iCtr) = myCell.Offset(0, iCtr).Value
Next iCtr
Next myCell
End With
End Sub
If you filter the data on sheet2, then go back (and activate sheet1), the
listbox gets updated.
-------
What I would like to know is how to manipulate this onto a UserForm and to
use all the data on a worksheet called "Masters", Column Labels are A1:D1,
data range is A2:D200.
I have a combobox that has a list of names on it and I would like to do a
autofilter type setup based on the name picked out of the combobox to appear
in the listbox for just that person.
I tried to follow this code ( i am somewhat new at this ) and I could not
figure out how to make it fit into a userform situation.

I tried to do a :
Private Sub Combobox1_Change()
If Combobox1.value = "Jeremy" then
Range("a1").select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=Combobox1.value
-- after here I have tried to do Listbox1.list = range("A2:D200"), i have
tried Listbox1.rowsource("A2:D200"), etc.. everything always kept coming
back with some sort of error or would list all the cells
with in the listbox.

Does anyone have any ideas for me? I would appreciate the help.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Filtering data to a userform listbox

Hi Kryer,

Try this code below all you need is press Alt-F11 create a userform, 2
command buttons, 2 listboxes at the top of the form then paste this code
below.

Private Sub CommandButton1_Click()
If Lb1.ListIndex = 0 Then
Lb2.AddItem Lb1.Text
Lb1.RemoveItem Lb1.ListIndex
End If
End Sub

Private Sub CommandButton2_Click()
Do While Lb1.ListCount 0
Lb2.AddItem Lb1.List(0)
Lb1.RemoveItem (0)
Loop
End Sub

Private Sub UserForm_Activate()
UserForm1.Caption = "Use of Listbox"
CommandButton1.Caption = "Transfer to Listbox 2"
CommandButton2.Caption = "Transfer All"
With Lb1
..AddItem Cells(1, 1).Value
..AddItem Cells(2, 1).Value
..AddItem Cells(3, 1).Value
..AddItem "Darryl"
..AddItem "Dom"
..AddItem "Donna"
..AddItem "Debra"
..AddItem "Dan"
..AddItem "Dieter"
End With
End Sub

Note: Try to put some values for Cells(1,1).Value, Cells(2,1).Value etc.
Hope this helps.

"Kryer" wrote:

Post: Autofilter and Listbox how to acomplish?
-----------
I used the listbox from the control toolbox toolbar (ActiveX controls) and
put it on sheet1. Then I used this code behind the worksheet.
Option Explicit
Private Sub Worksheet_Activate()
Dim wks As Worksheet
Dim rng As Range
Dim rngF As Range
Dim myCell As Range
Dim iCtr As Long
Set wks = Worksheets("sheet2")
Set rng = wks.AutoFilter.Range
With rng
Set rngF = .Resize(.Rows.Count - 1, 1).Offset(1, 0) _
.Cells.SpecialCells(xlCellTypeVisible)
End With
With Me.ListBox1
.Clear
.ColumnCount = rng.Columns.Count
For Each myCell In rngF.Cells
.AddItem (myCell.Value)
For iCtr = 1 To rng.Columns.Count - 1
.List(.ListCount - 1, iCtr) = myCell.Offset(0, iCtr).Value
Next iCtr
Next myCell
End With
End Sub
If you filter the data on sheet2, then go back (and activate sheet1), the
listbox gets updated.
-------
What I would like to know is how to manipulate this onto a UserForm and to
use all the data on a worksheet called "Masters", Column Labels are A1:D1,
data range is A2:D200.
I have a combobox that has a list of names on it and I would like to do a
autofilter type setup based on the name picked out of the combobox to appear
in the listbox for just that person.
I tried to follow this code ( i am somewhat new at this ) and I could not
figure out how to make it fit into a userform situation.

I tried to do a :
Private Sub Combobox1_Change()
If Combobox1.value = "Jeremy" then
Range("a1").select
Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=Combobox1.value
-- after here I have tried to do Listbox1.list = range("A2:D200"), i have
tried Listbox1.rowsource("A2:D200"), etc.. everything always kept coming
back with some sort of error or would list all the cells
with in the listbox.

Does anyone have any ideas for me? I would appreciate the help.

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
Filtering a Listbox from a cell value (Re-posted) NoodNutt Excel Worksheet Functions 0 February 26th 08 09:06 AM
filtering to show items in a listbox eileenj Excel Discussion (Misc queries) 1 August 11th 06 01:30 PM
Userform: listbox and controls' data entry validation sebastienm Excel Programming 2 September 15th 05 02:02 PM
UserForm Listbox in VBC Marcia3641 Excel Discussion (Misc queries) 7 July 22nd 05 10:20 AM
UserForm ListBox Otto Moehrbach[_6_] Excel Programming 3 December 30th 03 06:22 PM


All times are GMT +1. The time now is 10:51 AM.

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"