VBA Code to autohighlight modified cells? - excel

Currently using this code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address = "$B$1" Then Range("B9:AE53").Interior.Color = xlNone
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Range("B9:AE53")) Is Nothing Then
For Each c In Intersect(Target, Range("B9:AE53"))
Target.Interior.Color = vbYellow
Next c
End If
End Sub
Autohighlight only works when I manually edit (or F2 then enter) the cells in B9:AE53. I was hoping for something that would change the cell color if I edit the data in the orders sheet (reference for B9:AE53).
Was also hoping to transfer the event from B1 to a command button.

You can easily change the second part of your question, the one where you wish to reset the cell colors to nothing, to be executed by a button.
Change the code to this:
Sub CleanUp()
Range("B9:AE53").Interior.Color = xlNone
End Sub
Then add a button via the Forms Control Button in the developer toolbar and asign the above macro to it. This will clean up the area. Once you specifiy what the desired outcome is for the first part of you question I might be able to help with that.

Related

VBA script or something else to automatically pushes a button

Is it possible to make a VBA script or something else that automatically pushes a button with a macro in it?
For example, if the cell (1:I) has WAAR in it, that the button (planning maken) is pushed automatically (see image)?
It doesn't have to be this way, if you know something else it's fine too.
No, it is not possible to "push a button" automatically.
However, I imagine that the code behind the "push button" is something like:
Sub CommandButton_Click(Target as Range)
...
End Sub
What you can do, is create a macro that launches this procedure, but then you have the problem, when would anybody launch that macro? There, you have provided the answer (more or less) yourself: when cell "I1" gets a certain value. This, however, is not possible: you cannot declare a macro to be launched when a cell gets a certain value.
But: you can launch a macro when any cell is changed, it works as follows:
What does this all mean?
In the VBA project editor (left pane), you need to select your sheet, and in the source code editor, you need to go for "Worksheet" and "Change", like this you have the event which is called whenever some value changes in your worksheet.
The code itself looks as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 9 And Target.Value = True Then
CommandButton_Click (...)
End If
End Sub
This means that the macro will always be launched, but you only want something to happen when:
Target.Row = 1 (which correspond with cell "I1")
Target.Column = 9 (which corresponds with cell "I1")
Target.Value = True (which corresponds with value `WAAR`, I assume here that `WAAR` is just
the Dutch translation for the boolean `True` and not some string value)
You can use Worksheet_Change event to trigger a macro. So, you have to make sub and call it from Worksheet_Change event. Sub will be same as Planning Button doing. Check below code
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("I1")) Is Nothing Then
If Target = "WAAR" Then
Call MyMacro
End If
End If
End Sub
Sub MyMacro()
MsgBox "Value is entered to I1 cell"
End Sub
Depending on which type of button it is you need to either call the Sub the button is connected to, or trigger the button_click event procedure.
Say you have a Forms button which is calling the Sub Test:
Public Sub Test()
MsgBox "Forms Button clicked"
End Sub
In your Worksheet_Change() procedure your code would look like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 9 And Target.Value = True Then
Test
End If
End Sub
If you have an ActiveX button your code would look like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 9 And Target.Value = True Then
Sheet1.CommandButton1.Value = True ' This triggers the click event
End If
End Sub

Make private sub run when formula changes

I've searched for an answer to this but can't find it. I have the following code to dynamically name a button:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("E5")) Is Nothing Then _
Me.Buttons(1).Caption = Range("E5").Value
End Sub
Where "E5" is linked "D5", which in turn is linked to "A5" - i.e. "E5" updates when "A5" changes, via "D5".
How do I change this code so the sub will run when A5 is changed by the user? I'm going to eventually have multiple buttons on this page that need dynamic names, so the best change would be one that runs all private subs when any change is made on the sheet...if that's possible!
Thanks so much all help - really appreciate it!
DDT
You should use
Private Sub Worksheet_Change(ByVal Target As Range)
' if the cell being changed is A5
If Target.Address = Me.Range("$A$5").Address Then
' if E5 is not blank
If Me.Range("$E$5").Value2 <> vbNullString Then
' change the buttons Name
Me.Buttons(1).Name = Me.Range("$E$5").Value2
End If
End If
End Sub
Your If Not Intersect(Target, Range("E5")) Is Nothing Then is getting the intersection of your changed range and Range("E5"). Since Range("A5") never intersects with Range("E5"), your code will always return Nothing and thus always fail.
Note that this changes the buttons name but not the text that is displayed on the button. If you want the button to display different text, you can simply select your button and, in the formula bar, set its formula to =$E$5.

VBA Worksheet Change is not working properly?

I wrote the following code in my worksheet and all ranges are in this same sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("DistMatrix")) Is Nothing Then
Dim out1() As Double
out1 = OutStat(bucket(Target), Range("RegScale"))
FwdOut = outright(bucket(Target), Range("RegScale"))
Call NewScatter(FwdOut, out1)
End If
End Sub
I want to run a called sub if I choose a cell in the range DistMatrix.
This is partly working. I have to click on a cell in the range as if I want to write in it and then afterwards pick another one for the called sub to run.
I want however the sub to run as soon as I pick the cell. I don't want to go have to double click it as if it were to edit it and then pick another one for it to run.
You can use Worksheet_SelectionChange instead.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("DistMatrix")) Is Nothing Then
Dim out1() As Double
out1 = OutStat(bucket(Target), Range("RegScale"))
FwdOut = outright(bucket(Target), Range("RegScale"))
Call NewScatter(FwdOut, out1)
End If
End Sub

Macro to run when cell value changes

I am realy not sure what the problem with this code is or if it is a problem with the formatting of my worksheet but the following code will not automatically run when the value of D8 changes:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$8" Then
Toggle_Rows
End If
End Sub
Is it a formating issue or a code issue?
After seeing your question Here
I'd like you to try this:
In the work book goto the sheet that you wish to change the value of D8 and have the code run.
At the bottom of Excel right click the Tab with the name of that sheet and then select View Code
At the top of your Code window you should see the word "(General)".
Click on the drop down and select "WorkSheet", you should see a new Sub called
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
You can delete those lines. Now to the right of where you selected Worksheet theres another drop down this one should say "SelectionChange" Click on that then Select the word "Change" from the drop down. You should see another new sub
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
In between those 2 lines is where you want
If Target.Address = "$D$8" Then
Toggle_Rows
End If
Not an answer but a way to debug. as this works for me both manually and via code.
Try one of the following to see the value you get:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
Debug.Print Target.Address
If Target.Address = "$D$8" Then
Toggle_Rows
End If
End Sub

excel VBA run macro automatically whenever a cell is changed

Is there a simple way to get Excel to automatically execute a macro whenever a cell is changed?
The cell in question would be in Worksheet("BigBoard").Range("D2")
What I thought would be a simple Google inquiry is proving to be more complicated - every sample involved intersects (whatever those are) or color formatting or any other number of things that appear to be irrelevant.
Yes, this is possible by using worksheet events:
In the Visual Basic Editor open the worksheet you're interested in (i.e. "BigBoard") by double clicking on the name of the worksheet in the tree at the top left. Place the following code in the module:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("D2")) Is Nothing Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error Goto Finalize 'to re-enable the events
MsgBox "You changed THE CELL!"
Finalize:
Application.EnableEvents = True
End Sub
Another option is
Private Sub Worksheet_Change(ByVal Target As Range)
IF Target.Address = "$D$2" Then
MsgBox("Cell D2 Has Changed.")
End If
End Sub
I believe this uses fewer resources than Intersect, which will be helpful if your worksheet changes a lot.
In an attempt to find a way to make the target cell for the intersect method a name table array, I stumbled across a simple way to run something when ANY cell or set of cells on a particular sheet changes. This code is placed in the worksheet module as well:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 0 Then
'mycode here
end if
end sub
In an attempt to spot a change somewhere in a particular column (here in "W", i.e. "23"), I modified Peter Alberts' answer to:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Target.Column = 23 Then Exit Sub
Application.EnableEvents = False 'to prevent endless loop
On Error GoTo Finalize 'to re-enable the events
MsgBox "You changed a cell in column W, row " & Target.Row
MsgBox "You changed it to: " & Target.Value
Finalize:
Application.EnableEvents = True
End Sub
I was creating a form in which the user enters an email address used by another macro to email a specific cell group to the address entered. I patched together this simple code from several sites and my limited knowledge of VBA. This simply watches for one cell (In my case K22) to be updated and then kills any hyperlink in that cell.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("K22")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Range("K22").Select
Selection.Hyperlinks.Delete
End If
End Sub

Resources