Run-time error "9": Subscript out of range - excel

In a module in PERSONAL.XLSB, I called a worksheet (while keeping it open) in a different workbook of the same name. The whole code runs perfectly in the device I wrote it in. However, I had it transferred to a separate device with previously no enabled macros or personal files, and this particular line
Set C = Workbooks("Base.xlsm").Worksheets("Base").Range("A1").Offset(0,i)
returns (noticed while debugging; it gets highlighted)
Run-time error "9":
Subscript out of range.
I have no clue how to work around this since it's not happening in my own device. Please help!
Note. What the code basically does is copy some elements from the Base Worksheet into 2 sheets of a new workbook. However, in the device with the above problem, all things work perfectly until copying of cells start i.e., creation of the new workbook goes well. However, calling Base results in an error. What really baffles me is that there is no error in my own device. Are there any rules I need to follow to save Base.xlsm (in a separate folder?)

Related

worksheet.calculate method looping

My project is kind of a simulation. It computes the formulas in a set of worksheets repeatedly with differing input data. These worksheets are created for each run from scratch by the VBA macro based on templates, assembling blocs of cells with formulas like LEGO bricks. The workhorse of the simulation is the “worksheet.calculate” method. It does not work anymore since a while, and I could not find the reason. The code lines below are those, where execution of the macro gets caught in an “eternal” loop. Execution of the code line “ws_C_S.Calculate” takes forever. The task manager shows Excel using 50% to 90% of CPU in this phase and the computer starts to make remarkable noise after a few minutes (may be due to heavy disk operations). I am running Office 365 under Windows 10 64-bit and set the Option explicit option in every module. The saved workbook appears to be fine. It behaves okay when opened stand-alone by hand.
Suggestions how to fix this would be highly appreaciated.
Here are the code lines, where execution gets caught:
' create a fresh CalculBook with 1 worksheet and save it as xlsb file
Set wb_C = Workbooks.Add(xlWBATWorksheet)
Set ws_C_S = wb_C.Worksheets(1)
ws_C_S.Name = ws_S_Name
wb_C.SaveAs Filename:=CalculBookFullName, FileFormat:=xlExcel12
MsgBox "going to calculate the SystemSheet in the CalculBook"
' Here I had originally a number of Copy > PasteFormulas operations
' I removed them piece by piece because the final calculation failed
‘ I hoped to identify some faulty formulas this way
' But the error continues to happen even without any formulas in the worksheet!
ws_C_S.Activate ‘ it tried it with and without activating the worksheet, leading to the same result
ws_C_S.Calculate
MsgBox "SystemSheet calculated"

Method 'Add' of object 'Sheets' failed, and cannot rename new sheets

In my VBA project, I frequently make a temporary sheet in the workbook, which gets deleted before the macro exits. The sheet is added as follows
temp = "Temp"
ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = temp
This has been going well for quite a while. Now, however, the code returns an error
Run-time error '1004': Method 'Add' of object 'Sheets' failed
A sheet has been added, although not named "Temp" (instead it's named Sheetn).
I can successfully add a new sheet, but get the following error message. I am unable to change the name of the newly added sheet.
"We couldn't move this sheet"
The workbook is not protected, and there is no other sheet named Temp present in the workbook. Because of this, I feel I have encountered an invisible wall that prevents users from adding large quantities of sheets, even though these sheets are deleted.
This puzzles me, any explanations and/or workarounds?
Having noted from the comment thread that you've solved it by Saving, Closing and Reopening, that #SandPiper has had the same problem recur after a while, and that your OP asks for workarounds, the obvious one that I would suggest would be to stop creating and then removing the Temp sheet, and instead simply create it as a permanent part of the workbook, then hide it, and simply use it when it's needed and clear it afterwards (programmatically).
Having the same problem here and in my case the problem was resolved by temporarily disabling the autosave function. I never had this problem until i started working with 365 version. Any other having this problem on other version then 365?
Microsoft® Excel® voor Microsoft 365 MSO (Versie 2202 Build 16.0.14931.20128) 64 bits

getting Run-time Error 9: Subscript out of Range when trying to get value from a range in another open workbook

Whilst working in one workbook, I have code that puts the value of a named range in another open workbook (Workbooks("Draft Sales")) into a variable. I don't activate Workbooks("Draft Sales"). This works on my computer and one of my colleagues computers, but not on two other colleagues computers - where it brings up the error message "Run-time Error 9: Subscript out of Range".
I don't understand what the difference is. We're all on the same version of Excel (Excel 2013, 32 bit), and other parts of the code work fine on all four computers.
Here are two examples of code that works for two of us but not the other two:
varFormula = Workbooks("Draft Sales").Worksheets("HoldingBay").Range("InvOrCreditGrossTotalFormula").Formula
dblChkCalcd = Workbooks("Draft Sales").Worksheets("HoldingBay").Range("CheckCalculationIsFinished").Value
I've confirmed on my colleagues' computers that the "Draft Sales" workbook is open in the same instance of Excel, by running code to list all the open workbooks, and this does pick up the "Draft Sales" workbook.
Can anyone suggest why the code isn't working on my colleagues' computers?
Need to include the workbook extension:
Workbooks("Draft Sales.xlsx")

Error when setting workbook variable - 2147352565

I have a simple code that runs upon initialization of a userform that sets a few workbook and worksheet variables so they can be used easily throughout the rest of my modules, and the references can be easily changed in one place if the file moves. I recently migrated my workbooks from my desktop to a separate server/drive, and accordingly updated the file pathways; however, when I try to run the code now I immediately get the message:
"Run-time error '-2147352565 (8002000b)': Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus."
This error occurs on the line
Set ReportWkbk = Workbooks("N:\ rest of file pathway here\QuaRT_Template.xlsm")
Is there anything that can be happening on the server/drive that is causing this issue? If so, is there anything I can do to fix it? I do work with other excel workbooks saved in the same location that seem to have no issue being referenced, though their references are in the workbook itself, not through Visual Basic.
The Subscript Out of Range error occurs because the Excel workbook being referenced is not open (or opening) in the same instance of Excel. You can easily reproduce the error by creating two workbooks (name one WorkBook2.xlsx) and ensuring they open in separate instances of Excel. Then run this code:
Sub OpenWkbkNames()
Dim wbk As Workbook
For Each wbk In Workbooks
Debug.Print wbk.Name
Next
'Hmm. Workbook 2 is not listed, but lets activate it and see what happens.
Workbooks("workbook2.xlsx").Activate
End Sub
You could avoid the issue by looping through the names of the workbooks open in THIS instance of Excel to ensure its available.
I've been researching Run-time error '-2147352565 (8002000b)' and found this: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.vsconstants.disp_e_badindex.aspx
Apparently, that is VS's way of saying Subscript Out of Range too. I suspect (but have no solid proof) that since the code worked locally but the issue appeared after the file was migrated to a shared drive that Windows is taking a long time to open the file so opens it in a new instance of Excel. Essentially, Excel gets impatient and decides to move on while Windows completes its task. I base this assumption on the detection logic that was added to Excel 2013 (https://blogs.office.com/2013/06/03/opening-workbooks-by-running-separate-instances-of-excel/).

Published Excel macro fails to be recognized

I downloaded this macro to apply names to worksheets (worth noting that this page is also referenced in Microsoft's help pages somewhere). But when I try to run the macro, it's like it doesn't exist. If I select Macros from the Developer tab, it's not there (and yes, I've checked "all open workbooks"). If I open the VB editor window and open the module, I can see the code, but selecting Run or Debug
F8 does nothing at all. No error, no highlighted code. All works as expected for all my other macros.
This is Excel2010 on Windows7Enterprise.
Any ideas?
You do not run this macro. It goes into the Worksheet_Change event which is the specific Worksheet object module (ie. Sheet1 object module, Sheet2 object module, etc).
It is an event that fires automatically when you modify a cell in worksheet.
There instructions how to run this are actually on that web so it's worth reading them! :)
This is where the code should be placed
You can read more about the Worksheet_Change() event here
Just in case you still wander how to run this
place the code in the Sheet1 object module code area
go back to your spreadsheet Sheet1
type in a name in cell A1, and your sheet1 name will immediately change to whatever you've typed as long as it doesn't cause macro to exit at some point. The restrictions are mentioned here

Resources