Hi,
I need to write a macro to hide and unhide columns in multiple speadsheets on particular cell value (say Yes/No).
I have 6 excel worksheets - Sheet 1 - Sheet 6.
In first worksheet (Sheet 1), I have given a condition to be provided by user in terms of "Yes" or "No".
In case, if in the first worksheet the condition provided is "Yes", I have to hide Coumns F, G, H and K in other five worksheets.
Last edited by gmalpani; 11-24-2011 at 10:17 AM.
Hi,
Try this:
Private Sub Worksheet_Change(ByVal Target As Range) Dim wsEachSheet As Worksheet If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub For Each wsEachSheet In ThisWorkbook.Worksheets If wsEachSheet.Name <> "Sheet1" Then If UCase(Range("A1").Value) = "YES" Then wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = True ElseIf UCase(Range("A1").Value) = "NO" Then wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = False End If End If Next wsEachSheet End Sub
Right click on the Sheet1's tab and select View Code. Change A1 to the cell that will have Yes/No in it.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Hi Dom,
Thanks for the quick reply .. It really helps !
In this macro, it will hide the columns in all the worksheets.
In case, I have to hide columns in only WS 2, 3, 4 and 5 and rest ws 6 and 7 should not be affected. How can I make this change in the macro provided by you.
Br,
Gaurav
Try this:
Private Sub Worksheet_Change(ByVal Target As Range) Dim wsEachSheet As Worksheet If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub For Each wsEachSheet In Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet5")) If UCase(Range("A1").Value) = "YES" Then wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = True ElseIf UCase(Range("A1").Value) = "NO" Then wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = False End If Next wsEachSheet End Sub
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Hi Dom,
I need to modify the macro little bit.
In addition to the columns, I have to hide Row 89 - Row 99.
Below statements is for hiding columns in respective worksheet. Can you modify it to hide rows 89 - 99 as well. I have tried to modify this, but I think there is some error and it requires some AND operation in between.
If UCase(Range("A1").Value) = "YES" Then
wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = True
wsEachSheet.Range("89:99").EntireRow.Hidden = True
ElseIf UCase(Range("A1").Value) = "NO" Then
wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = False
wsEachSheet.Range("89:99").EntireRow.Hidden = True
Close, you just needed to change True to False to unhide the columns:
Private Sub Worksheet_Change(ByVal Target As Range) Dim wsEachSheet As Worksheet If Intersect(Target, Range("A1")) Is Nothing Then Exit Sub For Each wsEachSheet In Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet5")) If UCase(Range("A1").Value) = "YES" Then wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = True wsEachSheet.Range("89:99").EntireRow.Hidden = True ElseIf UCase(Range("A1").Value) = "NO" Then wsEachSheet.Range("F:F,G:G,H:H,K:K").EntireColumn.Hidden = False wsEachSheet.Range("89:99").EntireRow.Hidden = False End If Next wsEachSheet End Sub
Please remember to use code tags when posting code to the forum.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Hi Dom,
Thanks a lot for your time and support !
I am a beginner in Excel Macros. Could you please suggest me from where I should start ?
Does this forum provides some tutorials or docs which can help me.
Last edited by gmalpani; 11-24-2011 at 10:15 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks