If I only create one workbook the following works fine
Dim oXl As New Microsoft.Office.Interop.Excel.Application
Dim wb_main As Workbook
wb_main = oXl.Workbooks.Add
...add sheets and data to sheets
CType(wb_main.Worksheets(1), Worksheet).Select()
wb_main.SaveAs(Filename:=_files(0)._file_path.parentDir.parentDir & "out.xlsx")
But with two open, i get an error. Interop errors are not alwasy straightforward to debug.
Dim oXl As New Microsoft.Office.Interop.Excel.Application
Dim wb_main As Workbook
Dim wb_extended As Workbook
wb_main = oXl.Workbooks.Add
wb_extended = oXl.Workbooks.Add
...add sheets and data to sheets in both workbooks, no particular order
CType(wb_main.Worksheets(1), Worksheet).Select()
wb_main.SaveAs(Filename:=_files(0)._file_path.parentDir.parentDir & "out.xlsx")
I get an error HRESULT: 0x800A03EC using excel 2013. A google for the error shows alot of people with different problems as far as I can tell.
.Select() only works on the active workbook. wb_main.Activate() before selecting will solve the issue.
The second workbook open is the active one. making edits to the workbooks by using directly assigned variables does not change the active workbook.
Related
I would like to open a workbook in a new instance of Excel, then copy ranges from within several different sheets in another (already open) workbook to specified ranges in the newly opened workbook.
Here's what I have so far:
Dim wbl As Workbook
Set wbl = Workbooks("exports - 27thNov.xlsm")
Dim wsl As Worksheet
Set wsl = Sheets("odbd")
Dim lrl As Long
lrl = wsl.Range("A" & Rows.Count).End(xlUp).Row
Dim wbl2 As Workbook
Set wbl2 = Workbooks("pltemplate.xlsx")
Dim wsl2 As Worksheet
Set wsl2 = wbl2.Sheets("S")
wbl.Worksheets("odbd").Range("A2:AK" & lrl).Copy
wbl2.Activate
wsl2.Range("A4").Select
wsl2.Range("A4").PasteSpecial Paste:=xlPasteValues
Note: the Activate & Select commands were added to try to force the macro to work in some way.
Also, I changed the name of the destination workbook ("pltemplate.xlsx") to make it a more simple string, as well as the name of the source worksheet ("S"), yet I get the same run-time error message.
The line which throws out the error is:
Set wbl2 = Workbooks("pltemplate.xlsx")
After last OP comment, the solution was opening both workbooks in same instance of Excel.
The destination workbook (pltemplate) is already open, there's some code before the above code which opens a new instance of excel
Then you can't do anything between worbooks opened in diferent instances of Excel. Each VBA macro runs across own instance, so your only solution is opening both workbooks inside same instance of excel:
Set wbl2 = Workbooks("pltemplate.xlsx") When you execute this line, the workbook pltemplate.xlsx must be already opened in the same instance of Excel.
IF the workbooks is not opened, you must use:
Set wbl2 = Workbooks.Open("pathtofile/pltemplate.xlsx")
More info about Excel instances and/or related info:
How to reference a workbook from another excel instance
Running Multiple Instances of Excel simultaneously from VBA?
Can having multiple instances of Excel open cause VBA issues?
I have an Excel workbook in which I have embedded another Excel workbook. I am able to open it with VBA, but I have no idea how to refer and edit some cells in embedded workbook. Any idea how to do that? Thanks a lot in advance.
Sub openembeddedXL2()
Sheets("sheet1").OLEObjects("SalesFile").Activate
End Sub
As long as a workbook is open you can directly refer to it by its name.
Workbooks("workbook name") etc.
Since you open the workbook with Sheets("sheet1").OLEObjects("SalesFile").Activate the workbook related to the object will then be opened as a file called "Worksheet in your current workbook". You can therefore use:
Dim wb as workbook
Sheets("sheet1").OLEObjects("SalesFile").Activate
set wb = Workbooks("Worksheet in " & ThisWorkbook.Name)
Thisworkbook.sheets("Sheet1").Range("A1").value = wb.sheets("Sheet1").range("A1").Value 'etc. etc.
wb.Close
Thisworkbook is a handy tool here, as it will always refer to the workbook the macro is in, despite which workbook is currently active.
I have MS Access producing a bunch of workbooks and worksheets. When a workbook is creates there are extra worksheets in the workbook named "Sheetn"
I know the following code works but my question is about timing.
Dim ws as excel.worksheet
For each ws in wbWorking.Worksheets
oxl.DisplayWarnings=False
If ws.name like "Sheet*" then ws.delete
oxl.DisplayWarnings=True
Next ws
The above code does not work until I save the Workbook. The issues is that the client is watching for the workbooks to populate the directory and will open them as soon as they show. This causes issues if the above code runs and the client has the workbook open. I would like to delete worksheets before the workbook is saved.
Please advise.
Working from when you first create the new workbook, something along these lines should work:
Public Function BuildWorkbook(wbkToCopy As Excel.Workbook) As Excel.Workbook
Dim xl As Excel.Application
Dim dummySheet As Excel.Worksheet
Dim sheetToClone As Excel.Worksheet
Dim i As Long
Set xl = wbkToCopy.Application
Set BuildWorkbook = xl.Workbooks.Add(1)
Set dummySheet = BuildWorkbook.Worksheets(1)
For Each sheetToClone In wbkToCopy.Worksheets
'This is just to handle naming conflicts
Do Until sheetToClone.Name <> dummySheet.Name
dummySheet.Name = Format(Now(), "yyyymmddhhnnss") & CStr(i)
i = i + 1
DoEvents
Loop
sheetToClone.Copy BuildWorkbook.Worksheets(1)
Next
If BuildWorkbook.Worksheets.Count > 1 Then
dummySheet.Delete
End If
End Function
Obviously, you can add the sheets however you need to, but the keys are:
Specify =Workbooks.Add(1) when you create the new workbook, so it is created with only 1 worksheet
Set a reference to the worksheet that you don't want when you first create the new workbook
Using that initial reference, delete the worksheet at the very end of the process, before saving the workbook
I'm fairly new to vba coding and I'm getting hung up with how to navigate between sheets. I have a workbook that contains a single sheet and that sheet is copied into a new workbook so that it can be edited without damaging the origianl. The issue that I'm having is when the new work book is created I need to copy informaton from a 3rd workbook and paste it into the new one. I've tried a number of things but haven't been able to figure it out. I think the issue is that the new book doesn't have a name just Book# so once I leave to activate the other book I don't have a solid path back.
Sorry for the lack of an actual example. I have added the first 2 lines based on the response I received.
Dim wb As Workbook
Set wb = ActiveWorkbook
' Open the 3rd workbook & copy
Workbooks.Open ("Z:\Terms And Conditions.xlsx")
Windows("Terms And Conditions.xlsx").Activate
ActiveSheet.Shapes.Range(Array("Picture 4")).Select
Selection.Copy
' Return to wb and paste
ActiveWorkbook(wb).Activate 'The code is stopping here
Worksheets("Sheet1").Activate
Range("A534").Select
ActiveSheet.Paste
it would help to see some code but if you are simply copying the sheet to create a new workbook you can store a reference to the activeworkbook
dim wb as workbook
set wb = activeworkbook
and then refer to wb later in the code as required
I have a directory with list of Workbooks, I want to loop through them withouth opening them and update a Cell in a certain Sheet
I have tried to use
Dim wb As Workbook
Set wb = Workbooks("Z:\dir\bla.xls") 'THIS WILL COME TRHOUGH WHEN I LOOP
Set ws2 = wb.Sheets("TestSheet") 'SHEET NAME
Set CurCell_2 = ws2.Range("A1")
CurCell_2.Value = 5
The Problem comes it only works when I have the Workbook already open. I can use:
Workbooks.Open
But then It opens up in the background and takes to long to run through them all.
Can anyone help please
You cannot do that without opening the workbooks. However, I have found in my case that using Application.EnableEvents and setting it to false sped up greatly the process because we have macros on workbook open event.