When I click the + beside a group of rows, how do I get it to hide a row outside of the grouped rows.
I tried this but it didn't work.
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Rows(11).Hidden = True) Then
Rows(22).EntireRow.Hidden = False
Else
Rows(22).EntireRow.Hidden = True
End If
End Sub
Simply hiding or un-hiding a row will not trigger the Event, To use this Event, you must change a cell value.
EDIT#1
You can almost get what you want with the Worksheet_SelectionChange event.
Expand or collapse the Group of rows containing cell A11 and then click anywhere in the worksheet and row #22 will also expand/collapse. Put the following in a standard module:
Public Sub IsHiddenA11()
With Range("A11")
If .EntireRow.Hidden Then
Range("A22").EntireRow.Hidden = True
Else
Range("A22").EntireRow.Hidden = False
End If
End With
End Sub
and put this in the worksheet code area:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Call IsHiddenA11
Application.EnableEvents = True
End Sub
Related
I need to stay at the same cell in the excel sheet and copy the cell value to down after hitting enter. need to keep the most recent value at the top and shift one by one down(the red arrow shows the current cell and the green arrow shows the most recent value). is there any way to do this with VBA programming?
If you add this code to the Worksheet_Activate() event procedure
Private Sub Worksheet_Activate()
Application.MoveAfterReturn = False
End Sub
so that the active cell will not change after data entry then you could code the Worksheet_Change() event for your data-entry tab as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
With Application
.EnableEvents = False
Target.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.EnableEvents = True
End With
End If
End Sub
and also add the code below the Worksheet_Deactivate() procedure
Private Sub Worksheet_Deactivate()
Application.MoveAfterReturn = True
End Sub
so that the normal active-cell-move-on-Enter behaviour is restored on other sheets.
I have 4 Cells (S11:T12) and a Command Button 1, what I want is, until all 4 cells are populated the command button should be disabled (Which I can do from the properties tab) and once all 4 cells are filled with number, the command button should be enabled and once the data from these cells are deleted, the command button should be disabled again.
Under which event should I write the code?
I tried this, but it does not work.
Private Sub Workbook_Open(Cancel As Boolean)
If Sheets("WorkArea").Range("S11:T12") = "" Then
Sheets("WorkArea").CommandButton1.Enabled = False
Else
Sheets("WorkArea").CommandButton1.Enabled = True
End If
End Sub
Use the WorkSheet_Change event handler to handle the change in cells, and you can use the CountBlank worksheet function to determine if a range is empty.
Private Sub Worksheet_Change(ByVal Target As Range)
If WorksheetFunction.CountBlank(Range("S11:T12")) = 4 Then
Sheets("WorkArea").CommandButton1.Enabled = False
Else
Sheets("WorkArea").CommandButton1.Enabled = True
End If
End Sub
Worksheet_Change
CountBlank
According to your question however, you actually want:
Private Sub Worksheet_Change(ByVal Target As Range)
If WorksheetFunction.CountBlank(Range("S11:T12")) = 0 Then
Sheets("WorkArea").CommandButton1.Enabled = True
Else
Sheets("WorkArea").CommandButton1.Enabled = False
End If
End Sub
A Worksheet Change
This solution will not work if the critical range contains formulas.
To count the number of cells that are not empty you can use the WorksheetFunction.CountA method.
Usually you don't want this code to run when there are changes outside of the range, so you will restrict the code to the range with the Application.Intersect method. You don't have to enable or disable the command button on each change since obviously the code will run on each change.
Since this is all happening in worksheet "WorkArea", there is no need to refer to it by its name i.e. you can safely use Range(rngAddress) and CommandButton1 instead of ThisWorkbook.Worksheets("WorkArea").Range(rngAddress) and ThisWorkbook.Worksheets("WorkArea").CommandButton1 respectively.
The Code
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Const rngAddress As String = "S11:T12"
Dim rng As Range
Set rng = Range(rngAddress)
If Not Intersect(Target, rng) Is Nothing Then
If WorksheetFunction.CountA(rng) = rng.Cells.Count Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End If
End Sub
I've found a VBA code that allows me to change the sheet name based on a cell value. That cell values are constantly changing but the 48 sheets that I have do not update unless I click into that sheet. How do I get the marcos to run every time the cell value changes?
Below is the code that I'm using.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set Target = Range("DK5")
If Target = "" Then Exit Sub
Application.ActiveSheet.Name = VBA.Left(Target, 31)
Exit Sub
End Sub
If you always activate the sheet before manually changing the contents of cell DK5 in that sheet, then enter this event macro in the each sheet that you want this functionality:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("DK5")) Is Nothing Then Exit Sub
Application.EnableEvents = False
ActiveSheet.Name = Range("DK5").Text
Application.EnableEvents = True
End Sub
If cell DK5 contains a formula, then a different event macro is required.
EDIT#1:
If the DK5 cells contain formulas rather than typed constants, then use these events macro instead:
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
Me.Name = Me.Range("Dk5").Value
Application.EnableEvents = True
End Sub
(one macro in each sheet)
After some googling I finally found some code where I could prevent users from placing formulas inside cells. It works great, that's until I protected the sheet. Can anyone tell me what I'm doing wrong? I'm really new to VB.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
On Error Resume Next
Range("I39").SpecialCells(xlCellTypeFormulas).ClearContents
On Error GoTo 0
Application.EnableEvents = True
End If
End Sub
The entire code for my sub is as follows. I need to stop users from pasting in the cells and putting formulas in them.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("C26")) Is Nothing Then
Application.CutCopyMode = True
Application.EnableEvents = False
On Error Resume Next
Range("C26").SpecialCells(xlCellTypeFormulas).ClearContents
On Error GoTo 0
Application.EnableEvents = True
End If
End Sub
Here is a version that facilitates formula checking over a range of cells:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rNoFormulas As Range
Set rNoFormulas = Range("C26:I26")
If Intersect(Target, rNoFormulas) Is Nothing Then Exit Sub
If Target.HasFormula Then
Application.EnableEvents = False
Target.ClearContents
MsgBox "formulas not allowed in cell " & Target.Address
Target.Select
Application.EnableEvents = True
End If
End Sub
If you want to allow data entry in cell C26, but not formula entry, then use the Change Event:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rNoFormulas As Range
Set rNoFormulas = Range("C26")
If Intersect(Target, rNoFormulas) Is Nothing Then Exit Sub
If rNoFormulas.HasFormula Then
Application.EnableEvents = False
rNoFormulas.ClearContents
MsgBox "formulas not allowed in cell C26"
rNoFormulas.Select
Application.EnableEvents = True
End If
End Sub
If you just want to protect certain cells only, no vba code is need.
follow this step :
Open sheet that contains cells or columns that you want to protect, press ctrl while selecting those cells or column to be protect, then right click, choose format cells, choose protection tab and uncheck the locked option. those cells or column will not be locked although you have protected the sheet. default setting is all cells in the sheets is locked so you must choose which cells you want to unlock while protecting the sheet. you may record a macro if you still want to use vba. hope this help
In excel 2000, is it possible to bind a vba function to be executed when a cell is clicked with the mouse?
Found a solution:
Make a named range for the cells you want to capture clickevent
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Application.ActiveSheet.Cells(1, 1).Value = Target.Address
If Not Intersect(Target, Range("MyNamedRange")) Is Nothing Then
' do your stuff
Range("A1").Select
Endif
Application.EnableEvents = True
End Sub
You can bind to a cell double click.
Open VBA, goto the worksheet you want to wire up the event
Select WorkSheet in the dropdown on the top left and BeforeDoubleClick in the top right
The check the Target.Address is equal to the address of the cell you care about and call the function you wish.
Something like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address(True, True, xlA1) = "$A$1" Then
Call MyDemo
End If
End Sub
Private Sub MyDemo()
MsgBox "Hello"
End Sub