• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

Excel 2003: Simple VBA hide row on cell value

I have been trying for 2 hours to get a row to be hidden based on the content of a cell.  I hve tried a dozen of the solutions on the forum to no avail.  

What am I missing?

this is my code

Sub HideandUnhide()
If Cells(D, 71) = "*Requires 2 signatures*" Then
    Rows(75).EntireRow.Hidden = False
Else
    Rows(75).EntireRow.Hidden = True
End If
End Sub
0
YDagenais
Asked:
YDagenais
  • 6
  • 5
  • 4
1 Solution
 
telyni19Commented:
Two things:
1. The Cells function requires two numbers as inputs, so I think you'd want Cells(4,71) rather than using the letter D.
2. You're unhiding row 75 if the text is as indicated. Did you intend to hide it instead? If so, swap False and True.
0
 
Chris BottomleyCommented:
Also the worksheet change event handler is triggered when anything changes so could be used to trigger your activity whenever a change occurs to the worksheet.

It can be simpplified and tidied up depending on if the functionality applies to more than one row.

Chris
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("D" & 71) = "*Requires 2 signatures*" Then
        Rows(75).EntireRow.Hidden = False
    Else
        Rows(75).EntireRow.Hidden = True
    End If

End Sub

Open in new window

0
 
YDagenaisAuthor Commented:
I have tried both suggestions and the row 75 remains visible .  As additional information, the D71 cell is the result of a formula:=IF(data!C57="Yes","*Requires 2 signatures*"," ").  

Sub HideandUnhide()
If Cells(4, 71) = "*Requires 2 signatures*" Then
    Rows(75).EntireRow.Hidden = True
Else
    Rows(75).EntireRow.Hidden = False
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("D" & 71) = "*Requires 2 signatures*" Then
        Rows(75).EntireRow.Hidden = False
    Else
        Rows(75).EntireRow.Hidden = True
    End If

End Sub

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Chris BottomleyCommented:
It should work fine in the change event handler.

Did you place it in the specific worksheets code page?

Chris
0
 
telyni19Commented:
Sorry, about that, the Cells function is switched relative to the Range function. If you're using Cells, you'd want Cells(71, 4) for cell D71 - or Range("D71").Value.

And the Worksheet_Change example didn't switch the False and True values. So you'd want either of these:

Sub HideandUnhide()
If Cells(71, 4) = "*Requires 2 signatures*" Then
    Rows(75).EntireRow.Hidden = True
Else
    Rows(75).EntireRow.Hidden = False
End If
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("D" & 71) = "*Requires 2 signatures*" Then
        Rows(75).EntireRow.Hidden = True
    Else
        Rows(75).EntireRow.Hidden = False
    End If
End Sub
0
 
Chris BottomleyCommented:
telyni19:

Swapping true and false is easy enough but is not necessarily appropriate as we do not know if it is to be hidden until the condition is met or hidden if the condition is met.

The key comment was "row 75 remains visible" which implies the status does not change and therefore the function isn't working at all.

Chris
0
 
telyni19Commented:
Chris:
I'll let the author of the question tell me whether the cell should be hidden if the condition is met or not. (You're right, that hasn't actually been specified, which would be useful information.) However, aside from that, your function looks as if it should work, so the first thing I would think to try, given the information that it does not hide the row when it should, is to switch the values so that it should hide the row when the condition is met rather than the reverse. If that also does not do what the author intended, then there is a more subtle problem, and we really need to clarify what the function should be doing to avoid confusion.
0
 
YDagenaisAuthor Commented:
Ok I tried several versions.  D71 is =IF(data!C57="Yes","*Requires 2 signatures*"," ")
So if D71 = " " then it should hide row 75, else do not hide.
The code is in the appropriate worksheet change

Private Sub Worksheet_Change(ByVal Target As Range)
    If Cells(71, 4) = " " Then
        Rows(75).EntireRow.Hidden = True
    Else
        Rows(75).EntireRow.Hidden = False
    End If
End Sub
0
 
telyni19Commented:
Is the code running at all? Can you put a breakpoint on the If statement and make sure the code is getting that far? Then check what it thinks the value of Cells(71,4) is. Spaces are hard to track, so you could try "" in the IF function in D71 instead and then use this line for the If statement instead:
If Len(Cells(71, 4)) = 0
0
 
YDagenaisAuthor Commented:
I changed D71  to show "" and changed the code to If Len(Cells(71, 4)) = 0

I put breaks on all the lines and now the code works but only if the code is ran manually.  

Is it possible that the code runs every time D71 is changed?
0
 
telyni19Commented:
Yes, the code will run every time any cell on the worksheet is changed. The Worksheet_Change Sub is automatically triggered. If you don't want that and simply want to trigger it manually or on a button click or something, then you'd want to use a regular named Sub, as you had originally.
0
 
YDagenaisAuthor Commented:
It does not work unless the code is ran manually.  D71 cell is changed and the row does not unhide.

I have tried these variations.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Len(Cells(71, 4)) = 0 Then
        Rows(75).EntireRow.Hidden = True
    Else
        Rows(75).EntireRow.Hidden = False
    End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Len(Range("D" & 71) = 0 Then
        Rows(75).EntireRow.Hidden = True
    Else
        Rows(75).EntireRow.Hidden = False
    End If

End Sub
0
 
Chris BottomleyCommented:
>>>I put breaks on all the lines and now the code works but only if the code is ran manually.  
It wouldn't do so if defined as the change handler since the parameter Target  wouldn't allow it to.

Can you upload a sample workbook ... delete all the data to make it secure.

Chris
0
 
YDagenaisAuthor Commented:
Thank you Chris but our internal policy and firewalls prevents us from posting or exporting files.

I will tell the user to hide the row manyally, we have spent enough time on this.

Thank you .
0
 
YDagenaisAuthor Commented:
The solution should work and the code is accurate however i am unable to make it work due to internal policies.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now