+ Reply to Thread
Results 1 to 15 of 15

User form hides wrong information

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2010
    Location
    Tennessee, United States
    MS-Off Ver
    Office: Microsoft Office 2007. Home: Microsoft Office 2010
    Posts
    45

    User form hides wrong information

    I am creating a user form to hide certain options. I have coded the "OK" button to hide certain firlds depending on what I have checked. The problem is that when I click the "OK" button, it hides the wrong rows. It also hides rows when I click "OK" without anything being checked.

    The code for the "OK" button is something like:
    Private Sub Ok_Click()
    If chkOption1 = True Then
    Sheets("Sheet2").Select
    Selection.Rows("3:4").Select
    Selection.EntireRow.Hidden = True
    ElseIf chkOption2 = True Then
    Sheets("Sheet2").Select
    Selection.Rows("5:6").Select
    Selection.EntireRow.Hidden = True
    End If
    End Sub

    The rows it hides for the first click of "OK" are always numbers that end in a 3 or 4. What do I need to do to fix this?
    Last edited by EGR2317; 06-01-2010 at 09:21 AM.

  2. #2
    Forum Contributor
    Join Date
    02-25-2010
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    174

    Re: User form hides wrong information

    Use this as a guide on how to select the rows -

    Sub x()
    Sheets(2).Rows(3).Offset(1, 0).Hidden = True
    End Sub
    You can replace Sheets(2) with Sheets("Sheet2")

  3. #3
    Registered User
    Join Date
    05-26-2010
    Location
    Tennessee, United States
    MS-Off Ver
    Office: Microsoft Office 2007. Home: Microsoft Office 2010
    Posts
    45

    Re: User form hides wrong information

    Thanks for the tip, but I am still getting the same problem. It hides the wrong rows and still hides rows even when nothing is selected.

    When I tried to make an example to post, I couldn't get it to work at all. Still working on it so that you can see exactly what I am talking about.

  4. #4
    Forum Contributor
    Join Date
    02-25-2010
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    174

    Re: User form hides wrong information

    Quote Originally Posted by EGR2317 View Post
    Thanks for the tip, but I am still getting the same problem. It hides the wrong rows and still hides rows even when nothing is selected.

    When I tried to make an example to post, I couldn't get it to work at all. Still working on it so that you can see exactly what I am talking about.
    Try something like -

    Private Sub Ok_Click()
    If chkOption1.value = True Then
    Sheets(2).Rows(3).Offset(1, 0).Hidden = True
    ElseIf chkOption2.value = True Then
    Sheets(2).Rows(5).Offset(1, 0).Hidden = True
    End If
    End Sub

  5. #5
    Registered User
    Join Date
    05-26-2010
    Location
    Tennessee, United States
    MS-Off Ver
    Office: Microsoft Office 2007. Home: Microsoft Office 2010
    Posts
    45

    Re: User form hides wrong information

    When I tried the code, nothing would select or hide when chosen. It did, however, fix the problem of hiding rows when nothing was selected, thank you for that.

    Also, will using If/Else statements prohibit me from choosing multiple options to be true? I just thought of this while looking at my code.
    Last edited by EGR2317; 05-28-2010 at 11:56 AM.

  6. #6
    Forum Contributor
    Join Date
    02-25-2010
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    174

    Re: User form hides wrong information

    The VBA is not built to "select" anything. Most of these functions you will want to execute without physically selecting a cell or group of cells. If you want to change the active sheet then you can do so, but there was no need to select rows 3 and 4 by highlighting them.

    Can you give me an example of multiple options being true?

    You can write

    If chkOption1.value = True AND chkOption2.value = True Then
    ...Code...
    Else:
    End if

  7. #7
    Registered User
    Join Date
    05-26-2010
    Location
    Tennessee, United States
    MS-Off Ver
    Office: Microsoft Office 2007. Home: Microsoft Office 2010
    Posts
    45

    Re: User form hides wrong information

    The user form I am making is going to ask the user which options are omitted from the given sheet it will point to. If more than one option is omitted, then all rows corresponding to all omitted options would need to be hidden. Would I need to do something like:

    If chkOption1 = True Then
         Sheets(2).Rows(3).Offset(1, 0).Hidden = True
    ElseIf chkOption2 = True Then
         Sheets(2).Rows(3).Offset(1, 0).Hidden = True
    ElseIf chkOption1 = True And chkOption2 = True Then
         Sheets(2).Rows(3).Offset(3, 0).Hidden = True
    End If
    And when I use the term "select" I mean that the desired rows are not being hidden. I used my original means of selecting because it matched the way VBA hid rows when recording a macro to do the same thing. Now, even when I write it the same way it would be written in a macro, the rows are not being hidden like they are supposed to be.

    Thank you very much for all of the time you are putting into helping me in this matter.

  8. #8
    Forum Contributor
    Join Date
    02-25-2010
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    174

    Re: User form hides wrong information

    Can you attach the worksheet?

    Are your checkboxes on the worksheet or embedded in the form? If they are in the worksheet then the form will not know of their existance.

    EDIT: Also change your if logic, I forgot to tell you that you must put the AND statement first, if you do not the first statement will always execute and skip the 2nd else if.

    If chkOption1 = True And chkOption2 = True Then
         Sheets(2).Rows(3).Offset(3, 0).Hidden = True
    ElseIf chkOption2 = True Then
         Sheets(2).Rows(3).Offset(1, 0).Hidden = True
    ElseIf chkOption1 = True Then
         Sheets(2).Rows(3).Offset(1, 0).Hidden = True
    End If
    Second edit - Add this above your code before checking the IF statement

    Debug.Print chkOption1
    Debug.Print chkOption2

    Click on View and turn on the 'Immediate Window' Once you run the Macro see what those values say.
    Last edited by DP978; 05-28-2010 at 12:57 PM.

  9. #9
    Registered User
    Join Date
    05-26-2010
    Location
    Tennessee, United States
    MS-Off Ver
    Office: Microsoft Office 2007. Home: Microsoft Office 2010
    Posts
    45

    Re: User form hides wrong information

    The checkboxes are embedded in the user form.

    I have attatched an example to the best of my abilities to what I am trying to do.

    Also, I am wanting to link two user forms together so that when the user clicks yes on the first user form it brings up the second. How can I do this?

    The userform I am needing help with currently is InfoChoice

    Thanks again for all your help
    Last edited by EGR2317; 05-28-2010 at 01:07 PM.

  10. #10
    Forum Contributor
    Join Date
    02-25-2010
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    174

    Re: User form hides wrong information

    Ok - try this first -

    Get rid of all the "chk" prefixes in your code. You have them labeled "appearance" not "chkappearance" that should help. (do this for all of them)

  11. #11
    Registered User
    Join Date
    05-26-2010
    Location
    Tennessee, United States
    MS-Off Ver
    Office: Microsoft Office 2007. Home: Microsoft Office 2010
    Posts
    45

    Re: User form hides wrong information

    That worked. Thank you very much. I should have noticed that the names were different.

    One more thing. Is there a way to link my two user forms? When the user exits out of the OmitInfo box by choosing "Yes", I would like for it to take them straight into the other box.

  12. #12
    Registered User
    Join Date
    05-26-2010
    Location
    Tennessee, United States
    MS-Off Ver
    Office: Microsoft Office 2007. Home: Microsoft Office 2010
    Posts
    45

    Re: User form hides wrong information

    Disregard the last post. I referenced the wrong name in the first box which is why it would not link

  13. #13
    Forum Contributor
    Join Date
    02-25-2010
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    174

    Re: User form hides wrong information

    Sure -

    Sub chkYes_Click()
    If chkYes = True Then 
    Private Sub CommandButton1_Click()
     UserForm2.Hide
     UserForm1.Show
    Else: End
    End IF
    End Sub

  14. #14
    Registered User
    Join Date
    05-26-2010
    Location
    Tennessee, United States
    MS-Off Ver
    Office: Microsoft Office 2007. Home: Microsoft Office 2010
    Posts
    45

    Re: User form hides wrong information

    Thanks for all of your help. You have made this task a lot earier on me.

  15. #15
    Forum Contributor
    Join Date
    02-25-2010
    Location
    Boston
    MS-Off Ver
    Excel 2007
    Posts
    174

    Re: User form hides wrong information

    Not a problem, Glad I could help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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