Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'm trying to work with a range that has had an
autofilter applied to it in VBA. For example: Dim SimplifiedMethods As Range Set SimplifiedMethods = Range("SimplifiedMethods") where the range "SimplifiedMethods" has been filtered on a couple of criteria and only has about 1/10th of it's rows showing in the worksheet. Is there any way to have only the rows that are visible assigned to the Range object? Thanks in advance. Mark |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Mark,
Dim rng As Range Set rng = ActiveSheet.AutoFilter.Range. _ SpecialCells(xlCellTypeVisible) --- Regards, Norman "MarkJ" wrote in message ... I'm trying to work with a range that has had an autofilter applied to it in VBA. For example: Dim SimplifiedMethods As Range Set SimplifiedMethods = Range("SimplifiedMethods") where the range "SimplifiedMethods" has been filtered on a couple of criteria and only has about 1/10th of it's rows showing in the worksheet. Is there any way to have only the rows that are visible assigned to the Range object? Thanks in advance. Mark |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Norman,
Thanks for your help. I'm still having some problems. I tried this: Set FilteredRange = Worksheets _ ("tblInt9").AutoFilter.Range.SpecialCells _ (xlCellTypeVisible) Value = FilteredRange(1, 1) Value = FilteredRange(1, 2) Value = FilteredRange(1, 3) Value = FilteredRange(1, 4) Value = FilteredRange(2, 1) Value = FilteredRange(2, 2) Value = FilteredRange(2, 3) Value = FilteredRange(2, 4) The values for the first row are correct, but the second row of the range reverts back to the unfiltered values. The first four values are from row 5, which is what is shown after the filter, but the second for values are from row 1, which is not shown after the filter. Also, FilteredRange.Rows.Count results in 1, when there are a couple hundred rows shown after the filter. Any ideas? Thanks again. Mark -----Original Message----- Hi Mark, Dim rng As Range Set rng = ActiveSheet.AutoFilter.Range. _ SpecialCells (xlCellTypeVisible) --- Regards, Norman "MarkJ" wrote in message ... I'm trying to work with a range that has had an autofilter applied to it in VBA. For example: Dim SimplifiedMethods As Range Set SimplifiedMethods = Range("SimplifiedMethods") where the range "SimplifiedMethods" has been filtered on a couple of criteria and only has about 1/10th of it's rows showing in the worksheet. Is there any way to have only the rows that are visible assigned to the Range object? Thanks in advance. Mark . |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Mark,
It is not immeditely apparent what you are trying to do. If you are trying to reassure yourself that the correct range is being addressed, then a simple Msgbox FilteredRange .Address may suffice. Your attempt to reference individual cells of the autofiltered range will not work - as you have discovered. The reason for this is that , for example, FilteredRange(3,3) refers to a cell two rows down and two rows to the right of the first cell in the autofilter range and this cell may (or may not!) be in the autofilter range. Also FilteredRange(1,1) refers to the first header cell. Again as you have dicovered, you should not count the number of rows in the autofiltered range with the statement: FilteredRange.Rows.Count This is because typically the autofilter range is compised of a nummber of discontiguous areas and , in such cases, the Rows.Count will return thr number of rows in the first area of the autofiltered range. If you need the row count of the range, you would need to build it with a counter. The following exemplifies a method for returning the autofilter range address and row count via messagebox alerts and returns the individual filtered cell values (row by row to the immediate window. Sub Tester2() Dim FilteredRange As Range, rw As Range Dim iCtr As Long, j As Long Set FilteredRange = Worksheets("tblInt9").AutoFilter.Range. _ SpecialCells(xlCellTypeVisible) j = FilteredRange.Columns.Column For Each rw In FilteredRange.Rows If rw.Row FilteredRange.Rows.Row Then iCtr = iCtr + 1 Debug.Print Cells(rw.Row, j).Value _ & " " & Cells(rw.Row, j + 1) _ & " " & Cells(rw.Row, j + 2) _ & " " & Cells(rw.Row, j + 3) End If Next MsgBox FilteredRange.Address MsgBox "The number of autofiltered rows is " & iCtr End Sub --- Regards, Norman wrote in message ... Norman, Thanks for your help. I'm still having some problems. I tried this: Set FilteredRange = Worksheets _ ("tblInt9").AutoFilter.Range.SpecialCells _ (xlCellTypeVisible) Value = FilteredRange(1, 1) Value = FilteredRange(1, 2) Value = FilteredRange(1, 3) Value = FilteredRange(1, 4) Value = FilteredRange(2, 1) Value = FilteredRange(2, 2) Value = FilteredRange(2, 3) Value = FilteredRange(2, 4) The values for the first row are correct, but the second row of the range reverts back to the unfiltered values. The first four values are from row 5, which is what is shown after the filter, but the second for values are from row 1, which is not shown after the filter. Also, FilteredRange.Rows.Count results in 1, when there are a couple hundred rows shown after the filter. Any ideas? Thanks again. Mark -----Original Message----- Hi Mark, Dim rng As Range Set rng = ActiveSheet.AutoFilter.Range. _ SpecialCells (xlCellTypeVisible) --- Regards, Norman "MarkJ" wrote in message ... I'm trying to work with a range that has had an autofilter applied to it in VBA. For example: Dim SimplifiedMethods As Range Set SimplifiedMethods = Range("SimplifiedMethods") where the range "SimplifiedMethods" has been filtered on a couple of criteria and only has about 1/10th of it's rows showing in the worksheet. Is there any way to have only the rows that are visible assigned to the Range object? Thanks in advance. Mark . |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Norman,
That cleared it up. I am able to do exactly what I wanted now. Thanks for your help. Mark -----Original Message----- Hi Mark, It is not immeditely apparent what you are trying to do. If you are trying to reassure yourself that the correct range is being addressed, then a simple Msgbox FilteredRange .Address may suffice. Your attempt to reference individual cells of the autofiltered range will not work - as you have discovered. The reason for this is that , for example, FilteredRange(3,3) refers to a cell two rows down and two rows to the right of the first cell in the autofilter range and this cell may (or may not!) be in the autofilter range. Also FilteredRange(1,1) refers to the first header cell. Again as you have dicovered, you should not count the number of rows in the autofiltered range with the statement: FilteredRange.Rows.Count This is because typically the autofilter range is compised of a nummber of discontiguous areas and , in such cases, the Rows.Count will return thr number of rows in the first area of the autofiltered range. If you need the row count of the range, you would need to build it with a counter. The following exemplifies a method for returning the autofilter range address and row count via messagebox alerts and returns the individual filtered cell values (row by row to the immediate window. Sub Tester2() Dim FilteredRange As Range, rw As Range Dim iCtr As Long, j As Long Set FilteredRange = Worksheets ("tblInt9").AutoFilter.Range. _ SpecialCells(xlCellTypeVisible) j = FilteredRange.Columns.Column For Each rw In FilteredRange.Rows If rw.Row FilteredRange.Rows.Row Then iCtr = iCtr + 1 Debug.Print Cells(rw.Row, j).Value _ & " " & Cells(rw.Row, j + 1) _ & " " & Cells(rw.Row, j + 2) _ & " " & Cells(rw.Row, j + 3) End If Next MsgBox FilteredRange.Address MsgBox "The number of autofiltered rows is " & iCtr End Sub --- Regards, Norman wrote in message ... Norman, Thanks for your help. I'm still having some problems. I tried this: Set FilteredRange = Worksheets _ ("tblInt9").AutoFilter.Range.SpecialCells _ (xlCellTypeVisible) Value = FilteredRange(1, 1) Value = FilteredRange(1, 2) Value = FilteredRange(1, 3) Value = FilteredRange(1, 4) Value = FilteredRange(2, 1) Value = FilteredRange(2, 2) Value = FilteredRange(2, 3) Value = FilteredRange(2, 4) The values for the first row are correct, but the second row of the range reverts back to the unfiltered values. The first four values are from row 5, which is what is shown after the filter, but the second for values are from row 1, which is not shown after the filter. Also, FilteredRange.Rows.Count results in 1, when there are a couple hundred rows shown after the filter. Any ideas? Thanks again. Mark -----Original Message----- Hi Mark, Dim rng As Range Set rng = ActiveSheet.AutoFilter.Range. _ SpecialCells (xlCellTypeVisible) --- Regards, Norman "MarkJ" wrote in message ... I'm trying to work with a range that has had an autofilter applied to it in VBA. For example: Dim SimplifiedMethods As Range Set SimplifiedMethods = Range("SimplifiedMethods") where the range "SimplifiedMethods" has been filtered on a couple of criteria and only has about 1/10th of it's rows showing in the worksheet. Is there any way to have only the rows that are visible assigned to the Range object? Thanks in advance. Mark . . |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
you don't need to iterate the entire visible range to get the row count
Dim rng as Range, cell as Range, FilterRange as Range Dim i as Long set rng = Worksheets ("tblInt9").AutoFilter.Range ' get the header row out of the range set rng = rng.offset(1,0).Resize(rng.rows.count-1,1) on error resume next set FilterRange = rng.specialcells(xlVisible) On error goto 0 if FilterRange is nothing then msgbox "No matches" Else ' this gives you the visible cells in the first column of the filtered range. You shouldn't need anything else as you can offset from that for any other value. msgbox FilterRange.count i = 0 for each cell in rng i = i + 1 msgbox cell & " - " & cell(i,1) & " - " & cell(i,2) & " - " & cell(i,3) Next End if will give you the number of visible rows -- Regards, Tom Ogilvy |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
using countif in a filtered range | Excel Worksheet Functions | |||
Sum a filtered range | Excel Discussion (Misc queries) | |||
copy only filtered range. | Excel Discussion (Misc queries) | |||
How to copy a filtered range ? | Excel Discussion (Misc queries) | |||
Counting within a filtered range | Excel Worksheet Functions |