I'm trying to hide a sheet in an Excel workbook based on the contents of any of the cells in a given range.
Let's say I have two sheets - "Sheet1" and "Sheet2".
On Sheet1, I want to set up a range - cell C10 to F10.
Each of these cells can either be blank, or contain "Yes" or "No" - chosen from a dropdown box.
If ANY of the cells in the range are set to "Yes", I want Sheet2 to be visible, otherwise (if all the cells are either blank or contain "No") I want Sheet2 hidden.
I've tried various pieces of code, including the below.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
Application.ScreenUpdating = False
For Each rCell In Range("C10:F10")
If rCell.Value = "Yes" Then
Worksheets("Sheet2").Visible = True
Else
Worksheets("Sheet2").Visible = False
End If
Next rCell
Application.ScreenUpdating = True
End Sub
I've got about as far as Sheet 2 being visible if all the cells equal "Yes" or if F10 equals "Yes", but not if only one of the cells contains "Yes".
No loop needed, and create an If to test whether the Cell that change is in the range to test, just to save some comp time:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("C10:F10"),Target) Is Nothing Then
Worksheets("Sheet2").Visible = Application.Countif(Range("C10:F10"),"Yes")>0
End If
End Sub
Modify and try:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim rng As Range, cell As Range
Dim Hide As Boolean
For Each ws In ThisWorkbook.Worksheets
Set rng = ws.Range("C10:F10")
For Each cell In rng
Hide = False
If cell.Value = "Yes" Then
Hide = False
Exit For
Else
Hide = True
End If
Next
If Hide = True Then
ws.Visible = False
End If
Next
End Sub
Related
I'm working with some data in excel spanning B9:AJ1108 - so multiple rows and columns. I am looking to hide all rows except where the value in column B matches the number in cell C5.
I've tried multiple and can only just about get everything to hide but the unhiding is the issue. I understand how to hide all and how to unhide all. What I need help with is how to hide all and then unhide if something matches the value in C5.
Code so far:
Private Sub CommandButton2_Click()
Worksheets("Employee information").Range("B9:B1108").Rows.Hidden = False
End Sub
Private Sub CommandButton1_Click()
Worksheets("Employee information").Range("B9:B1108").Rows.Hidden = True
'Need to put in the argument to search for C5 value
End Sub
I would also like this to be button controlled but I don't know if that is a case of creating a module or just code within the sheet?
For unhiding the rows you can use "Rows.EntireRow.Hidden = False"
If you want to use a button for the macro to get executed, create a button and excel will ask you which macro you want to get when you click the button.
value= Worksheets("Employee information").cells(5,3).value
That will give you the value of the cell C5, now you need to go through the rows and look for this value.
Hide Rows Not Containing Criteria in Column
Private Sub CommandButton1_Click()
With Worksheets("Employee information")
' Define Criteria (restrict to numbers).
Dim Criteria As Variant
Criteria = .Range("C5").Value
If Not IsNumeric(Criteria) Then
Exit Sub
End If
' Define Criteria Range.
Dim rng As Range
Set rng = .Range("B9:B1108")
End With
' Declare additional variables.
Dim hRng As Range ' Hide Range
Dim cel As Range ' Current Cell (in Source Range)
Dim CurVal As Variant ' Current Value (of Current Cell in Source Range)
' Create a union (Hide Range) of all the cell ranges
' that do not contain Criteria.
For Each cel In rng.Cells
' Evaluate Current Value.
CurVal = cel.Value
If IsNumeric(CurVal) Then
If CurVal = Criteria Then
GoTo NextCell ' Match found: do nothing.
End If
End If
' Match not found: add Current Cell to Hide Range.
If Not hRng Is Nothing Then
Set hRng = Union(hRng, cel)
Else
Set hRng = cel
End If
NextCell:
Next cel
' Hide rows of Hide Range.
If Not hRng Is Nothing Then
hRng.Rows.Hidden = True
End If
End Sub
I'm trying to see if it is possible to choose an item from a data validation list in either sheet 2 or 3 or 4, cell E2 to change automatically. Meaning i want the cell E2 in each of these sheets linked with each other, so if i change any one of them the other has to show the new value. The list itself is in sheet 1.
I also would like to extend this macro from just cell E2 to a range of cells (E2 to E300).
The problem i have now is if i use the code, it also changes the value of cell E2 in sheet 1. I have the code copied to modules in sheets 2, 3 4 and not in module of sheet 1.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
If Target.Address = "$E$2" Then
For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = Me.Name And Not ws.Name = "Sheet1" Then
If Not ws.Range(Target.Address) = Me.Range(Target.Address) Then
ws.Range(Target.Address) = Me.Range(Target.Address)
End If
End If
Next ws
End If
End Sub
Welcome to SO. You cna handle an unique sub from Worksheet_Change event
I made up this code in that event on each worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
UPDATE_E_VALUES ActiveSheet, Target
End Sub
And the code in a module of sub UPDATE_E_VALUES is:
Public Sub UPDATE_E_VALUES(ByVal vThisWK As Worksheet, ByVal vThisCell As Range)
Application.EnableEvents = False
Application.ScreenUpdating = False
If vThisCell.Row >= 2 And vThisCell.Row <= 300 And vThisCell.Column = 5 Then 'check if cell is in range E2:E300
Select Case vThisWK.Name
Case "Sheet1"
'we do nothing
Case "Sheet5"
'we do nothing
Case Else
'we update all E cells with same address
With ThisWorkbook
.Worksheets("Sheet2").Range(vThisCell.Address).Value = vThisCell.Value
.Worksheets("Sheet3").Range(vThisCell.Address).Value = vThisCell.Value
.Worksheets("Sheet4").Range(vThisCell.Address).Value = vThisCell.Value
End With
End Select
With ThisWorkbook
.Worksheets("Sheet2").Range(vThisCell.Address).Value = vThisCell.Value
End With
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Hope you can adapt this to your needs. You can add/remove extra lines where you want sheets to be affected or not
I'm trying to disable the range of cells on another sheet ( say Sheet4) based on the value in the current sheet(say Sheet1)
I have tried to disable the values in the sheet4("M4,M6,M8,M10,M12") based on the values in sheet1("C5") by using worksheet change function.
With Sheet1 I have used the below code and getting the error message
Subscript Out of Range
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myRng As Range
Dim Cancel As Boolean
If Not Intersect(Target, Me.Range("C5")) Is Nothing Then
Set myRng = ThisWorkbook.Worksheets("Sheet4").Range("M4,M6,M8,M10,M12")
Me.Unprotect Password:=SHEET_PASSWORD
If Me.Range("C5").Value = "Yes" Then
myRng.Locked = False
Else
myRng.Locked = True
ThisWorkbook.Worksheets("Sheet4").Range("M4,M6,M8,M10,M12") = ""
End If
Me.Protect Password:=SHEET_PASSWORD
End If
End Sub
If I choose "Yes" in the cell value C5 in Sheet1, the cell values (M4,M6,M8,M10,M12) in sheet4 should be enabled. If i choose "No" in the cell value C5 in Sheet1, the cell values (M4,M6,M8,M10,M12) in sheet4 should be disabled.
I think This is what you are looking For.
Private Sub Worksheet_Change(ByVal Target As Range)
If ChangeEventFlag = 0 Then
Dim myRng As Range
Dim Cancel As Boolean
If Not Intersect(Target, Me.Range("C5")) Is Nothing Then
With ThisWorkbook.Worksheets("Sheet4")
.Unprotect Password:="password"
.Cells.Locked = False
Set myRng = .Range("M4,M6,M8,M10,M12")
If Me.Range("C5").Value = "Yes" Then
myRng.Locked = False
Else
myRng.Locked = True
myRng.Value = ""
End If
.Protect Password:="password"
End With
End If
End If
End Sub
Note:
Not sure about the use of ChangeEventFlag
Change the Password, I used password for testing
Use:
Whichever sheet you will paste this code on will Lock Range("M4,M6,M8,M10,M12") of Sheet4 if it's C5 is No(Anything Other Than Yes Actually) and will unlock it if C5 is Yes
I'm looking for some help please with some VBA.
Let's say I have a range of cells (B4:B12), so if I input data in a cell within the range I would like to clear all cells in the same range except for the cell in which I inputed the data. So that I can only have 1 cell with data in the range.
So if B5 had data and I inputed data in B7 then B5 would clear, then if i entered data in B10 then B7 would clear...
I hope there is a solution as I have been trying to find an answer for the past couple of hours.
I would do it this way:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Set myRange = Sh.Range("B4:B12")
'set the current cell/address/value so we don't lose them when the range is cleared
Set cell = Sh.Range(Target.address)
value = Target
'disable/enable so this isn't called every time a cell is cleared
'clear the range then reset the to entered value
If Not Intersect(Target, myRange) Is Nothing Then
Application.EnableEvents = False
myRange.Clear
cell.value = value
Application.EnableEvents = True
End If
End Sub
Or you could use worksheet event to bevplaced in the relevant worksheet code pane
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myVal As Variant
With Range("B4:B12") ‘reference your range
If Not Intersect(Target, .Cells) Is Nothing And Target.Count = 1 Then ‘ if changed range is one cell within the referenced one
myVal = Target.Value ‘store changed cell value
Application.EnableEvents = False
.ClearContents ‘ clear the content of referenced range
Target.Value = myVal ‘ write changed cell value back
Application.EnableEvents = True
End If
End With
End Sub
I am searching for a solution in excel. My goal is to enter a value in two different sheets but this value is the same in condition that if we change a value in any of these two sheets it will change automatically in the second one. So I can't use the =(reference to the cell) because it will create the link in one way.
Hope to find a solution.
This would do the trick. If I change the value of cell A1 in Sheet1, the value of cell A1 in Sheet2 is set be equal to that of A1 in Sheet1 and vice versa.
Past this code into Sheet1 in the VBA Editor. Then copy this code and also past this into Sheet2 - change "Sheet2" into "Sheet1" in the code (on line 15)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Changed As Range
Dim vNew As String
Set Changed = Range("A1")
Application.ScreenUpdating = False
Application.EnableEvents = False
If Not Intersect(Target, Changed) Is Nothing Then
vNew = Target.Value
Worksheets("Sheet2").Range("A1") = vNew
ActiveCell.Offset(1, 0).Select
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Set Changed = Nothing
End Sub