I moved a pagebreak in an Excel sheet by hand and recorded the following macro in the process because I want to automate it:
Sub RecordedMacro()
'
' RecordedMacro Macro
'
Set ActiveSheet.HPageBreaks(1).Location = Range("A71")
End Sub
Running this recorded macro fails with an error 1004 ("application-defined or object-defined error"), even with an active sheet where I can do it manually without any problems.
I did some Internet searching and it appears that most people end up using HPageBreaks.Add instead of changing a pagebreak's location, but I'd like to know why this macro fails and if there is a way to make it work as I see no reason for throwing an error.
EDIT: The following line fails with an 1004, too:
Set ActiveSheet.HPageBreaks(1).Location = ActiveSheet.HPageBreaks(1).Location
My answer is slightly different than what you discovered, and doesn't require the ResetAllPageBreaks. In addition this one toggles ScreenUpdating to avoid the flicker of going back and forth:
Sub MoveThatHBreak()
Application.ScreenUpdating = False
ActiveWindow.View = xlPageBreakPreview
Set ActiveSheet.HPageBreaks(1).Location = Range("A11")
ActiveWindow.View = xlNormalView
Application.ScreenUpdating = True
End Sub
This is in Excel 2016. I stumbled on this answer because I could only record the move while in Page Break Preview, so figured maybe the same was true in VBA.
I found the solution. I tried to think what the difference between manually setting the pagebreak and setting it via VBA is in my case, and for reasons I do not fully understand I really have to switch the view first:
Sheets(sheetName).Activate
ActiveWindow.View = xlPageBreakPreview
Sheets(sheetName).ResetAllPageBreaks
Set Sheets(sheetName).HPageBreaks(1).Location = ActiveSheet.Range("A71")
ActiveWindow.View = xlNormalView
This makes not that much sense to me as all other operations on the HPageBreaks collection work without changing the view first, but if Excel requires it, I'll do it :).
Related
The function Application.ScreenUpdating = False is not working whenever switching between worksheets or workbooks in Excel. This function alone worked fine in Excel 2010, but doesn't work in later versions from what I can tell. I am now using the office 365 desktop version of excel. In these later versions, the command only prevents updating when selecting cells or doing things within a specific worksheet, but for my purposes I need a form to pull data from a second worksheet which causes flickering.
Is there a way to prevent the screen from updating/flickering with SheetB briefly when it gets activated in this macro?
Sub ActivateSheetB()
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sheets("SheetB").Activate
End Sub
I've had the same thing happen to me forever and it's fairly annoying but from my own observation, I believe that application.screenupdating = false is still working. What I mean by that is your code is still being sped up. Other than it visually being annoying and making users think they broke excel this is still an effective method for speeding up your workbook even when switching between sheets.
Hopefully someone comes along with a better answer than mine because I'd love to know what that answer is as well xD
This is happening in several of my macros, but this is the one in from of me:
Private Sub resettool()
'''resets step 2 input and user input on MPP tabs
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Call showsheets 'makes all of these sheets .Visible = True
'clear data from lookups and data corrals
Sheets("Media by Copy Lookup").Range("b1",Range("b1").End(xlToRight).End(xlDown)).ClearContents
Sheets("Total Media Lookup").Range("d1",Range("d1").End(xlToRight).End(xlDown)).ClearContents
Sheets("Total Media Lookup").Range("b2:c100").ClearContents
Sheets("Media by Copy Data").Range("a1",Range("a1").End(xlToRight).End(xlDown)).ClearContents
'etc etc
End Sub
It continues with similar data-clearing lines for a while. This started happening when I took someone else's code and cleaned it by removing the .Select usages as people on here have suggested. It seems that the macro isn't able to access the sheets I'm referencing, because a line runs successfully if I step into the code, manually select the referenced sheet, and then hit go (but then of course I get the same error when I try to edit another sheet).
Any ideas why the macro wouldn't be able to access these sheets unless I explicitly activate/select them? The sheets are all visible, so that shouldn't be the problem.
P.S. I've seen the guide on using .Rows.Count).End(xlUp) instead of End(xlDown) to find the bottom of my data and will implement that soon, but this issue is occurring no matter how I define the range; it's about the sheet.
I've got this code in Excel using the macro recorder, which unticks the 'Locked' property of a slicer:
ActiveSheet.Shapes.Range(Array("WeekEndingPick")).Select
Selection.Locked = msoFalse
I don't like using Select \ Selection in VBA (except when I specifically want to visibly select a range) for a number of reasons*, it seems poor practice. Usually I can just rewrite things like this easily:
'Macro-recorded code
Range("A1").Select
Selection.Font.Bold = True
'Equivilant without select / selection:
Range("A1").Font.Bold = True
However, the following doesn't work as Locked is not a property of Range, it's a property of Shape:
ActiveSheet.Shapes.Range(Array("WeekEndingPick")).Locked = msoFalse
How can I work around this?
*If the required worksheet isn't active, it fails. If Application.ScreenUpdating = False, it fails. If the selection changes unexpectedly, all manner of nonsense can occur - not necessary failing, but running code against the wrong location.
Figured it almost as soon as I went back to fiddle around in Excel. It's a property of Slicer:
ThisWorkbook.SlicerCaches("Slicer_WeekEndingPick").Slicers("WeekEndingPick").Locked = msoFalse
Simple!
I'm new to VBA so there might be a simple answer to this question but if there is I sure haven't found it. What I am doing is copying data from several workbooks into one master workbook. I have writen the code for this and it works fine. The only problem is the workbooks where I'm retriving the data have userforms that automatically initiate when the workbook is accesed. This means that when I run my code to copy the data it hangs at each userform and wont continue until I've physically closed each userform. So my question is: Is there a way to remotely close the userforms in the raw data workbooks from my master workbook VBA code? Thanks in advance.
to close all userforms, (if you want a specific one , change my code)
sub Close_Userforms()
Dim Form as VBA.Userform 'if not work change to Object
For each Form in VBA.Userform
'can add a condition, like : if Form.name ="Whatever" then
unload Form 'if you don't want to lose the data from the userforms, Form.Hide, and later re-loop and Form.Show
next Form
edit : can also if Typename (Form)="Whatever" then , for the condition
Assuming you mean that the forms pop up when you open the workbooks, disable events before doing so:
Application.Enableevents = False
Workbooks.Open ...
Application.Enableevents = True
for example.
I would suggest trying
Application.EnableEvents = False
Further reading.
Short description: All events (Workbook_Open, Workbook_BeforeSave etc), that usually fires upon opening or closing a workbook, will be ignored.
I have written the following functions to make all macros a bit simpler (and faster). Simply place these functions in a regular module.
Public Function CalcOff()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False
End Function
Public Function CalcOn()
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Function
Begin your macro with:
CalcOff
And then end your macro with:
CalcOn
Remember that you need to put "CalcOn" in all places that exits the running macro.
Disabling ScreenUpdating makes the code "run in background" (nothing will be displayed).
Setting Calculation to manual improves the speed of the code, since no calculations will be made when changing data. But it's very important to exit all macros with "CalcOn", otherwise your sheet won't calculate (and that's not funny), and it will look like Excel has frozen (since ScreenUpdating would still be turned off).
However, if you by any chance happen to break a running code without exiting it the proper way (running "CalcOn"), simply close the Excel application and reopen it. Or run a macro that ends with the "CalcOn" code. Or create a new macro with that simple line.
Okay, so I've never had to do anything in VBA where I was REQUIRED to activate a sheet or select a cell. But, now, I'm trying to figure out how to do Zoom to 100% on a bunch of worksheets, and all the code I see (google results, including answers from this website) seems to select a sheet first:
ActiveWindow.Zoom = 100
But, I did find some code on OzGrid that seems to imply it's possible to do it without selecting a sheet first:
Sht.PageSetup.Zoom = 100
(although above we have Set Sht = ActiveSheet) I tried doing
Set Sht = ThisWorkbook.Worksheets("Sheet1")
Sht.PageSetup.Zoom = 150
but nothing happens... literally nothing.
So, is this possible? Or must I activate a worksheet before I can do the zooming? I've read so many times that this is bad programming practice, unless you absolutely have to.
Yes, I believe zooming is something that only has an effect on an active sheet.
However, if you didn't want to 'see' each sheet getting activated and zoomed as it happens, you could add the line
Application.ScreenUpdating = False
before your zoom code and then after it is done:
Application.ScreenUpdating = True
Setting Application.Screenupdating = False will not solve your problem. If you select a sheet or activate a sheet Application.screenupdating will be set to true.