Run macro when something changes - vba

How to call a macro automatically in Microsoft Word when I change something?
For example, I want to call the macro 'Tester' when I change anything.
Sub Tester()
Call Msgbox("Something has changed")
End Sub

Related

issue with concatenated subs

DISCLAIMER: I'm not a developer, just an average guy trying to use VBA on his own project. First time I post on stackoverflow so forgive me if I'm violating any unwritten community rule..
Hi all, I'm trying to have a bunch of subs running in sequence, so the next one starts only when the previous one has finished.
The problem is that excel keep on crashing during the execution, while if I run each sub manually they have no issue at all.
So far I have been using this method: First I have a mini sub that assigns value=1 to the global variable "oneclick", and then calls the first macro of the chain
then, in each sub there is a tiny piece just before the end:
sub macro1()
...macro code...
if oneclick=1 then
call macro2
end if
end sub
and so on for all the subs until the final sub resets the oneclick variable to zero.
I have no idea why this keeps on crashing. I can see that actually none of these subs is really closing until the very end of the last one, would this hit any sort of code limitation? I would be actually happier to have a single master sub that directs the process instead of relying on a kludge like this! any suggestion?
EDIT:
oh wow already so many answers.. now I'm gonna try some of those. Answering some of your questions:
1) the crash is really something sudden, not even an error message, simply excel quits and reopens, in the same state as it was when the macro chain first started.
2) I agree with you guys that there should be a master sub calling the smaller ones, however last time I tried I got a series of problems because one macro needs to work on the results of the previous one and so on. How do you tell to vba to wait until the previous sub has ended?
It is best if you don't chain the macros, but instead call them from another sub. This way you will have a clear way of understanding what you are doing, in what order.
Sub AllOfIt()
macro1
DoEvents
macro2
DoEvents
macro3
End Sub
Sub macro1
...
End Sub
Sub macro2
...
End Sub
Sub macro3
...
End Sub
There is generally no need to worry about if something starts before the previous macro ended. That does not happen unless you do something like using Application.OnTime
What probably is happening is that oneclick is not defined with a Dim statement, so by default its defined at procedure level, meaning if you set oneclick it to 0 in another Sub, your setting another local/procedure level variable, and your variable in macro1 is unchanged, so your chain of macros never stops, which leads to a stack overflow/crash as stated in one of the comments above.
So either define oneclick as public variable (I recommend to define it as Boolean as it seems to basically be a switch .... True/False) or you pass the variable as argument down your chain of macros ( Sub Macro2(ByRef oneclick as Boolean) ).
All that said, as one of comments above stated, you can have all your chained subs in the main sub ( macro1 ) as they will only get executed one after the other, e.g.
Sub macro1()
Dim oneclick As Boolean
oneclick = True 'Need to get set to true to start
If oneclick Then Call macro2(oneclick)
If oneclick Then Call macro3(oneclick)
........
End Sub
Sub macro2(ByRef oneclick As Boolean)
oneclick = False 'One of your macros has to set oneclick to false to stop the chain/execution, probably under acondition
End Sub

Userform in Excel to control the flow of a macro

This has got to be ridiculously easy, but I just cannot figure it out searching the web.
I have an Excel macro that performs various data entry/manipulation tasks. There is a point in the macro where I want the user to have the option of using data in column A or column B for calculations. All I want is to call a userform with two command buttons which pass their "true" or "false" value to the main macro and then perform "if" statements based on this information.
The trouble is I cannot get the userform to "tell" the macro anything. I'm sure this is a major noob question and I'm missing the method, but I cannot get this to work. Thank you!
Edit, I've attached the userform code below by request:
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub OptionButton1_Click()
End Sub
Private Sub OptionButton2_Click()
End Sub
Private Sub UserForm_Initialize()
End Sub
you could avoid userform for that and use a simple message box:
If MsgBox("use data in column A or B [Yes=A, NO=B]", vbYesNo) = vbYes Then
' code for using data in column A
Else
' code for using data in column B
End If

Macros not showing up in the run macro menu

I started learning VBA and I don't understand why some macros copy-pasted from the internet do not show up in the run macro menu (Alt-F8).
Below there are 2 macros, but only the second one is showing. Why? And how do I fix it?
Sub Test1(ByVal Target As Hyperlink)
'...
End Sub
Sub Test2()
'...
End Sub
Macros with arguments are not available in Macros list because they cannot be run alone instead they are called by another macro by passing the required arguments.
If a Sub declaration contains parameters it will not show there.
You cannot call macros that have parameters as you are describing. If you need to have a parameter, you can take it out and then have the user input the value.
Sub Test1()
Dim hyperLink As String
hyperLink = InputBox("Please input hyperlink", "My Parameter")
'...
End Sub
Alternatively, if the hyperlink is in your document, grab the value from your document instead.
Here are my 5 cents - if you give an optional parameter, you will be able to call the sub routine, even if it will not be shown among the ones which you can chose from.
Write aaaaTestMe and press Run.
Public Sub aaaaTestMe(Optional lngA As Long = 8)
Debug.Print lngA
End Sub
You can call an even private macro from any excel object you can assign a macro, calling it this way:
'MyWorkbook'!'MyModule.MyProcedure "MyParameter1"'
(be careful with single quotes: ' around procedure name with parameter)

Macro Throwing error while running during closing - Sub or Function not Defined

I have three macros which are placed in the sheet 2 of the excel sheet. I want to run the macro whenever the excel sheet is closed. I used the following code,
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Macro1
Macro2
Macro3
End Sub
where the Macro1, Macro2 and Macro3 are macros in sheet2. This code is placed in ThisWorkBook part of the excel sheet. When I execute this I get an error,
Compile Error:
Sub or Function not defined
Suppose the sheet2 has name "Nameofsheet2" , Can anybody help me how to solve this problem? I want the macros in the sheet2 to run whenever the excel sheet is closed.
Thanks
Place your macros code on a Module and it will probably work.
well the simplest way is to call them like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
SheetX.Macro1
SheetX.Macro2
SheetX.Macro3
End Sub
Where SheetX is the internal code-name of your Sheet2 (this is the name that you see in the Project Window of the VBA IDE, it may not be the same name as shown in the Excel sheet tabs)
If you want to call a macro from a workbook that's not the current one, just use Application.Run [workbook]!MacroName,
i.e. Application.Run "Personal.xlsb!clearImmediateWindow"
If it's stored in another sheet, use Sheet2.MacroName.

Run macro on cell being changed by user

I am trying to make a macro automatically run if any cells in a range is changed (C5 to c25).
As you see in the code below, it should automatically bring up a message box asking the user whether or not to continue (if the user says yes then runs the macro).
I cannot get the code to start running though once I have changed any one of the cells (from c5 to c25).
Here is the code - it is not all of my own:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("C5:C25")) Is Nothing Then Reminder
End Sub
Sub Reminder()
'
' Reminder Macro
'
response = MsgBox("Do you want to set a reminder in Outlook for when the next update is required? If yes, make sure your Microsoft Outlook is open.", vbYesNo)
If response = vbNo Then
MsgBox ("You selected 'No'")
Exit Sub
End If
'Rest of my macro code goes here...
End sub
Thank you!
Ensure your code is in the Worksheet's code module. From the comments above, you indicate it's in Module 3. It needs to be moved to the worksheet's code module.
Try:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("C5:C25"), Target) Is Nothing Then
Call reminder
End If
End Sub
Your code works fine, make sure you put it in the Worksheet object. Also I believe the code won't be active until you save as an xlsm, close, and reopen.