Need to move to the next visible row in VBA

iswarya

New Member
Joined
Apr 20, 2010
Messages
18
Hi,
I'm trying to accomplish this in VBA. I have a sheet where many rows are hidden. Through VBA, I need to iterate through the rows that are visible only. Is there a way to do this?



Cross-posted at excelforum.com
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello

What are you doing within the iterations?

Example using SpecialCells(xlCellTypeVisible):
Code:
[COLOR="Blue"]Public[/COLOR] [COLOR="Blue"]Sub[/COLOR] demo()
    [COLOR="Blue"]Dim[/COLOR] rngRow [COLOR="Blue"]As[/COLOR] Range
    [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] rngRow [COLOR="Blue"]In[/COLOR] Selection.SpecialCells(12).Rows
        [COLOR="Blue"]Debug.Print[/COLOR] rngRow.Address
    [COLOR="Blue"]Next[/COLOR] rngRow
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Hi

Try this

Code:
Option Explicit
Option Compare Text

Sub VisibleLines()
  Dim iRow As Long, iRows As Long
  Dim Sh As Worksheet
  Set Sh = ActiveSheet
  iRows = Sh.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  For iRow = 1 To iRows
    If Not Sh.Rows(iRow).Hidden = True Then
       MsgBox iRow & " Not Hidden"
    Else
      MsgBox iRow & " Hidden"
    End If
    
  Next iRow

End Sub

Regards

Mike
 
Upvote 0
Thanks that was helpful, but am not sure it solves my problem. What I'm looking for is something like this:

Say I have row43 and the next visible row is row62 and after that row71. Within the VBA code I need to be able to compare something in row43 and row62, and if that condition is true, I need to hide row43. In the next iteration I would compare row62 and row71, and repeat the same thing.

Hope I made myself clear.
 
Upvote 0
Hi

Sorry but I'm struggling a little to conjure up an example without the specifics. But this is what I came up with and hopefully you can understand it and adapt it to work for your requirement:

Code:
[COLOR="Blue"]Public[/COLOR] [COLOR="Blue"]Sub[/COLOR] demo()
    [COLOR="Blue"]Dim[/COLOR] rngRow [COLOR="Blue"]As[/COLOR] Range, rngPreviousRow [COLOR="Blue"]As[/COLOR] Range
    [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] rngRow [COLOR="Blue"]In[/COLOR] Selection.SpecialCells(12).Rows
        [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] rngPreviousRow [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR]
            [COLOR="Green"]'! compare the first cell in the row with the first cell in the previous visible row[/COLOR]
            [COLOR="Green"]'! if they match then hide the row[/COLOR]
            rngRow.Hidden = (rngRow.Resize(, 1).Value = rngPreviousRow.Resize(, 1).Value)
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
        [COLOR="Blue"]Set[/COLOR] rngPreviousRow = rngRow
    [COLOR="Blue"]Next[/COLOR] rngRow
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Just for comparison

heres mine

Code:
Sub VisibleLines()
  Dim iRow As Long, iRows As Long
  Dim Sh As Worksheet
  Dim vValue1 As Variant, iValue1Row As Long
  Dim vValue2 As Variant, iValue2Row As Long
  Dim bFirstRow As Boolean
  Set Sh = ActiveSheet
  bFirstRow = True
  iRows = Sh.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  For iRow = 1 To iRows
    If Not Sh.Rows(iRow).Hidden = True Then
     If Not bFirstRow Then
         vValue2 = Sh.Cells(iRow, 1).Value ' Add Value Two in this case column 1
        iValue2Row = iRow ' Add row of second value
        If vValue1 <> vValue2 Then ' Change for your test here
          Sh.Rows.Address(ivalue1row).Hidden = True ' hide the first row
          vValue1 = vValue2 ' Swap Values
          iValue1Row = iValue2Row ' Swap Values
        End If
     Else
        vValue1 = Sh.Cells(iRow, 1).Value ' Add Value one in this case column 1
        iValue1Row = iRow ' Add row of first value
     End If
    Else
      MsgBox iRow & " Hidden"
    End If
    
  Next iRow

End Sub
I havent tested it as the boss is in and i need to start looking busy, Looks can be deceptive :ROFLMAO:

Regards

Mike

Edit

Just tried compiling and it failed on the Row.Address will need fixing
 
Upvote 0
I know this thread is old, but it's the first link I get from Google, so I'll post what works for me.

Code:
Call MyMacro()
ActiveCell.Offset(1, 0).Activate
Do Until Selection.EntireRow.Hidden = False
If Seletion.EntireRow.Hidden = True Then
ActiveCell.Offset(1, 0).Activate
End If
Loop
 
Upvote 0
Super old thread I know but still one of the first things that comes up in google.

I've had problems with loops taking too long so I thought I would try to come up with something that doesn't use loops. This assumes that your data is already filtered and there are less than 500,000 lines of data in your sheet.

Code:
Sub VisibleData()Dim ColNum, EndCol, BegCol, BegRow As Integer
Dim EndRow As Long

ColNum = 2
EndRow = Cells(500000, ColNum).End(xlUp).Row

EndCol = 8
BegCol = 1
BegRow = 7

Range(Cells(BegRow, BegCol), Cells(EndRow, EndCol)).SpecialCells(xlCellTypeVisible).Select

End Sub


BegCol/Beg Row are where you want to start your selection from and EndCol/EndRow where they end. This macro also assumes that your dynamic vertically only.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,883
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top