Forum Discussion

mesg2anil's avatar
mesg2anil
Regular Contributor
13 years ago

Read Excel Rows and loop the Script

Hi,



Please find the script below, I want to read the excel file rows and columns and make the script run so many times depending on number of rows I have in the excel sheet. I need help on below script which I wrote, please suggest where I went wrong... If you have any sample code, please provide me.



'This function is to loop the records based on the below function "Function ReadLandAgentPay(Fname,Sname)"



Function ReadDataFromExcel()

Fname = "E:\\PropertyData.xls"

Sname = "Payment_Schedule"

CreateSCArr1 = ReadLandAgentPay(Fname,Sname)

For i = 1 to CreateSCArr1

s = ""

For j = 1 to 11

s = s + VarToString(Excel.Cells(i, j)) + "#"


Next

Next


ReadDataFromExcel = s


 



'Closes the driver

Call Excel.ActiveWorkbook.Save

Call Excel.Workbooks.Close

End Function


Excel.ActiveWorkbook.Save Excel.Workbooks.Close



'This will pass the excel loop info to the above function

Function ReadLandAgentPay(Fname,Sname)

Dim Driver, i

Set Driver = DDT.ExcelDriver(Fname, Sname)


RecNo = 0

CreateSCArr1 = CreateVariantArray(0, 0)


 



'Record Loop

While Not DDT.CurrentDriver.EOF


.CurrentDriver.EOF

 



'Column Loop

For i = 0 To DDT.CurrentDriver.ColumnCount - 1

PropNum = PropertyCount


i = 0 .CurrentDriver.ColumnCount - 1PropNum = PropertyCount

 



'Iterates through the properties



For j = 0 to (PropNum - 1)

PropName = Properties(j).Name


Next

Next


VarArrayRedim CreateSCArr1, RecNo

RecNo = RecNo + 1


 


DDT.CurrentDriver.Next()

WEnd


 



'Closing the driver



Call DDT.CloseDriver(Driver.Name)

Log.Message("Total Number of Agent Payments created: "& RecNo)

ReadLandAgentPay = CreateSCArr1



End Function


3 Replies

  • Hi,



    Can you clarify your problem? What issue do you have with your script exactly?

    Also, is there really a reason for mixing COM and DDT in a single script? And if you really need to use COM here, where do you obtain Excel's COM object (initialize the 'Excel' variable) in your script?
  • mesg2anil's avatar
    mesg2anil
    Regular Contributor
    Hi Jared,



    Thank you for the reply.

    I know I'm using both COM and DDT to read the excel file and input the data to the IE application. Do you have any example script on how to read number of rows from the excel sheet and run the application so many times to input the data from the excel to application?

    My requirement is...

    1. If I have two records in the excel sheet

    2. Two records should get created in the application

    3. Either DDT or COM way, I want to do the above stuff.

    Could you please give me one sample / example script if you have?
  • Hi,



    There's an example in the Using DDT Drivers. DDT was designed specially for such tasks as yours, it always reads files row by row and uses data it obtains from each row in a loop.