通过 UFT,我尝试从 Excel 工作表中读取行(可以是基于用户输入的任意数量的行)。我想将这些值(字符串值)传递给另一个函数。
下面的代码在“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 工作表始终只有一列,并根据用户输入动态更改行数。我在网上看到了一些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))
另一方面,您也可以尝试数据表。
关于excel - 使用 UFT 中的 VBscripting 读取 Excel 行中的值并将其存储到变量/数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41097471/