How to read complete Excel sheet into Multi-Dimensional dictionary objects


Following is the sheet that need to be fetched into the Multi-Dimensional dictionary object

'Excel File 








'Function
'========================================================================
' Description:  This function will read the entire excel sheet and capture the sheet into multidimensioanl dictinoary object
'Dependencies: First row of the sheet should be the unique identifier
' Input: xlFilePath: Excel File which is to be read by the script
' xlSheetName: SheetName which is to be read by the script
'Output: The objDict will be returned
'========================================================================

Public Function Func_ReadXL_IN_Dictionary(xlFilePath,xlSheetName)


' open and read excel file
Dim objXL,objWrkBk,objWrkSht
Dim objDict

Set objXL=CreateObject("Excel.Application")
Set objWrkBk=objXL.Workbooks.Open(xlFilePath)
Set objWrkSht=objWrkBk.Worksheets(xlSheetName)

Set objDict=CreateObject("Scripting.Dictionary")

' copy all excel data into the dictionary object
 Dim rCount,cCount ' get the row and column count of the worksheet
rCount=objWrkSht.usedrange.rows.count
cCount=objWrkSht.usedrange.columns.count

'Copy all Data cell by cell
Dim row,col
For row =2 to rCount
objDict.Add objWrkSht.cells(row,1).value,CreateObject("Scripting.Dictionary")
For col=1 to cCount
          objDict (objWrkSht.cells(row,1).value).add objWrkSht.cells(1,col).value,                    objWrkSht.cells(row,col).value
Next
Next          

set Func_ReadXL_IN_Dictionary=objDict
'Release objects
objWrkBk.Close
objXL.Quit
Set objWrkSht= Nothing
Set objWrkBk=Nothing
Set objXL=Nothing
Set objDict=Nothing

End Function



' USE THIS FUNCTION AS FOLLOWS


Dim xlFilePath:xlFilePath="D:\...\MasterFile.xlsx"
Dim xlSheetName:xlSheetName="MasterFile"
Dim objDict: objDict=objMasterFile

Dim objMasterF
set objMasterF = Func_ReadXL_IN_Dictionary (xlFilePath,xlSheetName)
a=objMasterF.keys
b=objMasterF.items

For i=0 to objMasterF.count-1
print a(i)        
c=objMasterF(a(i)).items
For j=0 to objMasterF(a(i)).count-1
print c(j)
Next
Next


'OUTPUT




Comments

Popular posts from this blog

Arrays

What is the difference between eval, execute and executeglobal.

Sample Test Strategy for a MicroService Project with APIs only