I am trying to put formula output and user input on same cell
Private Sub Worksheet_Change(ByVal target As Range)
Application.EnableEvents = False
If Intersect(target, Range("G1103:G1104")) Is Nothing Then
End If
ActiveSheet.Range("G1105").Formula = "=if(G1104="""","""",(((G1104-G1103)/G1103)*100))"
Application.EnableEvents = True
Exit sub
but this is not working and i not able to do manual entry on cell G1105.
I want to leave cell G1103 and G1104 for user input and calculate G1105 but user should be able to directly put number in cell G1105 without changing the formula and it should do calculation next time user put value in cell G1103 and G1104.
I cannot do exit sub after if nothing then because i have other code that needs to run on same event worksheet_change.
This will replace whatever is in G1105 with your Formula only when G1103 or G1104 are changed by the user.
Private Sub Worksheet_Change(ByVal target As Range)
Application.EnableEvents = False
If Not Intersect(target, Me.Range("G1103:G1104")) Is Nothing Then
Me.Range("G1105").Formula = "=if(G1104="""","""",(((G1104-G1103)/G1103)*100))"
End If
' rest of your code here
Application.EnableEvents = True
Exit sub
Related
I am using worksheet change event to trigger copy paste values. Worksheet change code is in the sheet2
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Then
Call myTR1
End If
Please note AB2 cell takes it's value from another sheet
Copy paste code is in a Module
Sub myTR1()
Sheets("BA1").Range("AR6:AS8").Value = Sheets("BA1").Range("AL17:AM19").Value
End Sub
When target range changes to "OK", my copy paste macro is not triggering. What am I doing wrong?
Using your eaxct code worked, although you didnt have end sub in your example?
EDIT:
Bear in mind the 'OK' is case sensitive so it will have to be in uppercase to fire, if you want it to fire either on lower or upper you can use the second code.
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Then
Call myTR1
End If
End Sub
Sub worksheet_change(ByVal Target As Range)
Application.EnableEvents = True
Set Target = Range("AB2")
If Target.Value = "OK" Or Target.Value = "ok" Then
Call myTR1
End If
End Sub
My spread sheet has target cells and cells that I want to be able to enter manual data without the Worksheet Change to activate or run. How do I allow these open user cells to be populated without the private sub running??
More information sample code. There are many code sections like this in the same format.
Private sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True
If Intersect(Target, Range("$B$8:$k$9")) Is Nothing Then
GoTo NEXT10
'Application.EnableEvents = False
End If
Application.EnableEvents = True
MsgBox Target.Address
'-------------------------------------------------------------------
'Application.EnableEvents = True
Application.EnableEvents = False
If Target.Address = "$B$8" Then
If Target.Value >= 0 Then
Range("B9") = Range("B8").Value * 42
End If
ElseIf Target.Address = "$B$9" Then
Range("B8") = Range("B9").Value / 42
End If
Application.EnableEvents = True
End Sub
The problem is if I enter, delete value or text in any empty cells on the worksheet, it activates the Worksheet_Change (ByVal Target ....).I use the If Intersect(Target,Range($B8$:$k$8) Is Nothing to bypass the target if no change. This format is used for other target ranges of important. But I do not understand why when any cell is changed the program runs?? Can this be avoid so various manual entries can be performed? For example text notes, labels, etc.
The Next10 is the start of another section of code but with diferent target address. If the target addess indicated is not intersected, it goes the check the next intersection.
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)
I have a macro that checks a cell once it has been changed and formats the input of that cell to an accepted format. This was done to make it easier for the user by allowing them to be able to input the data in multiple ways and have the cell come out correctly.
I have:
Private Sub Worksheet_Change(ByVal Target As Range)
Set rCells = Range("C3:C4, G9:G24, C60:C61, G66:G81")
Application.EnableEvents = False
If Not Application.Intersect(rCells, Range(Target.Address)) Is Nothing Then
Format(Range(Target.Address))
End If
Application.EnableEvents = True
End Sub
...to catch the change and then format the input to the correct format.
I want the user however to be able to leave the cell blank.
What can I add to this block to allow the user to clear a cell using DEL or BACKSPACE without triggering format()?
I have tried:
If IsEmpty(Target) Then Exit Sub
But that doesn't seem to do it.
Thank you for your help!
You can use this line to check whether all cell in Target range are empty:
If WorksheetFunction.CountBlank(Target) = Target.Cells.Count Then Exit Sub
also as I mentioned in comments
change Format(Range(Target.Address)) to Format(Target)
don't forget to change function name Format in real code to something like My_Format since there is built-in function with this name:)
Test for =""
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = "" Then Exit Sub
Set rCells = Range("C3:C4, G9:G24, C60:C61, G66:G81")
Application.EnableEvents = False
If Not Intersect(rCells, Target) Is Nothing Then
Format (Target)
End If
Application.EnableEvents = True
End Sub
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