Multiple Excel Instances - No errors, but no changes from on macros - vba

Please refer to the following for the question:
Dim xlapp As Excel.Application
Set xlapp = GetObject(Application.ActiveWorkbook.Path + "\file - Map.xlsm").Application
Set mapbook = xlapp.Application.Workbooks("file - Map.xlsm")
Set map = mapbook.Worksheets("Map")
Debug.Print mapbook.FullName
Set databook = ActiveWorkbook
Set data = databook.Worksheets("Tables")
data.Activate
Originally, all sheets were in one workbook and everything worked splendidly. The macros would bounce between sheets and do its thing.
Now I was asked to split the project into two workbooks so it can run in separate instances, on separate monitors.
Currently, I have the files split, but when I try to run the macro (after changes to reflect the separate instances), I get no errors but I also do not get the effect of the macros reflected across the instances.
The process is meant to be:
Find instance with second sheet
Assign sheet from second instance to variable "map"
Click button in "data"
See effect in map
I used to get errors when trying to find the other instance but after googling for a while, found what I think is the solution (the Debug.Print shows the right name), but nothing happens in "map" when the macro is run.

Related

Excel keeps crashing when I set a certain worksheet

The workbook keeps crashing when it gets to the
Set wsEDF = Worksheets("edf master") line. I have tried to delete the sheet and then reinstate it and the same thing happens. Am a bit confused.
Sub CopyGroups()
Application.ScreenUpdating = False
Set wsMarex = Worksheets("marex master")
Set wsMQ = Worksheets("macquarie master")
Set wsEDF = Worksheets("edf master")
There is an end sub plus all the variables were declared at the top. Also at one point the macros worked fine. So have gone back to an old version and will look at changing that based on some advice below.
There's no sheet named edf master in the active workbook. Watch for whitespace (leading and/or trailing).
If the active workbook is ThisWorkbook (i.e. the workbook that contains the code that's running), then you don't need any of this.
Look at the Project Explorer (Ctrl+R). Under "Microsoft Excel Objects" you'll find a class module for every single sheet in your workbook. Click one, then hit F4 to display the Properties box.
In the sheet's properties, you'll see it has a (Name) property, which is probably saying something like Sheet1. Change it to something meaningful, e.g. MarexMasterSheet, or MacquarieMasterSheet, or EDFMasterSheet.
Then you don't need wsMarex, wsMQ and wsEDF anymore - they're already declared for you, as global-scope Worksheet instances. So, say the next line of code was this:
Debug.Print wsMarex.CodeName
Now you can do this instead:
Debug.Print MarexMasterSheet.CodeName

Dimension not valid while trying to add a worksheet to a workbook

I am working on a VBA script in Workbook_A which creates a series of separate workbooks (Workbook_B, Workbook_C). The script will create these separate workbooks, name them according to variables present in Workbook_A (in this case those variables are Workbook_B, etc.), write simulation results in sheets by different simulation run, and save them.
So far, I am able to open/create new workbooks and save them correctly. I'm running into issues when it comes to creating new sheets. Here are the relevant portions of the code:
... Initializing other variables...
Dim workbookname As String
Dim wksht1 As Worksheet
... irrelevant code ...
Set wkbk = Workbooks.Add
wkbk.title = savedVeh
workbookname = savedVeh & "full"
wkbk.SaveAs fileName:=workbookname, FileFormat:=56
Workbook_A.Activate
' Starting process of creating sheets for different results
Set wksht1 = Workbooks(workbookname).Worksheets.Add '***
wksht1.Name = "const_speeds"
ffastsim_validation.Activate
I get an error on the line with the '*** commented. The error states:
"The specified dimension is not valid for the current chart type."
This is my first time playing with VBA - am I just making a syntax error? Or is wksht1 dimensioned improperly for the operation I'm trying to perform? I'd appreciate any help explaining what's going on here.
You do:
wkbk.SaveAs fileName:=workbookname, FileFormat:=56
And then:
Set wksht1 = Workbooks(workbookname).Worksheets.Add
If I understand correctly, that Workbook object you're trying to re-dereference again, would be the wkbk object. So why bother dereferencing it again from the Workbooks collection? You already have it:
Set wksht1 = wkbk.Worksheets.Add

VBA - excel closes the previous workbook on opening the new one

I have a strange problem, I suscpect it's connected to the version of the Excel, but I'm not sure at all. I can't figure it out alone so I need your help. I have a macro, which operates on a fresh workbook - it's not saved anywhere, as the worker will save it manually afterwards. The macro is a .xlam format add-in, adding a couple of buttons to the ribbon and these buttons start the code.
Inside the code I have simple lines for opening a new workbook, chosen earlier by an user:
Application.DisplayAlerts = False
Set wbMPA = Workbooks.Open(MPA_file)
Application.DisplayAlerts = True
Earlier, the code sets active workbook as an object/workbook the macro will mainly work on (tried both versions):
Set dwb = Application.ActiveWorkbook
and later in the code
dwb.activate
OR:
dwb = ActiveWorkbook.Name
and then
workbooks(dwb).Activate
The lines are in separate subs, but the variable is globally declared.
The code works fine until the opening of wbMPA (watching it in the locals all the time). When I try to open the new file with the code above, the earlier workbook (dwb) just closes itself from unknown reasons.
The error I get from the 1st method is this:
error screenshot
The second one gives a simple "Subscipt out of range".
The errors, however, are not a problem. The problem is the cause of them, which is closing of the workbook from unknown reasons.
It happens only when I open the completely new workbook (using the excel icon on the Start bar) - when I do it from File -> New -> Blank Workbook using already opened workbook, the error does not occur.
Another strange thing - me and my colleague from work use 2013 version of Excel. I never have this error, she has it every time.
This is a general scheme of the code, other things are meaningless in this case because there are no other manipulations of workbooks/worksheets.
Dim dwb As Object
Dim wbMPA As Object
Sub_1()
Set dwb = ActiveWorkbook
Set wbMPA = Workbooks.Open(MPA_file)
Call Sub_2
End Sub
Sub_2()
dwb.Activate
End Sub
I get an error on the activation of dwb in Sub_2, because it closes itself for God knows what the reason on the opening of wbMPA in the Sub_1.
If you have only opened a blank workbook (clicking Excel from Toolbar, for example) and then you open any named workbook before making any changes to the blank workbook, the blank workbook will disappear. I believe that is normal/expected behavior.
I can't speculate why this happens on one computer but not another, but this is always how I have observed new/blank documents (Excel, PowerPoint, Word) to behave, and assume this to be the normal behavior. You may have some different option/configuration on your Excel environment which is changing this default behavior, or maybe you are slightly altering the blank file before running the macro, and your co-worker isn't, etc.
A word of caution to avoid relying on ActiveWorkbook -- and especially in this case if the expectation is to always Set dwb to a new/blank workbook, the best way to do that is to explicitly create a new/blank workbook, rather than relying on the user to manually open a new/blank target workbook.
Set dwb = Workbooks.Add
If, on the other hand dwb must be assigned to some other known/existing workbook, then you should be either providing the file path to an Open statement, or the workbook name to the Workbooks collection.
On a related note, it's almost never necessary to Activate a workbook, see here:
How to avoid using Select in Excel VBA macros
And further note: your variables aren't globally scoped, they're scoped only to the module using Dim statement. A public declaration uses the Public keyword, not the Dim keyword. Both module-scoped and global-scoped should be used with caution (Public moreso than module-scoped) and in most cases it's preferable to pass objects/variables by reference to dependent subs and functions:
How to make Excel VBA variables available to multiple macros?

Excel VBA - Formatting script for automation

So here's what I'm trying to do:
Open file: Pc_Profile
Create new sheet: Sheet1
Copy desired cells from Pc_Profile to Sheet1 (see script below)
Copy entire Sheet1 to new excel file: db.xls
Rename sheet to content of cell A5
Create new sheet for next script run
Basically I'm trying to automate an extraction of a TON of excel files into a single organized file. Each script call should extract to its own sheet so there's no overwritten information.
Here is what I have working so far. It just copies the desired cells to a new sheet within the same file.
' Create Excel object
Set objExcel = CreateObject("Excel.Application")
' Open the workbook
Set objWorkbook = objExcel.Workbooks.Open _
("\\[directory]\Pc_Profile.xls")
' Set to True or False, whatever you like
objExcel.Visible = True
objWorkbook.Worksheets("Pc_Profile").Range("A5:D5").Copy
objWorkbook.Worksheets("Sheet1").Range("A1").PasteSpecial
objWorkbook.Worksheets("Pc_Profile").Range("A8:B8").Copy
objWorkbook.Worksheets("Sheet1").Range("A2").PasteSpecial
objWorkbook.Worksheets("Pc_Profile").Range("A13:B13").Copy
objWorkbook.Worksheets("Sheet1").Range("A3").PasteSpecial
objWorkbook.Worksheets("Pc_Profile").Range("A15:D17").Copy
objWorkbook.Worksheets("Sheet1").Range("A4").PasteSpecial
objWorkbook.Worksheets("Pc_Profile").Range("A24:E26").Copy
objWorkbook.Worksheets("Sheet1").Range("A7").PasteSpecial
objWorkbook.Worksheets("Pc_Profile").Range("A28:B30").Copy
objWorkbook.Worksheets("Sheet1").Range("A10").PasteSpecial
objWorkbook.Worksheets("Pc_Profile").Range("A43:B43").Copy
objWorkbook.Worksheets("Sheet1").Range("A13").PasteSpecial
objWorkbook.Worksheets("Pc_Profile").Range("A45:B45").Copy
objWorkbook.Worksheets("Sheet1").Range("A14").PasteSpecial
' Activate Sheet2 so you can see it actually pasted the data
objWorkbook.Worksheets("Sheet2").Activate
I would really appreciate the extra push. I'm automating this for a work project and have no experience with VB - I just learned that on the go.
A couple things that are good practice to get into before I get to your actual question:
1) Any macro that you expect to run a long time should have Application.ScreenUpdating = False before any actual work is done in the code, this tells Excel not to bother with changing what's displayed on the screen (big performance booster). Be sure to include an Application.ScreenUpdating = True near the end of your code
2) Similar to #1, you generally want to include Application.Calculation = xlManual to boost performance. If you have large ranges of cells that your macro needs accurate up-to-date values from, it may be easier to leave the calculation automatic, but that doesn't appear to be the case in this instance.
3) You don't need to create a new Excel instance (which is what your first line of code does). You're already in a perfectly good instance of Excel. This also saves you having to close the other instance at the end of the macro (or worse from forgetting to do so and having memory get hogged by Excel processes that aren't really in use)
As to your specific problem, it sounds like you have more workbooks that Pc_profile to copy from, and that you're wanting to create a new "db.xls" with each run of the macro. Based on those assumptions all you need to do is nest your code starting with 'Open the workbook and objWorkbook.Worksheets("Sheet1").Range("A14").PasteSpecial inside a Do While loop. The thing I'm not sure about is how to control the loop. If the list of files is always the same, you should just include a list on a sheet in the workbook that holds the macro and just iterate through that.
The other thing you should do for ease of coding, and to make the loop more effective is declare and use a Worksheet variable and set if for each workbook to the appropriate sheet to pull data from. Ex.
Dim ws as Worksheet
'The Dim is outside your loop, but this would be inside it
Set ws = objWorkbook.Worksheets("whatever_the_sheet's_name_is")
This way you can replace each occurrence of objWorkbook.Worksheets("Pc_Profile"). with ws., easier to type, easier to read, easier to update, and less error prone.
Next, you don't actually have code for moving Sheet1 to a new workbook, or renaming it. To move it (as well as the other Sheet1's yet to be created), you should, before getting to the Do While loop, have the following
Dim target as Workbook
Set target = Application.Workbooks.Add
Then at almost the end of the loop, you need objWorkbook.Worksheets("Sheet1").Move Before:=Target.Sheets(1)
Last, you need to include objWorkbook.Close SaveChanges:=False after you've moved Sheet1 out of the Pc_Profile and renamed it.

Why do I get an HRESULT 0x800A03EC when using Worksheets.Visible?

I've been trying to figure out ways to tell the difference between instances of excel that load the worksheets and ones that don't
Currently I use code to open existing files that looks something like that:
Dim wkbWorkBook as Excel.Workbook
Dim objExcel As Excel.Application
wkbWorkBook = System.Runtime.InteropServices.Marshal.BindToMoniker(filename)
objExcel = wkbWorkBook.Parent
'To make the excel app visible while working with it:
objExcel.Visible = true
What i've noticed while using this code is that If I open a file that exists, but isn't open in excel, when I make the Excel Application visible, the Worksheets aren't visible, but they do exist (I can access worksheets.count and there is an appropriate number of sheets)
I try using Worksheets.Visible but I've noticed it only has an HRESULT error in the place where a "Visible" value would be.
The same error occurs when I try to get the Visible property even when the worksheets are visible (in cases when I BindToMoniker() a file that is currently open in excel.. )
Part of my question is why the BindToMoniker() + Application = workbook.Parent is always giving me an Excel Application without any sheets loaded.. I can't work with it when it's like that..
without gurantees, but I would say worksheets.visible -> won't work, becasue worksheets gives you a list of worksheets.
You cannot apply .visible on that -> you would have to use worksheets(1).visible
Additionally, if there aren't any worksheets, this should fail anyways. you would have to check your count of worksheets first.
Another thing - i don't know if this is possible under vb.net with interop, but maybe you can access specific worksheets through their internal codename - like this:
pobjExcel.table1.visible