Before Close Code Errors When Closing From Other Workbooks
Nov 21, 2006
I have a workbook which includes a simple set of options on closing such as selecting the front sheet, restoring scrollbars and saving the workbook. To avoid problems with subscripts out of range I am using the ThisWorkbook statement to close the workbook.
This works fine and causes the workbook to close when close is clicked on any excel window. The problem is that excel falls over when it tries to resume closing the other workbooks. I am given (ironically) an error saying "excel has encountered a problem and needs to close". Does anyone know how to work around this?
My code is below:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
.DisplayHorizontalScrollBar = True
.DisplayWorkbookTabs = True
The background is there are about 40 people who use a Excel based program that contains a pivot and a bunch of other tools that they use on a regular basis, this set of tools has a version number. In this excel workbook, it has a function that looks on the network drive that we have and checks the local version vs the version on our network drive, if its wrong, then the user gets a popup stating your tools are out of date, would you like to update, then they click yes and I have these lines of code
Public Function GetNewTools() Dim MyFullName As String
'Turn off alerts Application.DisplayAlerts = False
'Open the new version of tools Workbooks.Open Filename:= _ "Network DriveUpdate.xlsm"
The update file has this code that executes on fileopen in the thisworkbook section by calling the following sub
The main issue we're running into is near the very end, the code never makes it to "TEST 2". After the first workbooks close, the code just stops running. No crashes, errors, freezes, anything. It just stops running and never makes it to the second msg box.
I am hoping someone can help. I am new to writing macros and have the following in the this workbook page. When opening the macro does exactly what I want and expect, but does not do the "before close" part. If I select the macro in editor and run it, it does just what I want, so dont understand what is going wrong. Do I have to put in some kind of prompt, or manually run this before close - preferably I would like it to just run.
secondly, I have password protected the workbook, but have to put the password in the macro, is there any way of making this invisible to anyone who then reads the macro....
1. I am opening several Excel Workbooks and copy ( consolidate) this one into one worksheet in a separate workbook.
2. I want to close those workbooks automatically that I opened once it was copied and pasted to the Target worksheet.
3. The workbook that should remain opened should be the target workbook.
4. The codes below allowed me to open multiple workbooks at the same time and copy them to target workbook but I do not know how to close them automatically.
Sub OpenFilesOnNetworkDrive() Dim i As Integer Dim Filt Dim FiltIndex Dim Title Title = "Select files: Hold Ctrl to select multiple files." Filt = "Excel Files (*.xls),*.xls," & "Text Files (*.txt),*.txt," & "All Files (*.*),*.*" FiltIndex = 1
Basically the main workbook opens 2 files at a time performs a calculate in the main workbook and then copies and pastes information in 3 ranges. Then closes the two open workbooks and loops and performs same operations until it hits the maximum loops. My macro is as follows and I have 3 question in capital letters.
How can I close the currentmonthend AND previousmonthend workbooks in the following code, but leave workbook "MoEnd Compare" open? I'd like to do some additional comparisons with the "MoEnd Compare" file. I tried the code below to close those two workbooks, but it is not working.
Sub check_month_end() Dim currentmonthend Dim previousmonthend Dim project_current Dim ptcurrent Dim yearcurrent Dim project_previous Dim ptprevious Dim yearprevious Dim r As Integer Dim c As Integer Dim l As Integer MsgBox "This will check project type and year on current and last period month end report." currentmonthend = Application. GetOpenFilename("Excel Files (*.xls),*.xls", , "Select CURRENT Month End Report?") previousmonthend = Application.GetOpenFilename("Excel Files (*.xls),*.xls", , "Select PREVIOUS Month End Report?").......................
I made an Excel based program that uses multiple workbooks with two main workbooks (“Master List” and “Products”) that all the others pull information from. I run into a problem when a user clicks the close X in the upper right hand corner and the entire application closes. I found some code that will let me close all the workbooks but one (the code is put in the “Products” workbook in the BeforeClose event).
For Each wb In Workbooks If Not wb Is ThisWorkbook Then wb.Close SaveChanges:=True End If Next wb
Is there a way to modify the code so it will close all the workbooks but the two main ones if someone clicks the close X button in the upper right hand corner?
I'm working on developing a stock option trading system in Excel using a live data feed and VBA. It tends to crash after running flawlessly for a half hour to 2 hours. It seems to crash when loading a support workbook, so I was hoping you could take a peek at my code and see if there's a problem with the way I'm dynamically opening/closing workbooks.
In order to cope with limitations in the amount of live data that I'm accessing (through DDE with Reuters Station), I've split up the data I need into about 600 workbooks, which open when they're needed or close when they're not (through VBA). At any given time, about 35-40 of these workbooks are open.
The main workbook that uses all this live data and support workbooks is set to have the application recalculate every 5 seconds (automatic recalc crashes the program almost immediately). After that refresh, it calls other subroutines, including opening/closing these support workbooks:
I have an existing code that runs almost flawlessly but I am running in to formatting cells error (or at least I think that is the problem).
For example, here is a line of code where my error lies:
If Worksheets(FirstSheet).Cells(Counter, ColtoMove) = MoveIf Then 'DO SOME ACTION
If I put my cursor on MoveIf it shows the expect number of 4500016239…….BUT it errors!
When I step through the code and go to the worksheet (or go to the left side of the = in the above code the data in the cell is “4.5E+09“.
I’ve attempted to change the formatting of this cell range to “Text” by the following With statement:
With Worksheets("CreateOrderFormsData") .Range("AC:AC").NumberFormat = "Text" End With
Note: if I verify the cells formatting after the above code is ran by rt click on the cell, format cells the Number field is set to “CUSTOM” and the Type: field is set to “TEXT”. Is this the problem? Please read on.
But that did not seem to do the trick…..actually if I now go to the left of the = again (which is now set to text) the data is “###########” in the cell, in the formula bar the number is the expected 4500016239 though but the CODE STILL FAILS!
Am I on the right track…..can anyone assist with this error please?
Sub CombineWBs() Dim wb As Workbook Dim newWB As Workbook Dim i As Long
On Error Resume Next
With Application.FileSearch .NewSearch .LookIn = "C:Budget" .Execute Set wb = Workbooks.Open(.FoundFiles(1)) RenameWS wb wb.Worksheets.Copy Set newWB = ActiveWorkbook wb.Close SaveChanges:=False For i = 2 To .FoundFiles.Count Set wb = Workbooks.Open(.FoundFiles(i)) RenameWS wb wb.Worksheets.Copy After:=newWB.Worksheets(newWB.Worksheets.Count)
I'm trying to create a VBA macro which will open external workbooks in a specific folder. thes workbooks will have filenames which are identical to the value in cell B1 of my active workbook. Once open, I would use indirect to pull certain results from that external workbook and then close it. I have a separate macro already which runs through the list. I found a few examples online but they don't seem to work.
My code works well if the worksheet name is Sheet1. It will change constantly. I have alot of other similar workbooks that are not affected and cannot figure this out. Here is the code that I get the error at: ....
i would like an if macro to pick up if cell dest (i have used a case to define this cell) contains an error or more imoprtantly #REF! then change the offending cells to 0 and put up a message box to put "Check XTA". i have found some that i think may work but i didnt understand them (they had function in them :smask so i couldnt put them in.is there a way to put them in with out functions or could someone point me in the right direction.
how to supress the continue,end,debug, message when there is a vba error. The idea being that if there is a bug in my system that I have no realised, I don't want my end user seeing that message! I would preferrable design my own error message to appear instead.
I am using the close.workbook command in VBA to close a workbook I opened to copy data from. I am getting a message that says "You have a large amount of data on the clipboard. Do you want it available?" What command do I use in VBA say no to this message?
I have a rather large workbook (30 sheets, 10MB) that has one worksheet with many INDIRECT functions in it (pulling data from the same file, different tabs). I am working to put simple code into the workbook to protect and/or unprotect all worksheets. I have gotten code to work to both protect and unprotect all the sheets, but when I run the unprotect code (see below), and then I go to edit the workbook, data from the sheet with many "indirect" functions temporarily "overwrites" the data on the active sheet (this is fixed when I scroll my mouse over the effected cells). I am developing this workbook for other users, so I'd like to fix this before sending it off to them.
This problem does not happen until after I run the following .....
I used the code below for Conditional Formatting. This works fine but the VBA-code crashes when I delete more than one selected cell. Is there a simple modification possible to prevent this from happening?
Private Sub Worksheet_Change(ByVal Target As Range) Dim icolor As Integer If Not Intersect(Target, Range("A1:C250")) Is Nothing Then Select Case Target Case 1 icolor = 6 Case 2 icolor = 12 Case 3 icolor = 7 Case 4 icolor = 53 Case 5 icolor = 15 Case 6 icolor = 42 Case Else 'Whatever End Select
Target.Interior.ColorIndex = icolor End If End Sub
I have a macro that opens all workbooks from one directory and runs a macro for each workbook to clean up the data. I cannot figure out how to take all those open workbooks and save them to another directory and close the workbook. Also, I do not want the macro workbook (xlsm) to save. I only want it to close. I am working in 2007 Excel.
I copied the sheet and redid the format. I renamed the original dashboard sheet from count to "keep" and named the copy after the original "count" Everything still works great - until i either hide or delete the original count which is now named "keep". I get an error at the red colored line below ".publish false".