Running a macro automatically - excel

I want a macro to run automatically everytime I navigate to a specific worksheet. I know this is possible because there is another macro on the worksheet that updates information automatically, but I can't seem to figure out how to set it so that my new macro will run automatically. Note all of the macros are in the same folder "Modules."

I think this might be along the lines of what you need. If you are in the VBA editor, on the left navigator pane, double click the sheet you are interested in having run the code automatically. In the window that opens, on the upper left change the drop down to "Worksheet". Then in the upper right drop down, change the event handler to "Activate"
You should see a new blank sub generate:
Private Sub Worksheet_Activate()
'put some code here
End Sub
Now anytime you activate that sheet, any code you enter in that sub will be run automatically. You can look at the event handlers in the right drop and try other ones if they suite your needs better.

Related

Get command button by using cell address

I want to call/ activate a button at the end of a Sub. I know the cell address of the command button, but I don't know the name/ID of the button.
How do I select/activate the button without looping through each button on active sheet?
https://stackoverflow.com/a/30600479/13049793
I have created my buttons on multiple rows with each assigned to the same macro, from my understanding, I cannot call the macro the button is assigned to because the macro uses the button's relative position, below is a simple example to illustrate the use of the buttons relative position:
Sub ExampleButtonClick()
Dim Cellvalue As String
Cellvalue = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(0, -1).Value
Msgbox (Cellvalue)
End Sub
i assume you want to run one macro that at the end initiates a different macro, i also assume the reason the button is rather not as a sub that is just called is because it has its independent function that can be used without the other sub
assuming that you used a command button as an activeX control, why not just use a private sub that the button performs and place that in an individual module and at the end of your current sub;
call module1 'assuming the private sub exists in module 1
or rather include the code even if duplicated in the first sub, searching for a button seems the circular route unless there is another intention you wish to pursue.
maybe elaborate on the task at hand or post the current code you are working on?

Stop macro to allow the workbook to be edited

I have written a code to open a workbook and filter data by the number in a particular column. I need to filter "0" first and then edit the data and then filter between "+8" and "-8".
I added a message box to pause the macro but I am not able to edit the opened workbook while the message box is displayed. I tried with "Application.waiting" option.
I need to pause the macro automatically and start it manually.
Sub Filter_data()
Workbooks.open"D:\Reposrts\AAA.csv"
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:="0"
Activesheet.columns("A:Z").Autofit
MsgBox"Task ok" 'Here I need to pause the macro and allow for edit opened wb and then manually start macro for below line'
Activesheet.Range("I1:I100").Autofilter field:=1,Criterial:=">8", Opersator:=xlAnd, Criterial:="<-8"
End Sub
Split your existing Macro into two parts
The first part contains everything up to have the MsgBox"Task ok" line
The second part contains everything after that point
Create a Modeless (or "Non-Modal") User Form with your label and an "OK" button to call the second part of your split macro
"Modeless" means you can edit the workbook while the User Form is open. MsgBox is "Modal", which is why you can't edit anything. More details available from Microsoft
Replace the MsgBox"Task ok" line in the first part of your split macro with a line that opens your User Form.
Once the first half-macro has finished, and the User Form is waiting for you to click "OK", you will be able to edit workbooks. Once you click "OK", the second part will start.
It is probably best to first consider if the "edits" you need the user to make are possible via VBA, a User Form, or a DialogBox (which include the likes of the "Select Range" DialogBox, or the "Colour Picker" DialogBox)
I would recommend you not to try to edit the Workbook while a macro is running- I don't even think that is possible at all. Furthermore, try not to refer to ranges by activating/selecting ranges.
Activesheet.Range("I1:I100").Autofilter 'Instead of this use the code below
With Workbooks("AAA.csv")
.Sheets("NAME").Range("I1:I100).Autofilter
'More code
End With
Like Chillin mentioned, you could assign hotkeys to your filter macros- split them in two. Other than that, you can use buttons to activate the macros.
To assign a keyboard shortcut to a macro:
Press ALT+F8 to open the macro dialog box. Select the macro, and click on Options. In the window that opens you can assign a keyboard shortcut to the macro you selected.

How to remove or delete comments in excel worksheet?

As I click on the particular cells in excel, comments appears that disturb me much so I want vba code to delete all the comments instantly in the active worksheet.
All you really need to do is get a range, then clear comments:
Worksheets("MySheet").Activate
ActiveSheet.UsedRange.ClearComments
Does that help?
More Detail
To get the above code to work, there are several approaches. The one I recommend here is:
Open your Excel workbook.
Click the Visual Basic option on the Developer tab. This opens a VBA window with a tree control to the left, which shows the worksheets and workbooks.
Right-click the worksheet and select Insert Module.
In the module window that opens, paste the code I show at the bottom of these instructions.
Save the worksheet as type Excel Macro-Enabled Workbook.
Close the VBA window.
When back in Excel, hit to bring up the Run Macro window. You should see your RemoveComments macro listed. Now click Run and your comments should be removed.
I actually tested this, so it will work if done properly. If it still doesn't work for you, be sure that the worksheet in question is the first worksheet in your workbook. If it isn't, then change Worksheets(1).Activate in your RemoveComments Sub so that it refers to the correct worksheet.
Sub RemoveComments()
Worksheets(1).Activate
ActiveSheet.UsedRange.ClearComments
End Sub
Please see my reply
Sub delete_comments()
Dim i As Range
For Each i In ActiveSheet.UsedRange
i.ClearNotes
Next i
End Sub

Excel VBA macro runs after an object has been moved

I know how to assign a macro to a cell and launch when it has changed.
I know how to assign a macro to a 'button' and launch it when clicked.
I know how to assign a macro to a worksheet and launch when any cell has changed.
But now ..... I have a text box in a chart and wish to have a macro launch AFTER the text box has been moved to a new location. Currently, I click on a 'button' after I move the text box and this works well. I would like to eliminate the final step in order to avoid a problem if the user fails to click the button.
If there is no built-in Event associated with a worksheet property or characteristic you wish to monitor, you can always use an Application.OnTime macro to monitor the item.
For example the TopLeftCell.Address of a Shape can be monitored from call to call and action taken if the property changes.
Some some information see OnTime
I think you can found what you want Here.

Excel combobox listfillrange property pointing at a formula-based named range has issues

ActiveX combobox objects in Excel do not behave well when their ListFillRange refers to a formula-based Named Range (Defined Name).
I think I have encountered other errors and possibly even Excel crashes thanks to this, but right now all that happens is the combobox_change() event is triggered anytime ANY cell in the workbook is changed.
I am not sure if this is really a bug, or if there is a fix, or a workaround. If it is a bug, how do I report it to the Excel people?
And finally, the real meat of my question is "How do I work around this issue best?" I would like to have some formula-based named ranges, but it seems like this won't be possible.
To reproduce this bug, do the following:
Create a new workbook. On Sheet3, create a small table 3 columns across, and several rows high.
Create a named range with this formula (or an equivalent): =OFFSET(Sheet3!$A$2:$C$36,0,0,COUNTA(Sheet3!$A:$A),COUNTA(Sheet3!$4:$4)) To do this use Input>Name>Define. Name the range something like "demoRange"
Go to Sheet1 and create a combobox, (it must be on a separate sheet). (Use the Control Toolbox menu, not the Forms menu).
Click on the Design Mode button (the blue triangle with pencil), then right click on the combo box and go to Properties.
In the properties window for the combobox, change the ListFillRange property so that it points at the named range you created in step 2 ("demoRange").
You may want to change the ColumnCount property to 3, and the ColumnWidths property to "50,50,50"
Set the linkedCell property to cell "A1" by typing A1 in the linkedCell property.
Close the properties window, and double click on the combobox to define its change() event.
Put a Debug.Assert(false) or Msgbox("demo") line in the subroutine for the new combobox's change event.
Exit design mode
important - Now select an item in the combobox. The event should trigger normally the first time. (The bug will not show if you don't do this step--something must be selected in the combobox)
Edit cells anywhere in the workbook [Edit] or any other open workbook [/edit], on any sheet and any location. Each time you edit any cell, (at least for me), the onchange event for the combo box is run.
Again, is this normal, and what is the best alternative for what I am doing? This combo box gets linked to various cells, and is supposed to be a replacement for the tiny font in the data validation dropdowns excel provides by default.
My advice is to never use ListFillRange and LinkedCell. They are just trouble. Fill your listbox with List and use the Change event to write to the cell. Somewhere, maybe the Workbook_Open event, fill the listbox
Private Sub Workbook_Open()
Sheet2.ListBox1.Clear
Sheet2.ListBox1.List = Sheet1.Range("demoRange").Value
End Sub
Then in the change event in the Sheet2 module, check that something was clicked and write it to the cell
Private Sub ListBox1_Change()
If Me.ListBox1.ListIndex >= 0 Then
Sheet2.Range("A1").Value = Me.ListBox1.Value
End If
End Sub
I have a few options available that I am aware of thus far. The best I can come up with is this:
Avoid directly using formula-based named ranges. Instead, define a subroutine that will check whether the defined range "demoRange" should be changed from what its current value is. Run this subroutine on the workbook_open and sheet3_deactivate events. If needed, prompt the user to ask if it's all right to update the named range. [edit] The macro that updates "demoRange" could probably just copy from a "demoRange_FormulaBased" named range into "demoRange" which would be static. [/edit]
This solution works well because you can keep using the linkedcell property, you don't have to use VBA to populate the comboboxes, and the named range can still be used for whatever other purposes it already had. Avoid using the onchange event to run this new subroutine, since it might end up being triggered thousands of times if a user opens the Find/Replace dialog and chooses "Replace All".

Resources