Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用UFT中的VBScript读取Excel行中的值并将其存储到变量/数组中_Excel_Vbscript_Qtp_Hp Uft - Fatal编程技术网

使用UFT中的VBScript读取Excel行中的值并将其存储到变量/数组中

使用UFT中的VBScript读取Excel行中的值并将其存储到变量/数组中,excel,vbscript,qtp,hp-uft,Excel,Vbscript,Qtp,Hp Uft,通过UFT,我试图从Excel工作表中读取行(根据用户输入可以是任意数量的行)。我想将这些值(字符串值)传递给另一个函数 下面的代码在“fieldvalueChar(j-1)=ws.cells(j,1)”行给出了下标超出范围的错误 Excel工作表将始终有一列,并根据用户输入动态更改行数。我在网上看到一些VBA代码,但没有VBS。这是因为您没有初始化阵列。你可以试试这样的 Dim value Dim lRow Dim fieldvalue ReDim fieldvalueChar(1) ' J

通过UFT,我试图从Excel工作表中读取行(根据用户输入可以是任意数量的行)。我想将这些值(字符串值)传递给另一个函数

下面的代码在“fieldvalueChar(j-1)=ws.cells(j,1)”行给出了下标超出范围的错误


Excel工作表将始终有一列,并根据用户输入动态更改行数。我在网上看到一些VBA代码,但没有VBS。

这是因为您没有初始化阵列。你可以试试这样的

Dim value 
Dim lRow
Dim fieldvalue
ReDim fieldvalueChar(1) ' Just Declare an array of 1, Since array size has to be constant when declaring 

'Open the spreadsheet document for read-only access.
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\saravananp\Desktop\Items.xls")

Set ws = objWorkbook.Sheets("Items")
rowcount = ws.usedrange.rows.count

' Redefine array size dynamically based on number of Rows 
ReDim fieldvalueChar(rowcount)

for j = 1 to rowcount
    fieldvalueChar(j-1) = ws.cells(j,1) 
next


MsgBox(fieldvalueChar(0))
MsgBox(fieldvalueChar(1))

另一方面,您也可以尝试使用数据表

你有没有机会尝试一下,UFT的数据表功能还没有尝试过我的乐趣。此外,当访问不存在的数组元素时,会抛出超出范围的下标。例:如果数组大小为5,如果访问6,则会出现该错误。在您的情况下,您的大小是0,因此如果您尝试访问任何索引,就会出现该错误。如果您想了解更多详细信息,请选中此项:
Dim value 
Dim lRow
Dim fieldvalue
ReDim fieldvalueChar(1) ' Just Declare an array of 1, Since array size has to be constant when declaring 

'Open the spreadsheet document for read-only access.
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\saravananp\Desktop\Items.xls")

Set ws = objWorkbook.Sheets("Items")
rowcount = ws.usedrange.rows.count

' Redefine array size dynamically based on number of Rows 
ReDim fieldvalueChar(rowcount)

for j = 1 to rowcount
    fieldvalueChar(j-1) = ws.cells(j,1) 
next


MsgBox(fieldvalueChar(0))
MsgBox(fieldvalueChar(1))