Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
This macro is supposed to increment down to the next visible cell in a
filtered range, then to the first empty cell below the filtered range. It's based off code Tom Ogilvy gave me, but I must have changed something and I can't find it. I call this macro within the loop of another macro. The problem is that this doesn't stop when it hits the end of the filtered range; instead, it loops back up to Row 1. When stepping through, when the ActiveCell is in the last visible row of the filtered range, on the third "Set rng =" line, "rng.count" is one more than the row number of the ActiveCell. But when I step down to the next line ("On Error Resume Next"), I moused over rng.count and found it was 1. (I don't understand what this "count" is counting - rows? cells? something else?) What I expected was the ActiveCell to become the first empty cell below the filtered range. The calling macro then would detect the empty cell and end its loop. What happens, though, is this macro selects back up to the top left cell ("rng1(1).Select"), and never goes below the filtered range, creating an endless loop. How do I fix this to drop out of the filtered range? Ed Sub Increment1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then rng1(1).Select End If End Sub |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
Sub AAIncrement1()
Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then If rng1(1).Row = ActiveCell.Row Then ActiveCell.Offset(1, 0).Select Else rng1(1).Select End If Else rng(rng.Count).Offset(1, 0).Select End If End Sub Might be what you want. -- Regards, Tom Ogilvy "Ed" wrote in message ... This macro is supposed to increment down to the next visible cell in a filtered range, then to the first empty cell below the filtered range. It's based off code Tom Ogilvy gave me, but I must have changed something and I can't find it. I call this macro within the loop of another macro. The problem is that this doesn't stop when it hits the end of the filtered range; instead, it loops back up to Row 1. When stepping through, when the ActiveCell is in the last visible row of the filtered range, on the third "Set rng =" line, "rng.count" is one more than the row number of the ActiveCell. But when I step down to the next line ("On Error Resume Next"), I moused over rng.count and found it was 1. (I don't understand what this "count" is counting - rows? cells? something else?) What I expected was the ActiveCell to become the first empty cell below the filtered range. The calling macro then would detect the empty cell and end its loop. What happens, though, is this macro selects back up to the top left cell ("rng1(1).Select"), and never goes below the filtered range, creating an endless loop. How do I fix this to drop out of the filtered range? Ed Sub Increment1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then rng1(1).Select End If End Sub |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
Tom - it works well, except when my filtered range is at the end of my used
range. I have slightly over 13,500 rows of data, with no blanks. I was having trouble when I first started using the AutoFilter with filtering down to the end of my used range, and someone told me to put something in a cell in the row just below my used range; my data ends in 13,592, and A13593 has "Last Row" in it. When I filter for the block of data that goes down to 13592, this macro iterates through all the visible cells (the outer macro starts the selection in B1, then calls the AAIncrement1 to drop down into B2 and start the evaluations). When it reaches the bottom, it jumps back up to A1 of the header row (that being rng1(1).Select), and runs down to the bottom again. Now it stops after the second time through, rather than looping continuously, but it still has to run twice. With any other filtered range that does not include that last data row, it runs once to the bottom of the filtered data and stops. Just to see, I commented out "On Error GoTo 0" and inserted If Err.Number < 0 Then MsgBox Err.Number End If Every time it hit the bottom of the visible range, I got a "1004" error. Would it make sense to If Err.Number = 1004 Then Exit Sub? Ed "Tom Ogilvy" wrote in message ... Sub AAIncrement1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then If rng1(1).Row = ActiveCell.Row Then ActiveCell.Offset(1, 0).Select Else rng1(1).Select End If Else rng(rng.Count).Offset(1, 0).Select End If End Sub Might be what you want. -- Regards, Tom Ogilvy "Ed" wrote in message ... This macro is supposed to increment down to the next visible cell in a filtered range, then to the first empty cell below the filtered range. It's based off code Tom Ogilvy gave me, but I must have changed something and I can't find it. I call this macro within the loop of another macro. The problem is that this doesn't stop when it hits the end of the filtered range; instead, it loops back up to Row 1. When stepping through, when the ActiveCell is in the last visible row of the filtered range, on the third "Set rng =" line, "rng.count" is one more than the row number of the ActiveCell. But when I step down to the next line ("On Error Resume Next"), I moused over rng.count and found it was 1. (I don't understand what this "count" is counting - rows? cells? something else?) What I expected was the ActiveCell to become the first empty cell below the filtered range. The calling macro then would detect the empty cell and end its loop. What happens, though, is this macro selects back up to the top left cell ("rng1(1).Select"), and never goes below the filtered range, creating an endless loop. How do I fix this to drop out of the filtered range? Ed Sub Increment1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then rng1(1).Select End If End Sub |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
Try this one:
Sub AAIncrement1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) On Error Resume Next Set rng1 = Intersect(rng, rng.SpecialCells(xlVisible)) On Error GoTo 0 If Not rng1 Is Nothing Then If rng1(1).Row = ActiveCell.Row Then ActiveCell.Offset(1, 0).Select Else rng1(1).Select End If Else rng(rng.Count).Offset(1, 0).Select End If End Sub -- Regards, Tom Ogilvy "Ed" wrote in message ... Tom - it works well, except when my filtered range is at the end of my used range. I have slightly over 13,500 rows of data, with no blanks. I was having trouble when I first started using the AutoFilter with filtering down to the end of my used range, and someone told me to put something in a cell in the row just below my used range; my data ends in 13,592, and A13593 has "Last Row" in it. When I filter for the block of data that goes down to 13592, this macro iterates through all the visible cells (the outer macro starts the selection in B1, then calls the AAIncrement1 to drop down into B2 and start the evaluations). When it reaches the bottom, it jumps back up to A1 of the header row (that being rng1(1).Select), and runs down to the bottom again. Now it stops after the second time through, rather than looping continuously, but it still has to run twice. With any other filtered range that does not include that last data row, it runs once to the bottom of the filtered data and stops. Just to see, I commented out "On Error GoTo 0" and inserted If Err.Number < 0 Then MsgBox Err.Number End If Every time it hit the bottom of the visible range, I got a "1004" error. Would it make sense to If Err.Number = 1004 Then Exit Sub? Ed "Tom Ogilvy" wrote in message ... Sub AAIncrement1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then If rng1(1).Row = ActiveCell.Row Then ActiveCell.Offset(1, 0).Select Else rng1(1).Select End If Else rng(rng.Count).Offset(1, 0).Select End If End Sub Might be what you want. -- Regards, Tom Ogilvy "Ed" wrote in message ... This macro is supposed to increment down to the next visible cell in a filtered range, then to the first empty cell below the filtered range. It's based off code Tom Ogilvy gave me, but I must have changed something and I can't find it. I call this macro within the loop of another macro. The problem is that this doesn't stop when it hits the end of the filtered range; instead, it loops back up to Row 1. When stepping through, when the ActiveCell is in the last visible row of the filtered range, on the third "Set rng =" line, "rng.count" is one more than the row number of the ActiveCell. But when I step down to the next line ("On Error Resume Next"), I moused over rng.count and found it was 1. (I don't understand what this "count" is counting - rows? cells? something else?) What I expected was the ActiveCell to become the first empty cell below the filtered range. The calling macro then would detect the empty cell and end its loop. What happens, though, is this macro selects back up to the top left cell ("rng1(1).Select"), and never goes below the filtered range, creating an endless loop. How do I fix this to drop out of the filtered range? Ed Sub Increment1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then rng1(1).Select End If End Sub |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
Tom - Sorry for the long hang time in a reply, but I do this through work
and your solution came after I had left. I just had a chance to test out your solution, and it works great! Thank you. If I may ask, when you do Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) what cell is identified by the rng(rng.Count) expression? I tried rng(rng.Count).Select to see if I could figure out what it does, but it didn't work. Another head-scratcher - I set up this same workbook on a network, but it is opened and search through a Visual Basic 6.0 interface. The VB app sets a reference to the Excel object, and searches using the same code that gave me problems - yet that way it works fine. Go figure! Thanks for all your help, Tom. Ed "Tom Ogilvy" wrote in message ... Try this one: Sub AAIncrement1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) On Error Resume Next Set rng1 = Intersect(rng, rng.SpecialCells(xlVisible)) On Error GoTo 0 If Not rng1 Is Nothing Then If rng1(1).Row = ActiveCell.Row Then ActiveCell.Offset(1, 0).Select Else rng1(1).Select End If Else rng(rng.Count).Offset(1, 0).Select End If End Sub -- Regards, Tom Ogilvy "Ed" wrote in message ... Tom - it works well, except when my filtered range is at the end of my used range. I have slightly over 13,500 rows of data, with no blanks. I was having trouble when I first started using the AutoFilter with filtering down to the end of my used range, and someone told me to put something in a cell in the row just below my used range; my data ends in 13,592, and A13593 has "Last Row" in it. When I filter for the block of data that goes down to 13592, this macro iterates through all the visible cells (the outer macro starts the selection in B1, then calls the AAIncrement1 to drop down into B2 and start the evaluations). When it reaches the bottom, it jumps back up to A1 of the header row (that being rng1(1).Select), and runs down to the bottom again. Now it stops after the second time through, rather than looping continuously, but it still has to run twice. With any other filtered range that does not include that last data row, it runs once to the bottom of the filtered data and stops. Just to see, I commented out "On Error GoTo 0" and inserted If Err.Number < 0 Then MsgBox Err.Number End If Every time it hit the bottom of the visible range, I got a "1004" error. Would it make sense to If Err.Number = 1004 Then Exit Sub? Ed "Tom Ogilvy" wrote in message ... Sub AAIncrement1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then If rng1(1).Row = ActiveCell.Row Then ActiveCell.Offset(1, 0).Select Else rng1(1).Select End If Else rng(rng.Count).Offset(1, 0).Select End If End Sub Might be what you want. -- Regards, Tom Ogilvy "Ed" wrote in message ... This macro is supposed to increment down to the next visible cell in a filtered range, then to the first empty cell below the filtered range. It's based off code Tom Ogilvy gave me, but I must have changed something and I can't find it. I call this macro within the loop of another macro. The problem is that this doesn't stop when it hits the end of the filtered range; instead, it loops back up to Row 1. When stepping through, when the ActiveCell is in the last visible row of the filtered range, on the third "Set rng =" line, "rng.count" is one more than the row number of the ActiveCell. But when I step down to the next line ("On Error Resume Next"), I moused over rng.count and found it was 1. (I don't understand what this "count" is counting - rows? cells? something else?) What I expected was the ActiveCell to become the first empty cell below the filtered range. The calling macro then would detect the empty cell and end its loop. What happens, though, is this macro selects back up to the top left cell ("rng1(1).Select"), and never goes below the filtered range, creating an endless loop. How do I fix this to drop out of the filtered range? Ed Sub Increment1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then rng1(1).Select End If End Sub |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
try this
Sub TestRange() Dim rng As Range Dim i As Long Set rng = Range("A1:D3") For i = 1 To rng.Count rng(i).Select MsgBox i & ", " & rng(i).Address & ", " & rng(i).Value Next i rng(1).Select MsgBox "click OK to see rng(rng.count)" rng(rng.Count).Select MsgBox "rng.count: " & vbNewLine & _ rng.Count & ", " & rng(i).Address & ", " & rng(i).Value End Sub -- Regards, Tom Ogilvy "Ed" wrote in message ... Tom - Sorry for the long hang time in a reply, but I do this through work and your solution came after I had left. I just had a chance to test out your solution, and it works great! Thank you. If I may ask, when you do Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) what cell is identified by the rng(rng.Count) expression? I tried rng(rng.Count).Select to see if I could figure out what it does, but it didn't work. Another head-scratcher - I set up this same workbook on a network, but it is opened and search through a Visual Basic 6.0 interface. The VB app sets a reference to the Excel object, and searches using the same code that gave me problems - yet that way it works fine. Go figure! Thanks for all your help, Tom. Ed "Tom Ogilvy" wrote in message ... Try this one: Sub AAIncrement1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) On Error Resume Next Set rng1 = Intersect(rng, rng.SpecialCells(xlVisible)) On Error GoTo 0 If Not rng1 Is Nothing Then If rng1(1).Row = ActiveCell.Row Then ActiveCell.Offset(1, 0).Select Else rng1(1).Select End If Else rng(rng.Count).Offset(1, 0).Select End If End Sub -- Regards, Tom Ogilvy "Ed" wrote in message ... Tom - it works well, except when my filtered range is at the end of my used range. I have slightly over 13,500 rows of data, with no blanks. I was having trouble when I first started using the AutoFilter with filtering down to the end of my used range, and someone told me to put something in a cell in the row just below my used range; my data ends in 13,592, and A13593 has "Last Row" in it. When I filter for the block of data that goes down to 13592, this macro iterates through all the visible cells (the outer macro starts the selection in B1, then calls the AAIncrement1 to drop down into B2 and start the evaluations). When it reaches the bottom, it jumps back up to A1 of the header row (that being rng1(1).Select), and runs down to the bottom again. Now it stops after the second time through, rather than looping continuously, but it still has to run twice. With any other filtered range that does not include that last data row, it runs once to the bottom of the filtered data and stops. Just to see, I commented out "On Error GoTo 0" and inserted If Err.Number < 0 Then MsgBox Err.Number End If Every time it hit the bottom of the visible range, I got a "1004" error. Would it make sense to If Err.Number = 1004 Then Exit Sub? Ed "Tom Ogilvy" wrote in message ... Sub AAIncrement1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.Count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then If rng1(1).Row = ActiveCell.Row Then ActiveCell.Offset(1, 0).Select Else rng1(1).Select End If Else rng(rng.Count).Offset(1, 0).Select End If End Sub Might be what you want. -- Regards, Tom Ogilvy "Ed" wrote in message ... This macro is supposed to increment down to the next visible cell in a filtered range, then to the first empty cell below the filtered range. It's based off code Tom Ogilvy gave me, but I must have changed something and I can't find it. I call this macro within the loop of another macro. The problem is that this doesn't stop when it hits the end of the filtered range; instead, it loops back up to Row 1. When stepping through, when the ActiveCell is in the last visible row of the filtered range, on the third "Set rng =" line, "rng.count" is one more than the row number of the ActiveCell. But when I step down to the next line ("On Error Resume Next"), I moused over rng.count and found it was 1. (I don't understand what this "count" is counting - rows? cells? something else?) What I expected was the ActiveCell to become the first empty cell below the filtered range. The calling macro then would detect the empty cell and end its loop. What happens, though, is this macro selects back up to the top left cell ("rng1(1).Select"), and never goes below the filtered range, creating an endless loop. How do I fix this to drop out of the filtered range? Ed Sub Increment1() Dim rng As Range, rng1 As Range Dim icol As Long icol = ActiveCell.Column Set rng = ActiveSheet.AutoFilter.Range Set rng = Intersect(rng, Columns(icol)) Set rng = Range(ActiveCell.Offset(1, 0), rng(rng.count)) On Error Resume Next Set rng1 = rng.SpecialCells(xlVisible) On Error GoTo 0 If Not rng1 Is Nothing Then rng1(1).Select End If End Sub |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
When I tried your TestRange macro, Tom, it set the range to the 12 cells
that were included in A1:D3. But when I hit MsgBox "click OK to see rng(rng.count)" D3 was selected, but i had incremented to 13, as shown by & rng(i).Address & ", " & rng(i).Value in the last MsgBox. I added & i & ", " to confirm this. Is this as it should be? Ed "Tom Ogilvy" wrote in message ... try this Sub TestRange() Dim rng As Range Dim i As Long Set rng = Range("A1:D3") For i = 1 To rng.Count rng(i).Select MsgBox i & ", " & rng(i).Address & ", " & rng(i).Value Next i rng(1).Select MsgBox "click OK to see rng(rng.count)" rng(rng.Count).Select MsgBox "rng.count: " & vbNewLine & _ rng.Count & ", " & rng(i).Address & ", " & rng(i).Value End Sub -- Regards, Tom Ogilvy |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
a loop counter is incremented so the i being 13 is to be expected.
it is similar to Sub Tester1() i = 1 Do Debug.Print i 'interior of the loop i = i + 1 Loop Until i 12 Debug.Print " outside loop: " & i End Sub rng(rng.count) refers to the last cell in the range for a single area range. -- Regards, Tom Ogilvy "Ed" wrote in message ... When I tried your TestRange macro, Tom, it set the range to the 12 cells that were included in A1:D3. But when I hit MsgBox "click OK to see rng(rng.count)" D3 was selected, but i had incremented to 13, as shown by & rng(i).Address & ", " & rng(i).Value in the last MsgBox. I added & i & ", " to confirm this. Is this as it should be? Ed "Tom Ogilvy" wrote in message ... try this Sub TestRange() Dim rng As Range Dim i As Long Set rng = Range("A1:D3") For i = 1 To rng.Count rng(i).Select MsgBox i & ", " & rng(i).Address & ", " & rng(i).Value Next i rng(1).Select MsgBox "click OK to see rng(rng.count)" rng(rng.Count).Select MsgBox "rng.count: " & vbNewLine & _ rng.Count & ", " & rng(i).Address & ", " & rng(i).Value End Sub -- Regards, Tom Ogilvy |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
Increment cells only in filtered range?
Thanks again, Tom. I really appreciate the time and explanations.
Ed "Tom Ogilvy" wrote in message ... a loop counter is incremented so the i being 13 is to be expected. it is similar to Sub Tester1() i = 1 Do Debug.Print i 'interior of the loop i = i + 1 Loop Until i 12 Debug.Print " outside loop: " & i End Sub rng(rng.count) refers to the last cell in the range for a single area range. -- Regards, Tom Ogilvy "Ed" wrote in message ... When I tried your TestRange macro, Tom, it set the range to the 12 cells that were included in A1:D3. But when I hit MsgBox "click OK to see rng(rng.count)" D3 was selected, but i had incremented to 13, as shown by & rng(i).Address & ", " & rng(i).Value in the last MsgBox. I added & i & ", " to confirm this. Is this as it should be? Ed "Tom Ogilvy" wrote in message ... try this Sub TestRange() Dim rng As Range Dim i As Long Set rng = Range("A1:D3") For i = 1 To rng.Count rng(i).Select MsgBox i & ", " & rng(i).Address & ", " & rng(i).Value Next i rng(1).Select MsgBox "click OK to see rng(rng.count)" rng(rng.Count).Select MsgBox "rng.count: " & vbNewLine & _ rng.Count & ", " & rng(i).Address & ", " & rng(i).Value End Sub -- Regards, Tom Ogilvy |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
calculate values in an unlimited range of cells in a column WITHOUTalso calculating values that had previously been filtered AGAINST? | Excel Discussion (Misc queries) | |||
Count only cells within Filtered Range | Excel Worksheet Functions | |||
sum of visable cells in filtered range | Excel Worksheet Functions | |||
average of visible cells in a filtered range | Excel Worksheet Functions | |||
Cells.Find in Auto-Filtered range | Excel Programming |