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

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

Related

run time error 9 subscript out of range when selecting workbook

I am trying to build a data entry form in Excel 2010 using VBA. During form initialization I am setting an object to refer to another worksheet so I can access it. This is what I have:
Private Sub UserForm_Initialize()
Dim master As Excel.Workbook
Dim masterworksheetFmn As Excel.Worksheet
Dim masterworksheetAdv As Excel.Worksheet
Dim masterworksheetTechs As Excel.Worksheet
Dim i As Integer
Set master = Excel.Workbooks("ServiceReturnsMaster.xlsm")
Set masterworksheetFmn = master.Worksheets("Foremen")
Set masterworksheetAdv = master.Worksheets("Advisors")
Set masterworksheetTechs = master.Worksheets("Techs")
When I run the macro, I get "Run Time error9: Subscript out of range". This occurs at this line:
Set master = Excel.Workbooks("ServiceReturnsMaster.xlsm")
HOWEVER, the error does not occur if I open the 2nd workbook before running the macro. I am assuming I have to activate the second workbook or something first or my reference is not written correctly. I can find lots of references to this run-time error, but none that directly address what I'm trying to do. What am I doing wrong?
When you use Excel.Workbooks you are referring to a generic Workbooks collection. If you want to access the Workbooks collection that is in your current application, then you need to use Application.Workbooks.
You can also use it without the Application qualifier as it's assumed you are working in the current application instance, so
Set master = Application.Workbooks("ServiceReturnsMaster.xlsm")
or
Set master = Workbooks("ServiceReturnsMaster.xlsm")
Should both work identically.
Alternatively, if your workbook isn't open at runtime, then you need to use the Workbooks.Open() method instead:
Set master = Workbooks.Open("C:\Path\to\ServiceReturnsMaster.xlsm")

vba excel sheets.range.value Error

So, sometimes when I try to execute this command, it gives me an error. The problem is that it is very inconsistent. In certain cases, it works and in others, it just doesn't.
This is the line from getCellVal function. cellName is a string and s is an integer.
getCellVal = Sheets(s).Range(cellName).Value
This time it is giving me:
Run-time error '438':
Object doesn't support this property or method
This line actually worked without problems moments ago. I added some other functions that use it and now it's not working anymore.
Any ideas about why?
Unqualified calls into the Sheets collection implicitly refer to whatever workbook is currently active, and it's important to know that the collection contains Worksheet objects, ...but also Chart objects.
Since you mean to work with a Worksheet, use the Worksheets collection instead.
If you're not getting an "index out of bounds" error, then the sheet you asked for does exist. But error 438 points to that sheet not being a Worksheet (and thus not having a Range member).
I bet the active workbook has a chart sheet at index s.
The solution is simply to be explicit.
If you mean to work with the workbook that contains the code that's running, qualify Workbook member calls with ThisWorkbook:
getCellVal = ThisWorkbook.Worksheets(s).Range(cellName).Value
If you mean to work with a workbook, then you need to get ahold of the Workbook object when you open it:
Dim wb As Workbook
Set wb = Application.Workbooks.Open(path)
'...
getCellVal = wb.Worksheets(s).Range(cellName).Value

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

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.

How can I use ActiveWorkbook/Workbooks(name) without getting a Runtime Error 91 in Excel 2013 VBA?

While this is not the first time I wrote macros like this, I cant seem to figure out how I get this to work. I have a snipped below that I think should work but doesn't.
My problem is that while at the beginning of my work day the snipped worked exactly once, but afterwards I always get the Runtime Error '91: object variable or with-block variable not set error message.
I have two workbooks opened, and I selected cells within the workbook I wanted to work with manually before starting the macro.
Option Explicit
Sub test()
Dim source, sheet As Worksheet
Dim sourcename As String
Dim targetname As String
Dim test As Workbook
Workbooks("Test.xlsm").Activate
test = ActiveWorkbook
Debug.Print (test.Name)
sourcename = "Tabelle1"
targetname = "Tabelle2"
Set sheet = ActiveWorkbook.Worksheets(sourcename)
Set source = ActiveWorkbook.Worksheets(targetname)
Debug.print(sheet.Name)
Debug.print(source.Name)
End Sub
The error occurs in the line containing test = ActiveWorkbook
EDIT: After the initial Seterror was fixed, the sheet and source variables are still empty, thus each is causing another error of this type.
What is the problem here and how can I fix it?
Two issues here:
1st- do not give the same name to both Variable and Sub, you have 'Test' for both
2nd- add Set before this line test = ActiveWorkbook like
Set test = ActiveWorkbook
remember- change Sub name into anything other then test

VBA Excel Workbooks Object Variable

I am trying to set two workbook variables - here are the first four lines of my code:
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Workbooks("C:\Users\ShaneM\Documents\File1.xlsx")
Set wb2 = Workbooks("C:\Users\ShaneM\Documents\Computer Languages\VBA\File2.xlsm")
The error I am getting on line 3 is:
Runtime error 9 - Subscript Out Of Range
The files definitely exist in these locations. Obviously much learning to be done. What am I doing wrong, please?
Thank you.
The .Workbooks() is a collection of workbook objects opened in the current Application object (which you don't write in your code, but it's called by default).
The collection .Workbooks() goes out of range because the workbook is not currently open in your Application; if you want to use your workbook, in fact, what you need to do is opening it into the current Excel Application instance:
Set wb1 = Workbooks.Open("C:\...")
This will though cause the workbook to show up, because you're opening in your current application, and unfortunately is not possible to open it in Visible=False mode. However, you might want to use a new Excel instance set to Visible = False, so you will be able to read the content without actually displaying it. Which means, in code:
Set excObj = CreateObject("Excel.Application")
excObj.Visible = False
Set wb1 = excObj.Workbooks.Open("C:\...")
Please note that I'm specifying to open the workbook with excObj, which is not the current Application but another object of the same type that is intentionally set to Visible = False.
According to https://msdn.microsoft.com/en-us/library/office/ff841074.aspx, the Workbooks collection contains only the currently opened workbooks. The error simply tells you, that the requested workbook is not opened at the moment.
You have to use the method Workbooks.Open:
Set wb1 = Workbooks.Open("C:\Users\ShaneM\Documents\File1.xlsx")
When the Workbook is already opened, you can then use the property Workbooks.Item to get the object (note that you can omit .Item, as it is the default property of the Workbooks collection).
Set wb1 = Workbooks("File1.xlsx")
Also note that the names of opened workbooks must be unique within the same application. I.e. the following will not work:
Set wb1 = Workbooks.Open("C:\Folder1\File.xlsx")
Set wb2 = Workbooks.Open("C:\Folder2\File.xlsx")