Excel 2013 crashes when unhiding columns - vba

I've spent hours trying to figure this out with no solution being found so far. I have a macro (see below) that unhides/hides a range on a spreadsheet. Seems simple enough and the first two times I run this macro it works fine; however, the third time, which is always unhiding the range, usually causes Excel 2013 to crash. I read here that having an volatile user defined function in the range I'm unhiding might be causing the problem, but I already tried making the function not volatile and Excel still crashed. Does anyone have any ideas or experienced this before?
Sub HideShowSQLCreator(control As IRibbonControl)
Dim SQL_Creator As Range
Dim Report_Home_Cell As Range
Set SQL_Creator = Sheets("Report").Range("SQL_Creator")
Set Report_Home_Cell = Sheets("Report").Range("Report_Home_Cell")
If SQL_Creator.EntireColumn.Hidden = False Then
SQL_Creator.EntireColumn.Hidden = True
Report_Home_Cell.EntireRow.Select
ActiveWindow.FreezePanes = True
ActiveSheet.Range("Report_Home_Cell").Select
Exit Sub
End If
If SQL_Creator.EntireColumn.Hidden = True Then
SQL_Creator.EntireColumn.Hidden = False
ActiveWindow.FreezePanes = False
SQL_Creator.Select
Exit Sub
End If
End Sub

So, I got frustrated and figured the workbook was just corrupted so I created a new one from scratch. The macro was working fine until I got to the end and realized that the crash had to do with a big pivot table to the right of the range I was trying to hide/unhide. I'm not sure why it happened, but my solution was to just put the columns I was trying to hide/unhide in a separate sheet in the workbook. The macro works fine with them in separate sheets. Weird stuff, but I'm glad there was a solution in the end.

Related

Application.ScreenUpdating = False not working switching between Excel sheets or workbooks

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

Sheet inaccessible to macro: Error 1004: Application/Object Defined Error

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.

Closing a userform that is in workbook A from workbook B

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.

Excel Type MisMatch with VBA to hide certain rows

I realize there are already a number of great responses for dealing with VBA TypeMismatch errors in Excel, but they all seem quite case-specific and I'm admittedly too much of a VBA n00b to follow them.
So, I wondered if you might be able to help me with this specific predicament:
I have made up a workbook and I want rows to automatically hide themselves if the value in column C is less than today's date. *I assigned the following Macro to an ActiveX Command Button:*
Sub Hide_PastOrders()
Dim MyRange As Range, C As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set MyRange = Range("d1:d1000")
MyRange.EntireRow.Hidden = False
For Each C In MyRange
If IsDate(C.Value) And C.Value < Date Then
C.EntireRow.Hidden = True
End If
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
It was working fine until recently, but now sometimes results in a 'Type MisMatch' and I'm not sure why. It only seems to be an issue on a couple of the worksheets and they are all assigned the same macro.
I have also implemented an ActiveX command button to 'Show all rows':
Sub ShowAll_Click()
ActiveSheet.Cells.EntireRow.Hidden = False
End Sub
Haven't had any issues with this one.
Also, I'm not sure that this is relevant, but I have created a number of internal references in my document. i.e. typed "=A5" in B5 for example so that if there are changes to our rentals for multiple pieces of equipment, I would only have to type the information in once. This proves to be very frustrating because everytime I encounter a 'TypeMismatch' Error, it reverts certain cells to "=REF".
Please let me know if I'm taking the wrong approach or if you have any suggestions!
Thanks,
Alexandra.
The expression in the if statement is not short-circuited in VBA. Try this:
If IsDate(C.Value) Then
If C.Value < Date Then
C.EntireRow.Hidden = True
End If
End If
See http://en.m.wikipedia.org/wiki/Short-circuit_evaluation

Removing Charts in Excel

I was trying to chart some RTD data and accidentally ended up with hundreds of charts on the same worksheet. Now I'm trying to undo my error and remove all of the charts but I'm not having much luck with it.
I recorded a macro where I deleted one of the charts manually and then tried editing the code to loop through all of the charts but I keep getting an error. My code is below:
Sub Macro3()
Dim i As Integer
For i = 1 To 100
Sheets("Calculations").Select
ActiveSheet.ChartObjects("Chart " & CStr(i)).Activate
ActiveChart.ChartArea.Select
ActiveWindow.Visible = False
Selection.Delete
Next i
End Sub
When I try running this, I get an error saying that the ChartObjects property was inaccessible from the Worksheet class.
I'm sure there's a simple explanation/solution to this but I've learned that VBA sometimes does things a little differently than you might be expecting. So, I guess my question is, how do I remove the charts without having to go through each one at a time?
Any help would be appreciated. Thanks.
Try this, it will get rid of all charts on your sheet regardless of their names.
Sub Macro3()
Worksheets("Calculations").ChartObjects.Delete
End Sub
It's quite possible you are attempting to access a chart by name which no longer exists. Try accessing the charts by index using ChartObjects(i) instead.