Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA to show or hide columns in one worksheet conditioned on value in other worksheet


I'll preface this by saying I'm a relative newbie to VBA, so bear with
me. I have three worksheets in a workbook, "Contents," "I-1" and "Prov
Rec". I have written one macro in Module 1 to automatically open to the
worksheet "Contents" regardless of which sheet was open when the user
last saved the file.

One cell in worksheet I-1 has a three-choice drop-down box. Whenever
the drop-down selection is changed on the I-1 worksheet, I want the
macro to either hide or unhide certain columns on the Prov Rec
worksheet.

Here's what I've done so far. I've borrowed from various sources I've
come across, but it's not coming together quite right. Any suggestions
as to what I'm missing and whether it can be written more efficiently?

All of these Subs are written in Module 2 one below the next. Let me
know if that's an issue as well.

Thanks in advance!
-------

Sub Run_On_Open()

' Run the macro CheckforChange any time an entry is made in I-1 cell
B17
ThisWorkbook.Worksheets("Contents").OnEntry = "CheckForChange"

End Sub


Sub CheckForChange()
Application.ScreenUpdating = False
Dim WorkbookTypeField As String
' A change in worksheet I-1 cell B17 will trigger the
SetWorkbookType macro.
ThisWorkbook.Worksheets ("I-1")
WorkbookTypeField = Range("B17")

If Not Application.Intersect(ActiveCell, WorkbookTypeField) _
Is Nothing Then SetWorkbookType
Application.ScreenUpdating = True

End Sub

Sub SetWorkbookType()
Application.ScreenUpdating = False
Dim WorkbookTypeField As String
' Checking value in worksheet I-1 cell B17
ThisWorkbook.Worksheets ("I-1")
WorkbookTypeField = Range("B17")

Select Case WorkbookTypeField
' If the value in worksheet I-1 cell B17 is Book-to-Tax...
Case "Book-to-Tax"
' Hide columns C-E on Prov Rec worksheet
Sheets("Prov Rec").Select
Columns("C:E").Select
Selection.EntireColumn.Hidden = True
' If the value in worksheet I-1 cell B17 is
Provision-to-Return...
Case "Provision-to-Return"
' Show columns C-E on Prov Rec worksheet
Sheets("Prov Rec").Select
Columns("C:E").Select
Selection.EntireColumn.Hidden = False
' If the value in worksheet I-1 cell B17 is
Extension-to-Return...
Case "Extension-to-Return"
Sheets("Prov Rec").Select
Columns("C:E").Select
Selection.EntireColumn.Hidden = False

End Select

Application.ScreenUpdating = True

End Sub


--
punsterr
------------------------------------------------------------------------
punsterr's Profile: http://www.excelforum.com/member.php...o&userid=23961
View this thread: http://www.excelforum.com/showthread...hreadid=396718

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA to show or hide columns in one worksheet conditioned on value in other worksheet

I think this is just what you need

Private Sub Workbook_Open()
Worksheets("Contents").Activate
End Sub

'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code

and

Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "B17"

On Error GoTo ws_exit:
Application.EnableEvents = False
With Target
If .Address(False, False) = WS_RANGE Then
Select Case .Value
' If value in worksheet I-1 cell B17 is Book-to-Tax...
Case "Book-to-Tax"
' Hide columns C-E on Prov Rec worksheet
Sheets("Prov Rec").Columns("C:E").Hidden = True
' If the value in worksheet I-1 cell B17 is
' Provision-to-Return...
Case "Provision-to-Return", "Extension-to-Return"
Sheets("Prov Rec").Columns("C:E").Hidden = False
End Select
End If
End With

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet (I-1) code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.




--

HTH

RP
(remove nothere from the email address if mailing direct)


"punsterr" wrote in
message ...

I'll preface this by saying I'm a relative newbie to VBA, so bear with
me. I have three worksheets in a workbook, "Contents," "I-1" and "Prov
Rec". I have written one macro in Module 1 to automatically open to the
worksheet "Contents" regardless of which sheet was open when the user
last saved the file.

One cell in worksheet I-1 has a three-choice drop-down box. Whenever
the drop-down selection is changed on the I-1 worksheet, I want the
macro to either hide or unhide certain columns on the Prov Rec
worksheet.

Here's what I've done so far. I've borrowed from various sources I've
come across, but it's not coming together quite right. Any suggestions
as to what I'm missing and whether it can be written more efficiently?

All of these Subs are written in Module 2 one below the next. Let me
know if that's an issue as well.

Thanks in advance!
-------

Sub Run_On_Open()

' Run the macro CheckforChange any time an entry is made in I-1 cell
B17
ThisWorkbook.Worksheets("Contents").OnEntry = "CheckForChange"

End Sub


Sub CheckForChange()
Application.ScreenUpdating = False
Dim WorkbookTypeField As String
' A change in worksheet I-1 cell B17 will trigger the
SetWorkbookType macro.
ThisWorkbook.Worksheets ("I-1")
WorkbookTypeField = Range("B17")

If Not Application.Intersect(ActiveCell, WorkbookTypeField) _
Is Nothing Then SetWorkbookType
Application.ScreenUpdating = True

End Sub

Sub SetWorkbookType()
Application.ScreenUpdating = False
Dim WorkbookTypeField As String
' Checking value in worksheet I-1 cell B17
ThisWorkbook.Worksheets ("I-1")
WorkbookTypeField = Range("B17")

Select Case WorkbookTypeField
' If the value in worksheet I-1 cell B17 is Book-to-Tax...
Case "Book-to-Tax"
' Hide columns C-E on Prov Rec worksheet
Sheets("Prov Rec").Select
Columns("C:E").Select
Selection.EntireColumn.Hidden = True
' If the value in worksheet I-1 cell B17 is
Provision-to-Return...
Case "Provision-to-Return"
' Show columns C-E on Prov Rec worksheet
Sheets("Prov Rec").Select
Columns("C:E").Select
Selection.EntireColumn.Hidden = False
' If the value in worksheet I-1 cell B17 is
Extension-to-Return...
Case "Extension-to-Return"
Sheets("Prov Rec").Select
Columns("C:E").Select
Selection.EntireColumn.Hidden = False

End Select

Application.ScreenUpdating = True

End Sub


--
punsterr
------------------------------------------------------------------------
punsterr's Profile:

http://www.excelforum.com/member.php...o&userid=23961
View this thread: http://www.excelforum.com/showthread...hreadid=396718



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command Button to show/hide a different worksheet Marilyn Excel Discussion (Misc queries) 1 April 22nd 09 03:37 AM
Can I lock a worksheet and still be able to hide and show groups? Ryan Excel Discussion (Misc queries) 2 September 6th 08 12:13 AM
Show or Hide a worksheet with a macro Hasty Excel Discussion (Misc queries) 4 February 16th 06 08:54 PM
Hide/Show Shapes on Worksheet?? Ken Loomis Excel Programming 2 June 27th 05 11:12 PM
Hide/show worksheet Mal Excel Discussion (Misc queries) 5 March 29th 05 04:00 PM


All times are GMT +1. The time now is 05:31 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"