Stop macro to allow the workbook to be edited - excel

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.

Related

Userform needed only once

I'm a new VBA user. I have a workbook with multiple worksheets. Each one has it own userform for data entry. The userform shows when I click on the sheet. After I'm done entering data, the worksheet is populated and the userform closes (unload). All this works well. However, after the initial data entry is complete, the goal is to use the data on the worksheets for other applications and the userform is no longer needed. What is the code or the terminology to say the userform should not reappear again when the worksheet is clicked on? Currently, I red X out of the userform. If I click the command button to close, it repopulates and I lose all my data.
Thanks!
As A.S.H commented; you could store the information in a number of ways. An easy example is declaring a variable outside of the Macro:
Public FormOpened as boolean
And then set FormOpened as true once the form has been shown. Then you could add a check to the start of the mouse-click macro:
If FormOpened = True then Exit sub

Link Command Button to VBA in another Workbook

Have an Excel file for different days of the year. also have a master excel file. Would like to create a link button from the day excel files to the VBA in the Master file. So if need to change some code, I can change it in the master file and the day files will run the new code from the master file.
Assuming that, in an open workbook called "Master.xlsm", you have a macro called test:
Sub test()
MsgBox "in master"
End Sub
and that you have a forms control button on a sheet in another open workbook (perhaps "Daily.xlsx"), then you can assign the macro to that button as:
If you subsequently open just "Daily.xlsx" without first opening "Master.xlsm", you will get the
message (due to the link to "Master.xlsm"). Irrespective of whether you say "Update" or "Don't Update", "Master.xlsm" will be automatically opened when you click the button, and the test macro will be executed.

Running a macro automatically

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.

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 2007 vba Application.Dialogs(xlDialogPrint).Show crashes if user chooses print preview

In Excel 2007, have a button that triggers a macro, which selects a few sheets out of many and sends to Application.Dialogs(xlDialogPrint).Show. As part of selecting the few sheets there are other macros triggered like showing certain rows, password protecting and unprotect-ing, etc.
It prints and cancels fine, except when users click the 'print preview' button in that printer dialog box. It shows the printer dialog fine, but no matter if they hit print or close it crashes.
It appears to run through the whole macro a second time and crashes because expected values and settings are not in place like normal when it runs through the first time.
Any way to account or or capture the print preview dialog stuff when print preview is launched via the printer dialog from Application.Dialogs(xlDialogPrint).Show?
I have tried changing passed parameters like
Application.Dialogs(xlDialogPrint).Show ,,,,,False
Application.Dialogs(xlDialogPrint).Show Arg6:=False
But these are not working; I've read that you can't alter the dialog anyway.
(Hope this is clear)
Not sure if you want to show print preview directly from the button click or not. Usually I use something like this. I find it easier to view the preview first, then decide if I want to print out a hard copy. But it might not work for your situation.
Private Sub CommandButton1_Click()
Dim vSheets() As Variant
vSheets = Array("Sheet1", "Sheet2")
ActiveWorkbook.Sheets(vSheets).Select 'sheets need to be selected
ActiveWorkbook.PrintOut preview:=True 'brings up print preview
End Sub

Resources