Micro Focus QTP (UFT) Forums
Subscript out of range - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Subscript out of range (/Thread-Subscript-out-of-range)



Subscript out of range - vijayendra.shukla - 06-11-2010

Hi,

I am having trouble retreiving and then updating data from an array.

Here is my problem:
I am trying to get data from a WebTable cell. The data is a date with format "6/11/10 4:35 AM". Now I will have to split this data to get the date value "6/11/10".
Since the value I have to use should be in format of mm/dd/yyyy format so for that i thought of splitting the first index and concatenating the yy value with "20". But thats when I get the error "Subscript out of range: 'sToDate'".

Here is the line of code that I am using:

Code:
sToDate = Split(Browser("ABC").Page("ABC").Frame("fraMgr").WebTable("Table").GetCellData(2, 1), " ")
sToDate(0) = Split(sToDate(0), "/")
sToDate(0,2) = "20" & sToDate(0,2)
The sToDate value is "6/11/10 4:35 AM"

I am getting error in the last line. I even tried checking the value of sToDate(0,2) element but got the same error.
When I saw the value in Watch window, it is perfectly showing me it's value as "10". Attaching screenshot for the same.

PS - My problem is not getting the date in desired format as i know that can be done using CDate function. And in fact i have already done that. But I am not able to understand why I am getting the error for above logic. So this is just to solve my curiosity Smile.


RE: Subscript out of range - guin.anirban - 06-11-2010

Split function will return only zero based one dimensional array. But in 3rd line you are trying to assign the value to a two dimensional array. You can try the following:

Code:
sToDate = Split("6/11/10 4:35 AM", " ")
MsgBox sToDate(0)
sToDate(0) = Split(sToDate(0), "/")
MsgBox sToDate(0)(2)
sToDate(0)(2) = "20" & sToDate(0)(2)
MsgBox sToDate(0)(2)



RE: Subscript out of range - vijayendra.shukla - 06-11-2010

Guin, Thanks a lot for clarification, this works.

Actually I was trying the two dimensional array because in Watch window I saw the notation QTP used for that element of array which is like "sToDate(0,2)" (this you can see in the attached doc as well).


RE: Subscript out of range - jsknight1969 - 06-11-2010

As an alternative you can use the built in date functions.
Code:
mydate = "6/12/10 10:23 AM"
mymonth = Month(mydate)
myday = Day(mydate)
myyear = Year(mydate)

msgbox "Month: " & mymonth &vbcrlf & "Day: " & myday & vbcrlf &  "Year: " & myyear

No need for multidimensional arrays then.