MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2004, 03:01 PM   #1
TiggerToo
 
Join Date: Feb 2002
Location: Macon, Georgia USA
Posts: 116
Default hide and unhide worksheet

hello all,

I need a little more instructions on why this VBA code does not work the way it suppose too:
I put the correct cell in place $K$23, now I want to hide a worksheet called "DutyCode" when info!$k$23 is blank or has "xx" or "XX" in the cell, and then unhide "DutyCode" when the number 27 is inputed in info!$k$23

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$k$23" Then Exit Sub
If Target.Value = 1234 Then
Worksheets("Sheet2").Visible = True
Else
Worksheets("Sheet2").Visible = False
End If
End Sub

any and all help is gladly tried and tested
Thanks
Walt
TiggerToo is offline   Reply With Quote
Old Mar 30th, 2004, 03:33 PM   #2
Davers
 
Davers's Avatar
 
Join Date: Sep 2002
Location: Michigan
Posts: 1,132
Default Re: hide and unhide worksheet

The only thing I can see is the first part is missing...try this:

Code:
Private Sub Worksheet_Calculate()
   Worksheet_Change Range("K23")
End Sub

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Target.Value = 27 Then
        Worksheets("Sheet2").Visible = True
    Else
        Worksheets("Sheet2").Visible = False
    End If

End Sub
__________________
Using Windows XP with Office XP
Davers is offline   Reply With Quote
Old Mar 30th, 2004, 04:50 PM   #3
Tom Urtis
MrExcel MVP
 
Tom Urtis's Avatar
 
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 9,090
Default Re: hide and unhide worksheet

Why would you frame the question to affect a sheet named "Duty Code" when your procedure shows "Sheet2", and why would you specify the number 27 in your question, but in your code specify 1234? Are you sure you are testing the right criteria?

You can get this to work at least two different ways. One way is to refer to the VBA object name of the worksheet whose tab is named "Sheet2" (or "Duty Code" or whatever sheet you are interested in hiding and unhiding). The worksheet object names are in the VBAProject window of the Visual Basic Editor (Alt+Q > Ctrl+R). My guess is, if you named a worksheet "Sheet2", the code name is "Sheet2", but you'd have to look to make sure, and of course with "Duty Code" you'd really need to look.

Anyway, the code would look like this *instead* of what you have, so replace what you have in your worksheet module with this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$K$23" Then Exit Sub
Run "SheetCommand"
If Target.Value = 1234 Then
Sheet2.Visible = False '.VWorksheets("Sheet2").Visible = False
Else
Sheet2.Visible = True 'Worksheets("Sheet2").Visible = True
End If
End Sub


Your other option is to call a macro that does the dirty deed when cell K23 is changed, instead of relying on the code to do that in the worksheet module when another worksheet is involved.

In the worksheet module:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$K$23" Then Exit Sub
Run "SheetCommand"
End Sub

In a standard module (assuming the target worksheet is Sheet1):

Sub SheetCommand()
If Worksheets("Sheet1").Range("K23").Value = 1234 Then
Worksheets("Sheet2").Visible = False
Else
Worksheets("Sheet2").Visible = True
End If
End Sub


Remember, to get to the worksheet module, right click on your sheet tab, left click on View Code, and paste the following procedure into the large white area that is the worksheet module. Press Alt+Q to return to the worksheet.


Both the above approaches worked for me just now when tested.
__________________
Tom Urtis
Tom Urtis is offline   Reply With Quote
Old Mar 30th, 2004, 06:42 PM   #4
Davers
 
Davers's Avatar
 
Join Date: Sep 2002
Location: Michigan
Posts: 1,132
Default Re: hide and unhide worksheet

Hmmm...why didn't anyone tell me my code was wrong?! I forgot the most important part to it, and included lines that didn't need to be there...sorry about that...that's what happens when I'm too quick to post!!!

Have a good day,

Dave..

P.S. Have revised my code to the following:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = Range("$K$23").Address Then
    If Target.Value = 27 Then
        Worksheets("Sheet2").Visible = True
    Else
        Worksheets("Sheet2").Visible = False
    End If
End If

End Sub
And this seems to work fine...
__________________
Using Windows XP with Office XP
Davers is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 09:35 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.