【问题标题】:Looping values from excel sheet in UFT在 UFT 中循环来自 excel 工作表的值
【发布时间】:2014-10-16 08:54:05
【问题描述】:

谁能帮帮我!!!!

我有这个函数可以从外部 excel 工作簿调用数据并将值放入被测应用程序中,我需要它逐行遍历 excel 工作表中的所有值

下面是框架的链接以及我正在尝试使用的函数,并且需要它循环遍历 Excel 工作表中的值。请帮忙。

非常感谢

http://www.automationrepository.com/2013/08/designing-hybrid-framework-in-qtp-part-3/

'================================================================================
'Function name - fnFetchDataFromExcelSheet
'Description - This function retrieves the data from the excel sheet based upon the column name
'================================================================================
Function fnFetchDataFromExcelSheet(strColumnName)

    Dim sColValue

    'Initialize the return the value to "empty" string
    fnFetchDataFromExcelSheet = "empty"

    'Add a new blank sheet into the QTP data table
    'Excel data will be copied into this blank sheet
    DataTable.AddSheet("dtDataSheet")

    'Import the data from the excel sheet into the QTP data table
    DataTable.ImportSheet sExcelWorkbookPath, sSheetName, "dtDataSheet"

    'Find the value from the data table
    sColValue = DataTable.Value(strColumnName, "dtDataSheet")

    'Return the value back to the calling function
    fnFetchDataFromExcelSheet = sColValue

    'Remove Reference
    DataTable.DeleteSheet("dtDataSheet")

End Function

【问题讨论】:

    标签: vba excel qtp


    【解决方案1】:

    您从网站发布的此功能在逻辑上不正确!

    首先,您需要将 sSheetName 和 strColumnName 都传递给函数。

    即使我们错误地忽略了它是一个未命中或假设 sSheetName 是一个全局变量(因此需要将其传递给函数),对于每次调用此函数,它都会将工作表导入数据表,获取给定单元格的第一行中的值,然后再次删除它(!!!)。它会非常严重地影响性能。每次你也会得到相同的价值!!

    我会说,忽略函数。请按照以下步骤操作。

    1) 首先您需要将 excel 导入数据表中。

    DataTable.ImportSheet /path/to/excel, /name/of/the/sheet, "dtDataSheet"
    

    /path/to/excel & /name/of/the/sheet 应该在 " "

    2) 获取行数

    intRowcount=DataTable.GetSheet("dtDataSheet").RowCount
    

    3) 使用简单的 For 循环

    For iLoop = 1 To intRowcount
    
      DataTable.GetSheet("dtDataSheet").SetCurrentRow iLoop
      Msgbox DataTable.value("ColumnName","dtDataSheet")  'Replace the msgbox with what you want
    
    Next
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      相关资源
      最近更新 更多