Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cast Excel Data into a 2-D Array
#1
Solved: 11 Years, 3 Months, 3 Weeks ago
Here is an example to cast an excel range to a 2D- array
Code:
Call ReadExcel (sFileName,sSheetName)
Function ReadExcel(sFileName,sSheetName)                                            
   Dim oExcel                                                                                
   Dim oRange                                                                                
   Dim arrRange                                                                              
                                                                                            
    'Open the file and set the sheet                                                        
    'On Error Resume Next                                                                    
       Set oExcel = CreateObject("Excel.Application")  
       oExcel.Visible=True
           oExcel.Workbooks.Open "C:\Documents and Settings\testingsvs_qa\Desktop\Book1.xls"  
           oExcel.Workbooks(1).Activate                  
      Set oRange = oExcel.Worksheets(sSheetName).UsedRange                              
                                                
                                                                                            
      If Err.Number <> 0 Then                                                                
         ReadExcel = Array("Error")    
         msgbox Err.Description                                                    
         Exit Function                                                                      
      End If                                                                                
   'On Error Goto 0                                                                          
                                                                                            
   'Cast excel data into a two-dimentional array                                            
   arrRange = oRange.Value                                                                  
                                                                                            
    oExcel.WorkBooks.Close    
     Set oRange = Nothing                                                                                                                    
     oExcel.Quit                                                                              
     Set oExcel = Nothing                                                                      
                                                                                              
     'return the arrRange and then return to the function                                      
     ReadExcel = arrRange                                                                      
                                                                                              
      End Function
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search and import data from excel under some rules. nwpulele 2 2,904 02-19-2015, 04:10 AM
Last Post: supputuri
  How to populate data in weblist using excel vijifun 1 3,298 12-05-2013, 12:06 PM
Last Post: basanth27
  XML Data read and extract output in an array SweetyChowdhury 9 7,734 05-10-2013, 03:05 PM
Last Post: SweetyChowdhury
  Does QTP support writing runtime data to multiple sheets in excel Shwethareddy 2 3,667 10-16-2012, 12:20 PM
Last Post: Shwethareddy
  Send Data from MSWord document 2 Excel File kisskasa 0 2,849 01-10-2012, 01:51 PM
Last Post: kisskasa

Forum Jump:


Users browsing this thread: 1 Guest(s)