Press OK on pop-up alert window via VBA - excel

Having an issue, I have written a Macro ,that refreshes every 2 min, to upload data from a webpage and the last step of my Macro is using "Text to column" function so that it will be splitted into columns nicely.
The problem appears here everytime it gets to executes this "Text to column" it asks if I want to overwrite the columns(and I DO), but I need to press manually OK.
Is there any way to make VBA press this OK button in the alert?
Thank you very much!

Just add Application.DisplayAlerts = False prior to the call, and set it back to True after.
http://msdn.microsoft.com/en-us/library/office/aa175241%28v=office.11%29.aspx
Update: I just ran a test and it does not prompt me by default. Can you post the code you're using? I simply did:
Public Sub Test()
Dim r As Range
Set r = ActiveWorkbook.Sheets(1).Columns("A:A")
r.TextToColumns Destination:=Range("B1")
End Sub
This link says the DisplayAlerts solution has worked in for others.

You can add a DoCmd.SendKeys BEFORE the instruction that generates the dialog.

You should use mouse click function
Before it maximize window so x,y cordinates of ok buttons remain same
Use windows lib 32

Related

Command Button doesn't work when first clicked

All the programming I have learnt is through manuals and websites such as this. So I wouldn't call myself a programmer but I do use a lot of quick and dirty macros to make my life easier. Occasionally, it all falls down and assistance is required!
I have a workbook that summarises the jobs I have worked on. In order to work quicker I have added a few command buttons too speed up simple tasks like filtering the blanks in a specific column. Normally this works OK. It is three weeks since I last used the workbook and today it is misbehaving. Click on a command button and the ribbon turns mostly grey. Click on the button again and the code runs and the ribbon returns to normal.
I have put a breakpoint in the code. On first click the breakpoint is not reached. On second click the breakpoint is reached!
Interestingly, if I click the command button the first time and then go into the vba editor and click on the break button the ribbon returns to normal again. So something is happening but it doesn't get as far as CommandButtonIssd_Click.
The command button code looks like this:
Private Sub CommandButtonIssd_Click()
FilterIt 9
End Sub
Most of the other command buttons just act on a different column number.
In the main module the code is:
Sub FilterIt(FieldNo As Integer)
'
' Filter Macro
'
FilterOff
ActiveSheet.Range("A4").AutoFilter
ActiveSheet.Range("A4").AutoFilter Field:=FieldNo, Criteria1:="="
End Sub
Sub FilterOff()
Dim a As Range, b As Range, iRow As Integer, iCol As Integer
If ActiveSheet.AutoFilterMode Then 'on, turn it off
Set a = Selection 'keep current location
iRow = a.Row
iCol = a.Column
Selection.AutoFilter
Range(Cells(iRow, iCol), Cells(iRow, iCol)).Select 'reset current location
End If
End Sub
All this does is filter column 9 to show only the blank cells.
I am thinking that it's a Windows10, Office365 issue as the code has been stable for a while and it was working OK last time I used it. In which case, it's just a case of waiting for MS to sort it's bugs out and issue an update (every Thursday, regular as clockwork!).
OR my code is suspect and needs tweaking to stop it messing with the system.
You thoughts would be appreciated.
I had this same problem today with an Excel 365 workbook: All CommandButtons would not fire on first click. They would fire on subsequent clicks, however.
today it is misbehaving. Click on a command button and the ribbon turns mostly grey. Click on the button again and the code runs and the ribbon returns to normal.
In my case, the bad behavior disappeared when I closed and reopened Excel and then reopened the workbook. After that, CommandButtons fired upon first click. I concur with #MikeK that it appears to be an Excel bug.
Finally!
Same problem, different workbook.
New laptop, Windows 11, Office 365 and I think I have a better definition of the problem as a little more information is now available.
If I open the spreadsheet on the laptop screen the macro and command buttons on the spreadsheet all work fine.
If set up the monitor screen as a duplicate of the laptop screen it all works OK.
If I set up the monitor screen as an extended display, then move the spreadsheet to the monitor screen and try again it fails as follows:
Open the macro sheet and it loads OK on the monitor screen but the form with the menu on it is displayed on the laptop screen!?
Exit the macro and it takes me to the macro spreadsheet and three command buttons as it should do.
Click on "Open Contact List" and the spreadsheet content is replaced by plain black (or the contents of the underlying window!). Click again without moving the mouse and the (now invisible) button works and loads the contact list.
close the contact list and the excel window continues to display incorrectly as above.
click inside the excel window and all is restored.
same applies to the other two buttons.
Shouldn't all this be handled by the windows system?
Previously I could deal with having to click a button twice to make it work. Now I have to click an invisible button!
If it is a Windows bug, how do I report it?

VBA - Run excel's built-in Screenshot tool (not ScreenClipping) using VBA

Does anyone here have any idea how to row Excel's built-in tool (see screenshot below) and automatically select specified window to capture using VBA Script?
What the code below does is essentially press the shortcut keys: Alt+N+SC+C, where Alt=%, N=Insert Ribbon, SC=Screenshot Button, C= Capture Screen Button, you may have to modify depending on language/office version/shortcut setup. I got it working by running the code from a form control button on the worksheet, running from VBA-editor did not work in this case.
Sub Screenshot()
SendKeys ("%NSCC"), True
End Sub
Note: You should only use the SendKeys Method if no other option is
available, because it can cause problems, if the wrong window is
active when the code runs.
_
Edit: As you have requested to pick one of the windows in the
screenshot gallery, i recommend you find the the SendKeys equivalent
of TAB and ENTER. Not the best solution i admit, but better than
nothing.
_
Edit2: See this brilliant answer:

Excel freez pane and invisible gridlines settings are not saved

I have an excel file in which I want to freez the view to cell D5 and I want the grid lines to be invisible. Getting this done works fine, however, these settings get lost sporadically after saving and re-opening the file. The file is on a server and four people work on it. I trust them when they tell me that they did not change it back. Could some local setting do this automatically?
If you cannot save it, maybe you can run some code to set your settings every time you open the file.
The following code will show you how to Run a Macro Automatically.
Private Sub Workbook_Open()
ActiveWindow.DisplayGridlines = False
MsgBox "Hi dude! your settings were applied"
End Sub
That will eliminate the grid lines everytime you open the workbook.
I don't know what you mean by freezing D5 cell, but can use the macro recorder to get the code of what you record to then use it in your procedures.
You should have your button "record macro" (mine is in spanish "Grabar macro")
For the code to execute when the workbook opens you should name the procedureas told (Private Sub Workbook_Open()) and paste it in the "ThisWorkbook" module.
Hope it helps
It seems that the view seetings are saved with the window and new windows are initialized with grid and without freez pane. If the file is then saved with the new window open, all settings are lost (i.e. set to default).

Edit excel sheet manually while running a code

I would like to know if i can fill in a row of values manually during the run of a code when a msgbox is displayed by the code asking me to fill the same. I tried filling them but after i press 'ok' on the message box, the code continues to run and also, i cant edit the file without pressing ok on the message box. So, could anyone tell me if there is a way I can do that? Thanks in advance!
You could try to use a userform instead of an messagebox. As far as i know there is no simple way to create a messagebox, where you can edit the sheet in background.
If i need to edit the sheet while having an active macro, i'm using a userform. (they are not that hard insert in vba, try it!)
You have to start the userform with the vbmodeless.
Should look like this: (i'm not 100% sure, because i've no vba ide herere to test, but i thinks it's something like this)
Sub ShowMyCreatedUserform()
myUserform.Show vbModeless
End Sub
If this userform is open, your code stops, but you can edit the sheets. After pressing a button (for example "ok"-button) you can continue the macro.
I found 'ontime' command on the net.. I think the code can be split into 2 parts and ontime function can be used..

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