Out of memory when open a golden xlsm file - excel

I added few VBA code in my workbook (myworkbook.xlsm) for the function :
when my workbook is opened, the VBA code will copy one sheet from the golden workbook (golden.xlsm).
I added the Sub Workbook_Open into the object "ThisWorkbook" of myworkbook.xlsm, the copy is successful. But the issue is when I use "Alt+F11" to open the Visual Basic editor, there will be a message box to complain "Out of memory", and I can't edit my other VBA code anymore.
I try to comment out some code in the Workbook_Open handler, the following line will lead to this issue :
Set goldenWorkbook = Workbooks.Open("golden.xlsm")
My code added for the open event of object "ThisWorkbook"
Private Sub Workbook_Open()
Dim goldenWorkbook As Workbook
Application.ScreenUpdating = False
Debug.Print "Hello~you just open the file"
Set goldenWorkbook = Workbooks.Open("golden.xlsm")
goldenWorkbook.Sheets("Common").Copy Before:=ThisWorkbook.Sheets(1)
goldenWorkbook.Close True
Application.ScreenUpdating = True
Set goldenWorkbook = Nothing
End Sub

Related

Open/Close Workbook in Userform

In a Userform I want to open another workbook (than the active one)
Part of my code:
Private Sub cmbKontoPos1_Change()
Workbooks.Open Filename:=filename1
'Here should of course be some code, but it is not now
Workbooks(filename1).Close SaveChanges:=False
Open-command works.
But the Close-command gives error:
Error no '9'
Index out of bounds
(I get the error-message in Swedish, hope I translate correct)
What is it I do not understand?
Quick fix is to have a global Workbook Variable so you can assign it on Open and use it on other UserForm Events, the example below has two Buttons and one opens while the other one closes the Workbook. Tested and working.
You can see the Dim secondWorkbook outside the UserForm buttons this is to make it global. On the first button I assign the Opened workbook to that Variable and i can close it later.
Dim secondWorkbook As Workbook
Private Sub CommandButton1_Click()
'Open WB
Dim filename1 As String
filename1 = "F:\WorkbookPath\WBName.xlsx"
Set secondWorkbook = Workbooks.Open(filename1)
End Sub
Private Sub CommandButton2_Click()
'Close WB
secondWorkbook.Close False
End Sub
You can also use this variable to make changes to the workbook like: secondWorkbook.Worksheets(1).Range("A1").Value = "Test" as long as it is still open of course.

Run Excel Macro from Access macro in VBA

I am trying to run an excel macro from access 2016. This has a lot of examples out their however they are all old. The exact error code I receive is run-time error '1004':
cannot run the macro "TestScript()". The macro may not be available in this workbook or all macros may be displayed. I am only running something easy for the test. I have ensured that macros is enabled in excel. the excel code is as follows
Public Sub TestScript()
MsgBox "IT WORKED"
End Sub
Real simple for the test. Access is opening the excel spreadsheet however it stops there with an error code.
My Access code is very simple and is below. I have also noted where the code stops. While I am new at VBA I have done a lot of research in this. I am testing as simple code as I could figure out. Any help would be welcomed.
Option Compare Database
Function runExcelmacro()
Dim XL As Object
Set XL = CreateObject("Excel.Application")
With XL
'Turn Off warnings
.Visible = False
.displayalerts = False
'WorkBook path such as "C:\Computer\WorkBook.xlsx"
.Workbooks.Open "C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm"
'Run the Macro in excel getworkbook
.Run TestScript 'Code stops here!
'Close Workbook
.ActiveWorkbook.Close (True)
.Quite
End With
Set XL = Nothing
End Function
Public Sub runMacroSub()
Call runExcelmacro("C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm", "TestScript")
End Sub
I guess OP did not put the code of Testscript in an extra module. Instead the code was put into the class module of the workbook or a worksheet. In the latter case you have to add the workbook or worksheet name in front of the name of the script.
Either it is `
.Run "ThisWorkbook.TestScript"
or in case it is in Sheet1 (codename of the sheet!)
.Run "Sheet1.TestScript"
Do not forget the quotation marks!
PS The OP's code above is working if you put testscript into a module and add quotation marks.
.Run "TestScript"
Here is a description how to create a module and add code
Here try this:
Public Sub TestScript()
MsgBox "IT WORKED"
End Sub
Option Compare Database
Function runExcelmacro()
Dim XL As Object, wb as object
Set XL = CreateObject("Excel.Application")
With XL
.Visible = False
.displayalerts = False
set wb = .Workbooks.Open "C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm"
wb.Run TestScript 'Code stops here!
.ActiveWorkbook.Close (True)
.Quite
End With
Set XL = Nothing
End Function
Public Sub runMacroSub()
Call runExcelmacro("C:\DATABASE\BLQ-10\Import Database BLQ 10\NTIRAINSTALLTO.xlsm", "TestScript")
End Sub
Here i declared wb as an object and then set wb = to the workbook.
This is of course assumin your test script is in the actual workbook being opened. Would be real funny if that wasnt a thing lol

How to duplicate a worksheet without duplicating its Worksheet Private sub code

I'm trying to duplicate a worksheet with a macro, but when doing so, to private sub is duplicated as well, what I don't want to happen because it interferes afterward with another module macro. I have seen this post Copy a worksheet without copying the code and tried, but it doesn't work properly. Some ideas on how to do that?
Sub Export()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Application.CutCopyMode = False
Sheets("CB").Select
MonthID = Range("N2").Value
YearID = Range("O2").Value
saldoID = Range("O18").Value
ActiveSheet.Unprotect
ActiveSheet.Copy After:=Sheets("CB")
' Get the code/object name of the new sheet...
Dim Code As String
Code = ActiveSheet.CodeName
' Remove all lines from its code module...
With ThisWorkbook.VBProject.VBComponents(Code).CodeModule
.DeleteLines 1, .CountOfLines
End With
The last step (.DeleteLines 1, .CountOfLines) always causes an error: "Can't enter break mode at this moment" - Run time error 1004 Application defined or object defined error.
Why, what is wrong or missing??
Thanks
One simple way:
copy sheet to new workbook
save new workbook as .xlsx
close new workbook
re-open new .xlsx workbook
copy the macro-free worksheet back to original workbook
The VBA code for this is simple; you could also include:
re-close the.xlsx
kill (delete) the .xlsx workbook

Open and close UserForm multiple times without showing it's workbook

I've created a workbook that only shows a UserForm when it runs with the following code
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub
When the UserForm is initialized, it opens another workbook (WHITEBOOK.xls). After data has been entered into the form and the user clicks the Submit button, the data is inserted into this workbook that has been opened (I've omitted that code, but it works)
Private Sub UserForm_Initialize()
Set wb = Workbooks.Open("S:\!Data Tracking\WHITEBOOK.xls")
wb.Sheets("Jobs with Correct Template").Activate
If IsEmpty(wb.Worksheets("Jobs with Correct Template").Range("A3").Value) = True Then
Set SheetNameCellRange = wb.Worksheets("Jobs with Correct Template").Range("A2")
Else
wb.Worksheets("Jobs with Correct Template").Activate
Set SheetNameCellRange = wb.Worksheets("Jobs with Correct Template").Range("A2", Range("A2").End(xlDown))
End If
End Sub
When the user closes the UserForm, I close the form with this code, and close the UserForm's workbook also.
Private Sub UserForm_Terminate()
Unload Me
For Each wb In Application.Workbooks
If (StrComp(wb.Name, "Whitebook Data Entry (Final).xlsm", vbTextCompare) = 0) Then
wb.Close
End If
Next
End Sub
The problem I'm having is that I can't open the UserForm if the WHITEBOOK.xls workbook is already opened. I get a Run-time error '9': Subscript out of range and it fails at this line of the UserForm_Initialize() Sub.
wb.Sheets("Jobs with Correct Template").Activate
I've tried adding a conditional when I open the WHITEBOOK workbook to check if it is already open, and if so, to make it active, but I keep running into the same issue.
The code I used to test if the workbook is open was
Private Sub UserForm_Initialize()
Dim wkb As Workbook
On Error Resume Next
Set wkb = Application.Workbooks("S:\!Data Tracking\WHITEBOOK.xls")
If wkb Is Nothing Then
Set wb = Workbooks.Open("S:\!Data Tracking\WHITEBOOK.xls")
Else
wb.Sheets("Jobs with Correct Template").Activate
End If

Auto Close Workbook

I have 2 workbooks. Workbook A & B.
When workbook A opens, workbook B opens automatically. Auto open workbook macro is working nicely. But when I want to close workbook A, the workbook B should close together automatically. But with the auto close macro below, it doesn't seems to work. Please advise where went wrong :
Private Sub Workbook_Close()
If Workbooks.Close("A.xlxm") Then
ThisWorkbook.Close
End If
End Sub
The code above is pasted in workbook B's ThisWorkbook.
In workbook A in the ThisWorkbook code pane.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.Workbooks.Item("B.xlsm").Close True
On Error GoTo 0
End Sub

Resources