[Solved] - How to get values from specify NameRange?

Hi, I want to know what is the fastest way to get all value from 2D by name range!
This is my original data:
1.Openworkbook -> getworkbook (wb)
2.Getworksheet (ws)
3.From "mynameRange" = "C1:F5" 

After reading this Post, I thought of this:
1. define firstrow, firstcol, rowcount, colcount from mynameRange(eg: Dim fistrow = ws.range(mynamerange).row,..)
Then Loop rowbyrow / colbycol and use:
2. print (ws (rowindex) (colindex).text)
but I'm not sure this is the fastest way or not? 
Thanks you!

3 Replies

SK Shamini Kiruba Sobers Syncfusion Team May 11, 2020 10:29 AM UTC

Hi Khanh Dang, 

Greetings from Syncfusion. 

You can get all the values in a named range only with its name, ant it is not necessary to define firstrow, firstcolumn and so on. Kindly look into the following code snippet which may fulfill your requirement. 

Code snippet: 

Using excelEngine As ExcelEngine = New ExcelEngine() 
    Dim application As IApplication = excelEngine.Excel 
    application.DefaultVersion = ExcelVersion.Excel2013 
    Dim workbook As IWorkbook = application.Workbooks.Open("Input.xlsx") 
    Dim worksheet As IWorksheet = workbook.Worksheets(0) 
 
    'Access a range by using defined name 
    worksheet.Names.Add("mynameRange", worksheet.Range("C1:F5")) 
    Dim namedRange As IRange = worksheet.Range("mynameRange") 
    For Each range As IRange In namedRange 
        Console.WriteLine(range.Text) 
    Next 
    Console.ReadKey() 
End Using 

We have also prepared a working sample with the above code, which can be downloaded from the following link. 

Kindly let us know if this helps. 

Regards, 
Shamini 



TG The GridLock May 11, 2020 08:14 PM UTC

Thanks Shamini,
This will help me coming!


SK Shamini Kiruba Sobers Syncfusion Team May 12, 2020 11:21 AM UTC

Hi Khanh Dang, 

We are glad that the provided solution helped you. Thanks for the update. 

Regards, 
Shamini 


Loader.
Up arrow icon