1
  • Do While Loop needs to exit when my Array hits it's UBound

I receive the Subscript Out of Range error when my loop attempts to be greater than it's size.

arrNo = 1

        strScreenList = PbWindow("w_genapp_frame").PbWindow("Application Enquiry Menu").PbList("lb_list").GetContent
        arrScreen = Split(strScreenList, VbLf, -1, 1)
        
        Do While arrScreen(arrNo) <> ""
            
            PbWindow("w_genapp_frame").PbWindow("Application Enquiry Menu").PbList("lb_list").Activate arrScreen(arrNo)
            strScreenName = arrScreen(arrNo)
            sf_EnquireScreens(strScreenName)
            
        arrNo = arrNo+1
        Loop

As you can see, strScreenList is split into strings to arrScreen (Which contains 36 values)

Once my loop attempts to step over arrNo(35) (this is the point at which I would like it to step out of my loop) I receive the Subscript Out of Range error

From what I've gathered, I'm receiving the error because my Array is too short for the extra value it's trying to add to it.

Is there a way to gracefully get my script to step out once my array hits it's Ubound?

6
  • I should note that the reason I've set my comparison to "" is that I was hoping Array No 36 will be an empty string.. thus it would step out. I have attempted with a Ubound comparison, but still see the same Subscript error Commented Sep 28, 2021 at 11:33
  • 2
    Array(36) does not exist. That's why the error. Please, try Do While arrNo <= Ubound(arrScreen). It could be used as you tried, only if you are sure that there are empty strings/elements in the array and need exiting in such a case. But, in that way, without an empty element, the loop will continuously iterate, until the error appears... Commented Sep 28, 2021 at 11:37
  • 1
    Or just for ... lbound ... to ubound ... Commented Sep 28, 2021 at 11:46
  • @FaneDuru You're right, there aren't any empty strings. I had assumed that the arr+1 would add an empty string at the end of my original array. I have just tested your solution and it worked!! Very simple and slightly irritating i hadn't thought of that myself. Thanks so much for the help Commented Sep 28, 2021 at 11:49
  • 1
    I understand that Split separates my string into the desired amount of elements. I had just made the mistake of assuming ArrNo+1 would add an empty string when it got to the end. Anyway, all sorted now. Thanks again @FaneDuru Commented Sep 28, 2021 at 11:55

1 Answer 1

0

Answered by @FaneDuru

Array(36) does not exist. That's why the error. Please, try Do While arrNo <= Ubound(arrScreen). It could be used as you tried, only if you are sure that there are empty strings/elements in the array and need exiting in such a case. But, in that way, without an empty element, the loop will continuously iterate, until the error appears.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.