在 UFT 中使用 VBscripting 读取 Excel 行的值并将其存储到 variables/arrays
Read and store the values from Excel rows to variables/arrays using VBscripting in UFT
通过 UFT,我试图从 Excel sheet 中读取行(可以是基于用户输入的任意数量的行)。我想将这些值(是字符串值)传递给另一个函数。
下面的代码在第 'fieldvalueChar(j-1) = ws.cells(j,1)'
行给出了下标超出范围的错误
Dim value
Dim lRow
Dim fieldvalue
Dim fieldvalueChar()
'Open the spreadsheet document for read-only access.
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("Path\Input.xlsx")
Set ws = objWorkbook.Sheets("Sheet1")
rowcount = ws.usedrange.rows.count
for j = 1 to rowcount
fieldvalueChar(j-1) = ws.cells(j,1)
next
MsgBox(fieldvalueChar(0))
MsgBox(fieldvalueChar(1))
Excel sheet 将始终只有一列,并且根据用户输入动态更改行数。我在网上看到一些 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,我试图从 Excel sheet 中读取行(可以是基于用户输入的任意数量的行)。我想将这些值(是字符串值)传递给另一个函数。
下面的代码在第 'fieldvalueChar(j-1) = ws.cells(j,1)'
行给出了下标超出范围的错误Dim value
Dim lRow
Dim fieldvalue
Dim fieldvalueChar()
'Open the spreadsheet document for read-only access.
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("Path\Input.xlsx")
Set ws = objWorkbook.Sheets("Sheet1")
rowcount = ws.usedrange.rows.count
for j = 1 to rowcount
fieldvalueChar(j-1) = ws.cells(j,1)
next
MsgBox(fieldvalueChar(0))
MsgBox(fieldvalueChar(1))
Excel sheet 将始终只有一列,并且根据用户输入动态更改行数。我在网上看到一些 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))
另一方面,您也可以尝试使用数据表。