+ Reply to Thread
Results 1 to 4 of 4

Code to hide rows is no longer working

  1. #1
    Registered User
    Join Date
    01-26-2015
    Location
    United States
    MS-Off Ver
    2010 & 2013
    Posts
    24

    Code to hide rows is no longer working

    Hello,

    I added a code last year to hide certain rows if there was no data in them. In one section, the code had to be altered a little because if there wasn't information in any of a range of columns then hide the row. I am trying to make changes to the spreadsheet and when I do, the code no longer works. Can someone help me please, I had this code entered in several workbooks that I have to edit now. the following is the code causing a runtime error: the error that is highlighted will always be the two lines following the "i = 45-70"

    Sub Expense_Hide()
    Dim RowCnt As Long
    Dim Ws As Worksheet
    Dim i, LastRow

    Application.ScreenUpdating = False
    For Each Ws In Worksheets
    Ws.Rows.Hidden = False: Ws.Activate
    For i = 45 To 71
    If Cells(i, "A").EntireRow.Hidden = True Then
    Cells.EntireRow.Hidden = False
    Target.Offset(1).Select
    Exit Sub
    End If
    Next

    For i = 45 To 70
    If Application.Sum(Range("A" & i & ":" & "I" & i)) = 0 _
    And Application.CountA(Range("A" & i & ":" & "I" & i)) = 0 Then
    Cells(i, "A").EntireRow.Hidden = True
    Else
    Cells(i, "A").EntireRow.Hidden = False
    End If
    Next
    Next Ws

    For Each Ws In Worksheets
    'Exclude specific sheets
    If Ws.Name <> "ACTUAL VARIANCES" And Ws.Name <> "% VARIANCES" And Ws.Name <> "TOTALS" And Ws.Name <> "Chart Total Difference" And Ws.Name <> "OVERTIME" Then
    With Ws
    For RowCnt = 6 To 41
    If .Cells(RowCnt, 3).Value = 0 Then
    .Rows(RowCnt).Hidden = True
    End If
    Next RowCnt

    For RowCnt = 78 To 111
    If .Cells(RowCnt, 3).Value = 0 Then
    .Rows(RowCnt).Hidden = True
    End If
    Next RowCnt
    End With
    End If
    Next Ws

    Application.ScreenUpdating = True

    End Sub

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Code to hide rows is no longer working

    Well for starters you need to declare the worksheet when referencing a range. (I.E. Cells(i, "A").EntireRow.Hidden references the active sheet not the worksheet that you are looping through. You would need to add the variable for your worksheet loop to it. ws.Cells(i, "A").EntireRow.Hidden)

    Try that first and report back if it is still having issues then we can look at your code further. It is also always better to submit an example workbook

    Note: Use code tags around your code please.
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Registered User
    Join Date
    01-26-2015
    Location
    United States
    MS-Off Ver
    2010 & 2013
    Posts
    24

    Re: Code to hide rows is no longer working

    Unfortunately, that did not help. I set up several different macros and then saved them in one to run simutanously because my worksheets have different set ups with several different items going on. This same code works until I alter one of the worksheets.

    I am trying to attached the file but it will not allow me to. I think it would be easier if you could see all the parts of it.

    Sub Totals()
    Dim RowCnt As Long
    Dim Ws As Worksheet
    Dim i, LastRow
    Application.ScreenUpdating = False
    For Each Ws In Worksheets
    'Exclude specific sheets
    If Ws.Name = "TOTALS" Then
    With Ws
    For RowCnt = 6 To 53
    If .Cells(RowCnt, 3).Value = 0 Then
    .Rows(RowCnt).Hidden = True
    End If
    Next RowCnt
    For RowCnt = 61 To 108
    If .Cells(RowCnt, 3).Value = 0 Then
    .Rows(RowCnt).Hidden = True
    End If
    Next RowCnt
    End With
    End If
    Next Ws
    Application.ScreenUpdating = True

    End Sub

    Sub Actual_Variances()
    Dim RowCnt As Long
    Dim Ws As Worksheet
    Dim i, LastRow
    Application.ScreenUpdating = False
    For Each Ws In Worksheets
    'Exclude specific sheets
    If Ws.Name = "ACTUAL VARIANCES" Then
    With Ws
    For RowCnt = 6 To 53
    If .Cells(RowCnt, 3).Value = 0 Then
    .Rows(RowCnt).Hidden = True
    End If
    Next RowCnt
    End With
    End If
    Next Ws
    Application.ScreenUpdating = True

    End Sub

    Sub Per_Vairances()
    Dim RowCnt As Long
    Dim Ws As Worksheet
    Dim i, LastRow
    Application.ScreenUpdating = False
    For Each Ws In Worksheets
    'Exclude specific sheets
    If Ws.Name = "PER VARIANCES" Then
    With Ws
    For RowCnt = 6 To 53
    If .Cells(RowCnt, 3).Value = "" Then
    .Rows(RowCnt).Hidden = True
    End If
    Next RowCnt
    End With
    End If
    Next Ws
    Application.ScreenUpdating = True

    End Sub

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Code to hide rows is no longer working

    Once again, use code tags around your code. It is a forum rule and makes your code much easier to read.

    I think it would be easier if you could see all the parts of it.
    Nope. While messy, there is nothing actually wrong with the syntax of your code.

    I have cleaned it up a bit and added a couple of comments. It is on you to figure out what is going on if you can't submit the sheet.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Show/Hide objects no longer working in Excel 2013
    By kaaskiwi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-25-2015, 07:07 PM
  2. PasteSpecial in VBA Code No Longer Working
    By kmsandrbs in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-06-2014, 01:27 PM
  3. VB Code no longer working
    By buttercup1227 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2014, 10:38 AM
  4. Delete vs Hide rows code not working
    By cxc300cxc in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-22-2013, 03:41 PM
  5. [SOLVED] Code stopped working, can no longer copy and save a worksheet
    By Oracle in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-30-2012, 07:56 PM
  6. VBA Code no longer working
    By jdbel in forum Excel General
    Replies: 16
    Last Post: 11-16-2011, 07:33 PM
  7. mail Code no longer working in 2007
    By lonnied in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-10-2007, 06:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1