Hide/unhide

I have a button that when the user presses the button the program will search
an ordered column for values equal to 0 and hide the rows that has values
equal to zero. If the rows already are hidden then they shall be revealed by
pressing the button. My code is:

Sub showHideButton_Klicka()
Dim relativCell As Range
Dim i As Integer, j As Integer, k As Integer
Dim blnFirstFound As Boolean, blnLastFound As Boolean, blnIsHidden As Boolean

Set relativCell = Worksheets("Berakning").Cells.Find("Rel.", LookIn:=xlValues)
'hittar forsta och sista cell med varde 0
Do Until IsEmpty(relativCell.Offset(i, 0)) = True Or blnLastFound = True
If blnFirstFound = False Then
If relativCell.Offset(i, 0) = 0 Then
blnFirstFound = True
k = i
Else: End If
End If

If blnFirstFound = True And blnLastFound = False Then
j = i
If relativCell.Offset(j + 1, 0) <> 0 Then
blnLastFound = True
Else: End If
Else: End If

i = i + 1 'langst ner i listan
Loop
'slut hittar forsta och sista cell med varde 0


'markerar rader med cell varde 0
startRow = relativCell.Offset(k, 0).Row
endRow = relativCell.Offset(j, 0).Row
Rows("" & startRow & ":" & endRow & "").Select

'har ska man gomma
If blnIsHidden = False Then
Selection.EntireRow.Hidden = True 'gommer rader
blnIsHidden = True
End If
'slut gomma

'start ta fram
If blnIsHidden = True Then
Selection.EntireRow.Hidden = False 'gommer rader
blnIsHidden = False
End If
'slut ta fram

End Sub

this does not really work since blnIsHidden is false when the sub starts. And then when i hide it becomes true so it automatically hides and then unhides. what i want to do is to hide if not hidden and unhide if already hidden..I just cant figure out how to do it. i thought about using two buttons but how do you keep the selection of the rows If you know what I mean please help me out! Thank you all very much!!!



Answer this question

Hide/unhide

  • TigerPhoenix

    The problem has now fallen into a logic issue. I suggest you create a Finite State Machine design pattern in the code. That code will specify the exact states of the visibility of buttons in  the unique states of the machine. That way the logic of visibility can directed from state to state and not from the state of indivudual buttons to determine other buttons visibility.

    This is psuedo code

    int state = 1 ; // 1 is the initial state where one button is on

    switch (state)

    case 1:  // Initial state user has not made a choice
       button 1 visible;
       button 2 invisible;
    break;

    case 2 // User made a choice...processing begins
       button 1 visible
       button 2 visible
    case 3 // Processing ends display data
        button 1 invisible
        button 2 visible
    case 4 // Allow user to edit. Go back to state 1 when user is done.
       button 1 invisible
       button 2 invisible
    break;

    ------------

    Now your code will specify a state and call the state machine to set the buttons visibility accordingly.

  • jbap01

    Yes sorry i have not had time to reply. It help very much, I changed some things to get it to work for my application but I really had a lot of use of your code! Thank you very much! Also every one else that answered my question thank you very much! ou save my life on daily basis!!!
  • jeusdi

    The following code select cell A1 in this case and works down the column until a blank cell is found.

    If the cell contents are zero then it will hide the row if it is not hidden and unhide the row if it is already hidden.

    Hope this is what you were looking for.

    Write to me at charleschand@aol.com if you need further help.

    [code]

    Private Sub CommandButton1_Click()
    Dim thisValue As Integer
    Dim strValue As String
    Range("A1").Select
    thisValue = Selection.Value
    strValue = Selection.Value
    Do While Len(strValue) > 0
    If thisValue = 0 Then
    ' invert the hidden property
    Selection.EntireRow.Hidden = Not Selection.EntireRow.Hidden

    End If
    ActiveCell.Offset(1, 0).Select
    thisValue = Selection.Value
    strValue = Selection.Value
    Loop
    End Sub

    [End Code]


  • AndyC12345

    Try my original code again I saw one error.

    Dim thisValue as Double is good because you are processing double numbers

    But your Dim strValue as Double is incorrect. This should be String. This is why the program is not stopping when you reach a blank cell.

    I have tried it again with the following values in a column and it works

    0,000120000014
    0
    0,000120000016
    0
    0,000120000018
    0
    0,000120000020
    0
    0,000120000022
    0

    Write again if you have problems.

    ChasAA


  • yousaid

    AndersBank wrote:

    doubles (a typical value is 0,000120000014). Is that a problem



    yes it may be. Double looses its precision around 15 decimal places. If you needing exact numbers there will be a problem. I do financial calculations and never use double, I use decimal in C#. See this post for information on problems that we have run into on C#. I am not sure if decimal type is a part of VBA or if the double type of VBA behaves like C#....


  • QWERTYtech

    Did the code I gave you earlier help

    ChasAA


  • mvalpreda

    Thank you very much! That was really nice code! However I am having some trouble implementing it in my program. I do some more stuff but bascially I get your code to work but with some problems. You see I have to search for the column to start looking in. I do that in "Set relativCell ...." and it works fine. And then I try to run your code with some changes. My numbers are all doubles (a typical value is 0,000120000014). Is that a problem I also need to stop the process. Now the loop continues and I have to shut down the program.. I tried doing that with the first "Do Until...." cause I thought that I would find the either the last number in the column or stop when I have found the last zero. I check for the last zero at the bottom with the last if-..... I cannot figure out what is wrong, it just keeps running till infinity every time. Please help me out if you can. Thanks alot!

    Sub showHideButton_Klicka()
    Dim blnLastFound As Boolean
    Dim thisValue As Double
    Dim strValue As Double
    Set relativCell = Worksheets("Berakning").Cells.Find("Rel.", LookIn:=xlValues)
    Range(relativCell.Offset(1, 0).Address).Select
    thisValue = Selection.Value
    strValue = Selection.Value
    Do Until IsEmpty(relativCell) = True Or blnLastFound = True
    Do While Len(strValue) <> 0
    If thisValue = 0 Then
    ' invert the hidden property
    Selection.EntireRow.Hidden = Not Selection.EntireRow.Hidden

    End If
    ActiveCell.Offset(1, 0).Select
    thisValue = Selection.Value
    strValue = Selection.Value

    If ActiveCell.Offset(2, 0).Value <> 0 Then
    blnLastFound = True
    Else: End If
    Loop
    Loop
    End Sub


  • Edward__

    Hi AndersBank, mark the post(s) that helped you as the answer(s), so when others search the forums, they might be more inclined to look at a successful post than a non successful one... Or post why it failed for you. Thanks.


  • Hide/unhide