+ Reply to Thread
Results 1 to 5 of 5

VBA IF statement to hide worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    01-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    23

    VBA IF statement to hide worksheet

    Hello,
    I have Excel 2010. I am new to VBA. I tried the "record a macro" function, but was unable to get it to do what I want. I am trying to write a macro to hide a worksheet if C$3="" in that worksheet. There are about 23 different sheets, and I am having trouble coding it. I would greatly appreciate any insight you may have.

  2. #2
    Forum Contributor
    Join Date
    06-16-2011
    Location
    London
    MS-Off Ver
    Excel XP
    Posts
    276

    Re: VBA IF statement to hide worksheet

    Open the vba editor and find the sheet(s) you want this to happen on in the top left window. Right click it and select 'view code' and paste this in:

    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Range("C3").Value = "" Then activesheet.Visible = False
    
    
    End Sub

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA IF statement to hide worksheet

    Hello Schwartz,

    The basic VBA statement would be. There are other methods but to know which one is best for your needs would require more information.
        If $C$3 = "" Then 
           ActiveSheet.Visible = False
        Else
           ActiveSheet.Visible = True
        End If
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    01-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: VBA IF statement to hide worksheet

    Thank you for the quick replies. Let me clarify exactly what I need. I apologize I did not specify earlier. The macro needs to Look at C3 of each sheet. If there is no value in C3, it needs to hide that sheet. If there is a value, It needs the tab to be renamed to the value in C3. I have the code to change the tab name, but I do not know how to incorporate the hiding function.
    Private Sub Workbook_Open()
        Dim wSheet As Worksheet
        On Error Resume Next
        For Each wSheet In Me.Worksheets
            If wSheet.Range("C3") = "" Then
                wSheet.Name = "Sheet" & wSheet.Index
            Else
                wSheet.Name = Format(wSheet.Range("C3"), "mmm dd, yyyy")
            End If
        Next wSheet
        On Error GoTo 0
    End Sub
    Last edited by Schwartz; 01-23-2012 at 05:04 PM.

  5. #5
    Registered User
    Join Date
    01-19-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    23

    Re: VBA IF statement to hide worksheet

    Thank you for the quick replies. Let me clarify exactly what I need. I apologize I did not specify earlier. The macro needs to Look at C3 of each sheet. If there is no value in C3, it needs to hide that sheet. If there is a value, It needs the tab to be renamed to the value in C3. I have the code to change the tab name, but I do not know how to incorporate the hiding function.
    Private Sub Workbook_Open()
        Dim wSheet As Worksheet
        On Error Resume Next
        For Each wSheet In Me.Worksheets
            If wSheet.Range("C3") = "" Then
                wSheet.Name = "Sheet" & wSheet.Index
            Else
                wSheet.Name = Format(wSheet.Range("C3"), "mmm dd, yyyy")
            End If
        Next wSheet
        On Error GoTo 0
    End Sub
    Last edited by Schwartz; 01-23-2012 at 05:09 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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