Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 22nd, 2008, 11:22 PM
Registered User
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default ListBox Load Every Visible Cell

After I filter the data in Column A, I need to load ListBox1 with all of the visible cells in Column B until there is an empty cell in Column B. Is this even possible?

This is what I have been trying so far, and the problems I am having are that it is still reading the hidden cells and it is also deleting the row that is active and ListBox1 does not fill with anything even though the MsgBox has an output.


Sub ListBoxLoad()

ActiveCell.Offset(1, 0).Select
Do
ActiveCell.EntireRow.Hidden = True
ActiveCell.Offset(1, 0).Select
MsgBox ActiveCell
UserForm1.ListBox1.Value = ActiveCell
Loop Until ActiveCell = ""

End Sub

 
Old November 26th, 2008, 01:43 PM
Authorized User
 
Join Date: Feb 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's 1 possible approach. I created a simple Userform and added a list box. The following code goes in the form's module:

Code:
Private Sub GetFilteredList(ByRef CriteriaRange As Range, ByRef ListRange As Range)

    Dim r As Integer

    If CriteriaRange.Rows.Count <> ListRange.Rows.Count Then
        Debug.Print "The criteria and list ranges must be of equal length."
        Exit Sub
    End If

    For r = 1 To CriteriaRange.Rows.Count
        If CriteriaRange.Cells(r, 1).Value >= 10 Then
            ListBox1.AddItem ListRange.Cells(r, 1).Value
        End If
    Next

End Sub

Private Sub UserForm_Initialize()
    With Worksheets("Sheet1")
        GetFilteredList .Range("A1:A18"), .Range("B1:B18")
    End With
End Sub
The sub FilteredList accepts two range parameters, representing a criteria column and the resultant list column. After checking that the two range parameters passed are of equal height, it them loops through each row in the criteria column. Within the loop, I have added a simple condition, whereby if the current cell in the criteria column is >= 10, add the corresponding list column's cell value to the ListBox control.

The sub is called within the form's Initialize event, passing in a couple of ranges from Sheet1.

This is just one of many ways you could do this.

HTH

 
Old February 22nd, 2016, 05:42 AM
Registered User
 
Join Date: Feb 2016
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need Help

Quote:
Originally Posted by Keith Smith View Post
Here's 1 possible approach. I created a simple Userform and added a list box. The following code goes in the form's module:

Code:
Private Sub GetFilteredList(ByRef CriteriaRange As Range, ByRef ListRange As Range)

    Dim r As Integer

    If CriteriaRange.Rows.Count <> ListRange.Rows.Count Then
        Debug.Print "The criteria and list ranges must be of equal length."
        Exit Sub
    End If

    For r = 1 To CriteriaRange.Rows.Count
        If CriteriaRange.Cells(r, 1).Value >= 10 Then
            ListBox1.AddItem ListRange.Cells(r, 1).Value
        End If
    Next

End Sub

Private Sub UserForm_Initialize()
    With Worksheets("Sheet1")
        GetFilteredList .Range("A1:A18"), .Range("B1:B18")
    End With
End Sub
The sub FilteredList accepts two range parameters, representing a criteria column and the resultant list column. After checking that the two range parameters passed are of equal height, it them loops through each row in the criteria column. Within the loop, I have added a simple condition, whereby if the current cell in the criteria column is >= 10, add the corresponding list column's cell value to the ListBox control.

The sub is called within the form's Initialize event, passing in a couple of ranges from Sheet1.

This is just one of many ways you could do this.

HTH
Thanks for code its realy helpful for me
i am neweb on this forum i need help
case is same

but i want to display 4 column A B C D in my list box while in the above case only one column result show in listbox

waiting for your reply

regard


Zafar Janjua





Similar Threads
Thread Thread Starter Forum Replies Last Post
Separating a CSV cell into 1 value per cell bigtonyicu Excel VBA 2 March 14th, 2008 12:28 PM
I need to refer a cell within a cell like =RC[ RC2 chakravarthi_os Excel VBA 1 September 24th, 2006 08:19 AM
if the cell content is a part of another cell cont sriramus Excel VBA 1 November 15th, 2005 10:20 AM
visible vs. not visible on form akibaMaila VB.NET 2002/2003 Basics 2 August 15th, 2005 02:40 PM
Lose cell Text when editing cell in VSFlexGrid 6 bobcratchet VB How-To 0 July 30th, 2004 09:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.