gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:59:37 26 4
gpt4 key购买 nike

通过 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com