I got a "Subscript out of range error" while running a code that I wrote.
The faulty code line was:
value = Sheets("name_of_sheet").Range("C2").value
The strange thing for me is that this exact code worked daily for a year now without any problem.
Changing "Sheets" to "Worksheets" solved the problem, so I am sure that the sheet name is OK.
Can someone explain this please?
Thanks.
Related
When stepping through my code i get error 1004 on this line.
Range("tablename[[#Headers],[startheadername]:[lastheadername]]").Select
Now the weird thing is, I use it every month without issue via a button.
This error only pops when im stepping through the code.
this works instead
ActiveSheet.ListObjects("Tablename").HeaderRowRange(1).Resize(, 7).Select
The error code involves the words "range of object" which made me think that maybe there was a way to use the object in another manner. my original code was just a simple range reference.
I dont have particular insight into listobjects, i mainly copy pasted and tested. But headerrowrange(1) is the first column header in the tablename, resize(,7) expands it from 1 to 7 and .Select does what it does.
So this new code selects the first 7 table headers without error 1004.
This site got me there https://exceloffthegrid.com/vba-excel-tables-code/
I am running a really simple copy-paste macro to break circular references in excel. It equals some offset of the range "facilityPaste" to "facilityCopy". However, everytime this is run, I get 2 "Out of Memory" errors. I have tried looking for an answer everywhere but for some reason cannot resolve this.
So far I have tried renaming the named ranges, and specifying everything to be a .Value, but to no avail.
The identical code used to not throw any errors before. As it's part of a larger (confidential) block of code, would it be possible that the error is caused by something else? Note that the error occurs also when run separately.
Any help is greatly appreciated!
EDIT 1: The errors are thrown regardless of what code is actually ran. Perhaps this is a different problem altogether? To confirm, the code execute correctly.
Sub [redacted]()
Application.ScreenUpdating = False
Range("facilityCopy").Offset(, Range("Case").Value).Value = Range("facilityCopy").Value
Calculate
Application.ScreenUpdating = True
End Sub
I created a new macro earlier and it was working fine but now I get this error. I have not changed anything including the columns or worksheet name, any ideas why it's erroring now? The row that is highlighted in yellow when I try to debug it is:Columns("L:L").Select.
Sub FTE_Joiners()
FTE_Joiners Macro
Sheets("Joiners").Select
Columns("L:L").Select
Range("L2").Activate
Selection.NumberFormat = "0"".""0""""0"
End Sub
You can reduce that to
Sheets("Joiners").Columns("L:L").NumberFormat = "0"".""0""""0"
There's nothing in what you've told us that would suggest a problem.
Read this.
Sorry for such a simple question, I can't figure this out. I have searched several forums for ideas but none of them solved my issue.
All I want is to have the graph axis reset upon the end of some code.
Sheet1.ChartObjects("Chart 13").Chart.Axes(xlValue).MinimumScaleIsAuto = True
This is what I used along with several variations and I get the same error.
Method 'MinimumScaleIsAuto' of object 'Axis' failed.
OR Subscript out of Range
I can assure you that it is Chart 13, and on Sheet1.
Interestingly, if I record a macro doing what I want, then run the macro, I get the same error...
I'm not a specialist in VBA, but I have written a vba script for Excel, containg 12 modules with code, a lot of code. It's a very complex script but it's working fine. The problem is that it works only on my PCs. I tested it on 3 different PCs, with different Excel versions 2007, 2013 and 2016, and it works without any problem. But when I give the script to my colegues it gives errors.
Example:
This works for me:
Private Sub Test()
Workbooks("script").Sheets("Sheet1").Range("A:Z").ClearContents
......
It gives error Subscript out of range (Error 9) on other computers, I don't know why. If I change the code to:
Private Sub Test()
Set wb1 = ActiveWorkbook
wb1.Sheets("Sheet1").Range("A:Z").ClearContents
......
it works on other pcs too, but if I have to change the code this way, it will take a lot of time and most importantly, I can't test it if it works after the ammendments because it works for me anyway.
I found this thread VBA Subscript out of range - error 9 but I doesn't explain why It works for me on different Excel versions and it doesn't for other people.
Any help and suggestions will be appreciated.
It turned out that adding the file extension solves the problem as Tim suggested -
Workbooks("script.xlsx")
This way it works perfetcly on all PCs. It was something very simple, but I didn't know that.
Thanks again.