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).
Related
Macros on my spreadsheet that have been working for years, stopped working via buttons, though they still work from the VBA Developer window.
I use a normal shape (rectangle) as the button and assigned the macro to it (selecting from "ThisWorkbook").
To head off suggested fixes I've seen for other similar posts:
It is still an .xlsm file
Macros are still enabled.
It's running on the same laptop as before (Windows 10, Office 365).
No Windows updates have occurred lately.
I only have this one file open.
I tried rebooting laptop and restarting Excel.
I'm not using an Active-X Control.
I tried it with a Form Control button and a regular Shape button - neither work.
It's not related to the actual VBA code (see below for proof).
I created a new program to show the problem is not the code itself:
Sub button_not_working()
MsgBox "button_not_working"
End Sub
This program works using the green Play button in the VBA screen, but not via an assigned button on a sheet.
Code is in a normal VBA code Module (not "ThisWorkbook" area on VBA screen).
Macro is assigned by right-clicking shape, and the list of available Macros is just those in "This Workbook" on the Assign Macro popup.
When clicking the Shape to run the assigned macro I get this error message.
You can't call code from ThisWorkbook in a button event.
I like to put the button events in the code behind the sheet where the button lives. This way the code move with the sheet wherever that sheet is copied.
Use a form button. Right-click on the form button and select Assign Macro.... Then select the VBA subroutine from the list that pops up. Only procedures visible on this popup will work.
NOTE:
Never use _ in any names in VBA. It's reserved for event handling.
Public Sub ButtonIsWorking()
MsgBox "button is working"
End Sub
Code in sheet:
Assign Macro:
Button press:
Often this issue can occur due to multiple screens or resolution difference issues. If you are using a laptop connected to screens try using the button on the laptop while it's disconnected from your monitors. If this fixes the issue then ensure you have the same resolution and/or scaling between the two setups.
If this change only happened recently and is on a desktop or laptop screen without additional monitors ensure your scaling is set to 100% in case it has recently changed.
Solved - turned out I needed to check the box for "Enable Excel 4.0 Macros".
Apparently, Microsoft has just started disabling this by default in Office 365 starting this month!
So in Excel... File>>Options>>Trust Center>>Trust Center Settings>>Macro settings
Something to try: Make sure your 'caller' shapes have unique names
Note: It's possible to have multiple shapes with the same name and (for whatever reason) that can confuse excel's shape-to-macro-connection.
Additional Info 1:
If you use a 'grouped shapes' object as a control, you should assign the same name to all shapes in the group.
Additional Info 2:
The original OP symptom was simply "Macros ... stopped working via buttons." The OP has since been updated, and shows an error message. The disconnect-due-to-non-unique-naming described here doesn't elicit an error message. Rather the symptoms can be either of: a) the macro isn't run at all or b) the 'caller' object in the called macro is invalid.
I run a lot of macros, and lately had to change to O365, which gave me the "wonderful" 64 bit Excel. One of its most maddening shenanigans is that when I run some macros (working with SAP GUI, creating SAP reports, naming them, saving and downloading to a specific folder where the next macro step opens them and incorporates into a relevant macro sheet), the new Excel opens these in a new instance (which I don't want but cannot prevent - can you help here?), which causes a dialog "File in Use" (which I don't want but cannot prevent - can anyone help me here?) because it tries to open my PERSONAL.XLSB (which I need and therefore don't want to get rid of just because of this) and qualifies it as "locked for editing" by another user, who is myself (which is absolutely stupid but I cannot change it - can anyone help me perhaps with this part?).
Sometimes the macro finishes fine when I don't answer that dialog; sometimes it seems to cause the Excel to hang up in trying to do next steps, sometimes the macro quits on me when I click "Read only" and so on.
All this is maddening also because these files that (possibly) SAP GUI opens, I have to waste my time by closing them after the macro finishes (has anyone a possible solution in stopping SAP opening these files? I was unable to find it anywhere so far.).
In some macros I fixed it by a specific part of code which waits till when these files open up and then it closes them - yet even that wait is wasting my time and it would be better without.
But with O365 and other instances it is even more colorful, because some files open in the same instance (this issue was there even before the O365 though) and I can manually close them after the macro finishes (although again, I have to wait till they open, which is again "vanity and torture of the soul"), but those opening in the new instance (specific to O365) show as blank sheets, I have to click into them, a warning sound follows but no dialog is visible, then I have to click into them again from the Excel icon in the bottom menu bar and then it tells me that I cannot close them because a dialog is open.
Then I need to close this dialog plus the one which appeared about the "File in Use", then I finally wait for the file (that I don't need) to open (because it is already saved inside the folder and already copied and pasted onto the relevant macro sheet) and only then I can close it.
So far I was unable to find anywhere on the internet an advice for how to stop SAP opening these files (when they are already saved where I need them), neither how to stop them opening in new Excel instances. So reluctantly, I think I should aim for at least a tiny little VBA code which will cause my macro to click on the "Read Only" button. Can anyone here help me with any part of this "Excel complaint"?
I do not know how/if SAP can be 'persuaded' to not open the workbooks in discussion... But, I think, you can handle that in a different way the "PERSONAL.XLSB" behavior, from the opening point of view. Of course, if SAP itself has this "bad habit" and not your code opens it in a new session, which have to be solved in a different way...
"PERSONAL.XLSB" is located in "...AppData\Roaming\Microsoft\Excel\XLSTART" folder. All workbooks located in this folder are automatically open when an Excel session starts;
You may change the "PERSONAL.XLSB" workbook path. If you want it to be open when a specific (necessary) workbook needs it, add a reference to it.
addRef. In order to do that, modify the (standard) VBAProject (of "PERSONAL.XLSB"), in, let us say, "VBPersProj", then (being in VBE) trough 'Tools - References...` tick its check box and press 'OK';
If the exported workbook is open in a new session and, even if it does not bother you any more because of the annoying dialogs and you need to identify that specific session, you can use the next function:
Function sameExSession(wbFullName As String, Optional boolClose As Boolean) As Boolean
Dim sessEx As Excel.Application, wb As Workbook
Set sessEx = GetObject(wbFullName).Application
If sessEx.hWnd = Application.hWnd Then
sameExSession = True
Else
sameExSession = False
If boolClose Then
sessEx.Workbooks(Right(wbFullName, Len(wbFullName) - InStrRev(wbFullName, "\"))).Close False
sessEx.Quit: Set sessEx = Nothing
End If
End If
End Function
It offers the possibility to close the workbook and the session itself if it is a different against the one where the code runs. It is an Optional parameter to be used, only if you need that. You can test it in the next way:
Sub testSameExSession()
Dim wbFullName As String
wbFullName = ThisWorkbook.fullName 'use here the full name of the SAP exported workbook
If sameExSession(wbFullName, True) Then
'process the workbook if this is what you need
End If
'if not open in the active session, the workbook could be closed and session quit, choosing the above way
'You can also simply call the function, without the closing parameter, as:
Debug.Print sameExSession(wbFullName)
End Sub
In case of False function retur, it can be closed and reopen in the existing session, if needed...
Edited:
If you need personal.xlsb being used/open when ribbon controls calls some of its Subs/functions, please adapt the code to call it in the next way:
Dim x as Long
x = Run("'C:\Users\your_UserName\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB'!GiveMeFive", 4, 3)
if calling a function (with parameters), which must return. Or
Run "'C:\Users\your_UserName\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB'!YourSubName"
when calling a Sub. In such a way, "Personal.xlsb" is opened if it is closed. And, in your specific case nothing will press the ribbon controls.
But, isn't the ribbon added when a specific workbook is open? Probably, the one you are using to make the above mentioned calls... If so, adding the suggested reference at "Personal.xlsb", will configure the new ribbon tab only when it is loaded. And your mentioned "problem" does not exist, in fact. Is it an add-in?
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?
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.
I have an Excel workbook that I want to open in Full screen mode.
This workbook should open with a password lock(Which I will implement) upon double-clicking the file.
Is there a way I can open the Excel workbook in something like 'Kiosk' mode
I don't want any toolbars,Ribon interfaces,Statusbars,Cell select indicators(see below) ...
I am also looking towards locking ALL cells,so they cant be edited
Security wise,is there a way to truly protect a document.
For instance,I am looking at securing this file so that it cant be opened in any other computer,Something like a unique hash.
Looking forward to hear your ideas.
Thanks,
I have figured out 80 % of my requirement from the macro code below
Private Sub Workbook_Deactivate()
On Error Resume Next
With Application
.DisplayFullScreen = False
.CommandBars("Worksheet Menu Bar").Enabled = True
End With
End Sub
In addition to this,I have Hid the Gridlines,Heading and Formula Bar.
What I only need to do is hide the status and title bar(where it shows title.xlx)
How can I hide these both.
Can you write a macro that performs these 2 tasks and merge it with the existing macro seen above
thanks,