VBA script or something else to automatically pushes a button - excel

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

Related

Cell assignment causes For Next loop to not exit [duplicate]

I am having a problem with Excel crashing, when I run VBA code on an excel sheet.
I'm trying to add the following formula on worksheet change:
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("testpage").Range("A1:A8").Formula = "=B1+C1"
End Sub
When this code is run i get a message saying "excel has encountered a problem and needs to close" and excel closes.
If I run the code in the Worksheet_Activate() procedure, it works fine and doesn't crash
Private Sub Worksheet_Activate()
Worksheets("testpage").Range("A1:A8").Formula = "=B1+C1"
End Sub
But I really need it to work in the Worksheet_Change() procedure.
Has anyone experienced similar crashes when using the Worksheet_Change() event and can anyone point in the right direction to fix this issue ?
I recommend this when using Worksheet_Change
You do not need the sheet name. In a Sheet Code Module, an unqualified Range reference refers to that sheet. That said, it is clearer to use the Me qualifier. If you are trying to use another sheet, then qualify the range reference with that sheet.
Whenever you are working with Worksheet_Change event, always switch Off events if you are writing data to any cell. This is required so that the code doesn't retrigger the Change event, and go into a possible endless loop
Whenever you are switching off events, use error handling to turn it back on, else if you get an error, the code will not run the next time.
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
Me.Range("A1:A8").Formula = "=B1+C1"
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
Few other things that you may want to know when working with this event.
If you want to ensure that the code doesn't run when more than one cell is changed then add a small check
Private Sub Worksheet_Change(ByVal Target As Range)
'~~> For Excel 2003
If Target.Cells.Count > 1 Then Exit Sub
'
'~~> Rest of code
'
End Sub
The CountLarge was introduced in Excel 2007 onward because Target.Cells.Count returns an Long value which can error out in Excel 2007 becuase of increased total cells count.
Private Sub Worksheet_Change(ByVal Target As Range)
'~~> For Excel 2007
If Target.Cells.CountLarge > 1 Then Exit Sub
'
'~~> Rest of code
'
End Sub
To work with all the cells that were changed use this code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aCell As Range
For Each aCell In Target.Cells
With aCell
'~~> Do Something
End With
Next
End Sub
To detect change in a particular cell, use Intersect. For example, if a change happens in Cell A1, then the below code will fire
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1")) Is Nothing Then
MsgBox "Cell A1 was changed"
'~~> Your code here
End If
End Sub
To detect change in a particular set of range, use Intersect again. For example, if a change happens in range A1:A10, then the below code will fire
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1:A10")) Is Nothing Then
MsgBox "one or more Cells in A1:A10 range was changed"
'~~> Your code here
End If
End Sub
Note: If you were getting an error earlier and you made the above changes and If your code is still not working then it is possible that the events have not been reset. In the Immediate Window, type Application.EnableEvents = True and press the ENTER key. This will reset it to True. If you do not see the Immediate Window, the press the shortcut key Ctl+G to launch the Immediate Window.
Excel was crashing, not the VBA function.
The events were not disabled and the call stack was filled by an infinite loop of OnChange events.
A little advice that helps finding this type of errors: set a breakpoint on the first line of the event, then execute it step by step pressing F8.
Also this solution is good:
Option Explicit
Private Busy As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Busy Then
Busy = True
Range("A1:A8").Formula = "=B1+C1"
Busy = False
End If
End Sub

VBA - Target Offset Value crashed [duplicate]

I am having a problem with Excel crashing, when I run VBA code on an excel sheet.
I'm trying to add the following formula on worksheet change:
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("testpage").Range("A1:A8").Formula = "=B1+C1"
End Sub
When this code is run i get a message saying "excel has encountered a problem and needs to close" and excel closes.
If I run the code in the Worksheet_Activate() procedure, it works fine and doesn't crash
Private Sub Worksheet_Activate()
Worksheets("testpage").Range("A1:A8").Formula = "=B1+C1"
End Sub
But I really need it to work in the Worksheet_Change() procedure.
Has anyone experienced similar crashes when using the Worksheet_Change() event and can anyone point in the right direction to fix this issue ?
I recommend this when using Worksheet_Change
You do not need the sheet name. In a Sheet Code Module, an unqualified Range reference refers to that sheet. That said, it is clearer to use the Me qualifier. If you are trying to use another sheet, then qualify the range reference with that sheet.
Whenever you are working with Worksheet_Change event, always switch Off events if you are writing data to any cell. This is required so that the code doesn't retrigger the Change event, and go into a possible endless loop
Whenever you are switching off events, use error handling to turn it back on, else if you get an error, the code will not run the next time.
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
Me.Range("A1:A8").Formula = "=B1+C1"
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
Few other things that you may want to know when working with this event.
If you want to ensure that the code doesn't run when more than one cell is changed then add a small check
Private Sub Worksheet_Change(ByVal Target As Range)
'~~> For Excel 2003
If Target.Cells.Count > 1 Then Exit Sub
'
'~~> Rest of code
'
End Sub
The CountLarge was introduced in Excel 2007 onward because Target.Cells.Count returns an Long value which can error out in Excel 2007 becuase of increased total cells count.
Private Sub Worksheet_Change(ByVal Target As Range)
'~~> For Excel 2007
If Target.Cells.CountLarge > 1 Then Exit Sub
'
'~~> Rest of code
'
End Sub
To work with all the cells that were changed use this code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aCell As Range
For Each aCell In Target.Cells
With aCell
'~~> Do Something
End With
Next
End Sub
To detect change in a particular cell, use Intersect. For example, if a change happens in Cell A1, then the below code will fire
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1")) Is Nothing Then
MsgBox "Cell A1 was changed"
'~~> Your code here
End If
End Sub
To detect change in a particular set of range, use Intersect again. For example, if a change happens in range A1:A10, then the below code will fire
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1:A10")) Is Nothing Then
MsgBox "one or more Cells in A1:A10 range was changed"
'~~> Your code here
End If
End Sub
Note: If you were getting an error earlier and you made the above changes and If your code is still not working then it is possible that the events have not been reset. In the Immediate Window, type Application.EnableEvents = True and press the ENTER key. This will reset it to True. If you do not see the Immediate Window, the press the shortcut key Ctl+G to launch the Immediate Window.
Excel was crashing, not the VBA function.
The events were not disabled and the call stack was filled by an infinite loop of OnChange events.
A little advice that helps finding this type of errors: set a breakpoint on the first line of the event, then execute it step by step pressing F8.
Also this solution is good:
Option Explicit
Private Busy As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Busy Then
Busy = True
Range("A1:A8").Formula = "=B1+C1"
Busy = False
End If
End Sub

Trigger VBA script when any 1 cell selected containing a defined string

The code below worked fine so long as the "MainSubroutine" only needed to be triggered when cell G14 was clicked on, but now I need to widen the trigger event to whenever any target cell on the sheet is clicked on and it has the specific string, "Click to Learn More".
So how do I write that? I tried altering line 2 to "If Selection.Count = 1 AND selection.value = "Click to Learn More" Then..." but obviously that didn't work.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("G14")) Is Nothing Then
Call MainSubroutine
End If
End If
End Sub
This should do it? If it can contain the string use Instr or similar.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If target.Count = 1 Then
If Target.value="Click to learn more" Then
Call MainSubroutine
End If
End If
End Sub

VBA Code to autohighlight modified cells?

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.

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

Resources