PDA

View Full Version : How to correct this error



leal72
04-25-2019, 01:23 PM
Having trouble finding a way to work around this error "subscript out of range"
I think the error is happening because of the UBound(xx) value is 3 and the (n) value is 10. This function is part of a larger code that I am not very familiar with but hoping maybe there is a work around for this error.

24148

Leith Ross
04-25-2019, 02:06 PM
Hello leal72,

You have not declared the variable n in your code.

leal72
04-25-2019, 03:24 PM
thanks for reply. That's not it though, I declared it to test it out and same error

大灰狼1976
04-25-2019, 07:46 PM
Guess that the value of the n variable exceeds ubound(xx).

leal72
04-26-2019, 06:49 AM
Guess that the value of the n variable exceeds ubound(xx).

yes, this correct. Looking for a solution that will allow code to not error out

Paul_Hossler
04-26-2019, 07:43 AM
1. Pasting the code between CODE tags would make it easier for people to test

2. Are you sure you've converted the code correctly?




If x = xx(n) Then
j = 1
ElseIf x= xx(n) Then
j = n-1



seems odd. Pretty sure you meant





If x = xx(1) Then



https://www2.units.it/ipl/students_area/imm2/files/Numerical_Recipes.pdf

24151

leal72
04-26-2019, 08:45 AM
Thank you for the response

Might just be lost cause for me. Working on code for someone else, code writter no longer around to get feedback from. As code is now, it will process data for some sample sets but not for others, ie whenever the variable n value exceeds UBound(xx)

Made the change you suggested
If x = xx(1) Then gets me past this sticking point but creates another calculation issue down the line, run-time error 6, overflow
slope = (yy2 - yy1) / (xx2 - xx1) and the subscript out of range still exists
ElseIf x = xx(n) Then

Paul_Hossler
04-27-2019, 11:47 AM
Instead of trying to port C code to VBA, it might be better to just define what you want to do, and not how you want to do it