PC Review


Reply
Thread Tools Rate Thread

Dim an array from a filtered Range

 
 
CBartman
Guest
Posts: n/a
 
      16th Oct 2009
Would ultimately like to use array for userform combobox source.
Excel 2003
I seem to be having trouble with "non-consecutive" rows.
"Banging" my head. Any help would be appreciated. Thanks

Sub RangeFilter()

Dim w As Worksheet
Dim filterArray()
Dim currentFiltRange As String

Set w = Workbooks("My-Stuff.xls").Worksheets("My-Machines")
With w.ListObjects(1)

'filter the list (by column 5 data) based on userform textbox.text
currentFiltRange = .Range.AutoFilter(5, tbAcctNum.Text)
'sheet data is filtered correctly

MsgBox
w.ListObjects(1).DataBodyRange.SpecialCells(xlCellTypeVisible).Address
'Example result (based on selection)- $A$2:$P$4, $A$14:$P$32,
$A$36:$P$38
'visible results of filter. These addresses are correct.

ReDim filterArray(0 To f)
'An attempt to include only visible rows in an array

For f = 0 To .DataBodyRange.SpecialCells(xlCellTypeVisible).Rows.Count
'code to include ALL visible rows.
Next f

MsgBox f
'showing me number of visible rows for the above example as: 10
'incorrectly reporting number of visible rows (actual number of
rows: 13)
End With
End Sub
 
Reply With Quote
 
 
 
 
Bernie Deitrick
Guest
Posts: n/a
 
      16th Oct 2009
C,

If you sort your table and then filter it, you can use the filtered values from the range directly
as the rowsource of the combobox. I have assumed that you have a header row on your list.

Private Sub UserForm_Initialize()
Dim myR As Range
Dim myStr As String

Set myR = Workbooks("My-Stuff.xls").Worksheets("My-Machines").ListObjects(1).Range
myR.Sort Key1:=myR.Cells(1, 5), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
myR.AutoFilter Field:=5, Criteria1:=tbAcctNum.Text

Set myR = myR.Offset(1, 0).Resize(myR.rows.Count - 1) _
.SpecialCells(xlCellTypeVisible)
myStr = myR.Parent.Name & "!" & myR.Address
Me.ComboBox1.RowSource = myStr
myR.AutoFilter
End Sub



HTH,
Bernie
MS Excel MVP


"CBartman" <(E-Mail Removed)> wrote in message
news:8EEDEAFF-68AA-48D5-8265-(E-Mail Removed)...
> Would ultimately like to use array for userform combobox source.
> Excel 2003
> I seem to be having trouble with "non-consecutive" rows.
> "Banging" my head. Any help would be appreciated. Thanks
>
> Sub RangeFilter()
>
> Dim w As Worksheet
> Dim currentFiltRange As String
>
> Set w = Workbooks("My-Stuff.xls").Worksheets("My-Machines")
> With w.ListObjects(1)
>
> 'filter the list (by column 5 data) based on userform textbox.text
> currentFiltRange = .Range.AutoFilter(5, tbAcctNum.Text)
> 'sheet data is filtered correctly
>
> MsgBox
> w.ListObjects(1).DataBodyRange.SpecialCells(xlCellTypeVisible).Address
> 'Example result (based on selection)- $A$2:$P$4, $A$14:$P$32,
> $A$36:$P$38
> 'visible results of filter. These addresses are correct.
>
> ReDim filterArray(0 To f)
> 'An attempt to include only visible rows in an array
>
> For f = 0 To .DataBodyRange.SpecialCells(xlCellTypeVisible).Rows.Count
> 'code to include ALL visible rows.
> Next f
>
> MsgBox f
> 'showing me number of visible rows for the above example as: 10
> 'incorrectly reporting number of visible rows (actual number of
> rows: 13)
> End With
> End Sub



 
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
Get filtered range into array RB Smissaert Microsoft Excel Programming 20 16th Aug 2010 11:33 PM
Redimming an array dynamically assigned from range (how to redim first dimension of a 2-D array? /or/ reverse the original array order) Keith R Microsoft Excel Programming 3 13th Nov 2007 04:08 PM
creating a filtered range/named range mark kubicki Microsoft Excel Programming 1 4th Nov 2006 03:14 PM
traversing through a filtered range based on another filtered range zestpt Microsoft Excel Programming 4 12th Jul 2004 06:37 PM
Output an access report to excel files with filtered a filtered date range. Frank Microsoft Access Form Coding 2 30th Jul 2003 01:03 AM


Features
 

Advertising
 

Newsgroups
 


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