How to Lookup For a Range in Excel wothout using Excel scope

Hi All,

I want to lookup for a value and return the cell reference without using Excel Scope and also i dont want to use Datatable.Please Let me know if its possible!!!

@Aswini,

No you can’t achieve this without datatable. Any reason for not using the datatble?

I think it’s possible to solve this issue without using Excel Application Scope and DataTable. One alternative could be leveraging the “Invoke Code” activity in UiPath along with Excel Interop library. This method allows you to directly open the Excel file, search for a specific value, and then return the cell reference where it’s found. This way, you don’t have to load the entire worksheet into memory or use DataTables, which can be particularly useful when you’re trying to reduce resource usage or simplify the automation process. Just keep in mind that to use this approach, Excel must be installed on the machine running your automation.

1 Like

Yeah i want to know the cell reference value.We are getting errors with excel scope after upgrading to Microsoft 365 Apps.So we are creating a reusable code for excel functionality without using svope activities.For Read and write we used Workbook activities but for lookup had to look for another approach

Which code can be used for Invoke Code activity ?

I’m not exactly sure what you were trying to do, but I’ve prepared this code for you. Below is the VBA code, a screenshot from Studio, and a view of the test table in Excel. It searches for a Name and returns the cell address.


I typed “Marian” as the input, and it gave me “B2” as the result.

Below are the arguments needed to use the code:




I hope this helps with your automation in some way.

1 Like

Thank You so much and really appreciate your patience.This is what i am looking for let me try this

Do you want me to send you the VBA code?

Yeah but one doubt do we have to enable the workbook for macros?

@Aswini

one thing you can do is to use excel as DB and query the records, then calculate the cell number using the value

Is it some question someone asked? I dont see any reason not to read data using workbook and then use datatable for lookup

cheers

Not really, because the macro is triggered from UiPath, so it can be any workbook.

Why not try to resolve those errors? Excel Scope and all the activities work just fine if configured properly, and would quickly and easily achieve your goal.

You can also use invoke code or codedworkflows and ClosedXML (it comes as sub dependecy from UiPath.Excel.Activities)

Main advantage over Excel.Interop is that it doesn’t require to have excel installed on the machine and you don’t have to worry about releasing com objects and checking if excel is ‘stuck’ in background

I’ve written two methods below, hope this helps:

Dim file As String = "path to your xlsx file"
	
Dim searchText As String = "the value you want to search for"
Dim foundAddress As String = ""
	
Dim workbook As XLWorkbook = New XLWorkbook(file)
Dim worksheet As IXLWorksheet = workbook.Worksheets(0)
	
	
'// Code v1 where we know the in which column to search
Dim columnIndex As Integer = 1 'change this to your column index

For Each row As IXLRow In worksheet.RowsUsed()
		
	If row.Cells().ElementAt(columnIndex).ToString("V") = searchText Then
		foundAddress = row.Cells().ElementAt(columnIndex).ToString("A")
		Exit For
	End If
Next
	
Console.WriteLine(String.Format("Found value: {0} in cell: {1}", searchText, foundAddress))
foundAddress = "" 'reset reference for second method

'// Code v2 where we don't know the column index
Dim searchCellFunction As Func(Of IXLCell, Boolean) = Function(cell As IXLCell) cell.ToString("V") = searchText

For Each row As IXLRow In worksheet.RowsUsed()

	If row.Cells().Any(searchCellFunction) Then
		foundAddress = row.Cells().First(searchCellFunction).ToString("A")
		Exit For
	End If
Next
	
Console.WriteLine(String.Format("Found value: {0} in cell: {1}", searchText, foundAddress))

can you please send me the code?

Please find below the code used in the Invoke Code activity.

vba.txt (1,9 KB)

You have to add ‘ClosedXML.Excel’ to imports in the workflow.

I’m attaching the whole project (FYI it’s with the latest Studio)
BlankProcess8.zip (113.8 KB)

Thank You So Much Tried this and its giving me the output. But incase if we dint have any specified range this might not work isnt it ?

Hello TQSM for your response i did import ClosedXML.Excel but getting below error

Use only method 1 if you know the column or use method 2 if you want to search in all the cells/rows from a sheet

You could also adapt to search in all the sheets.

Check if you have System.Linq in imports in that workflow, that’s where the method ElementAtOrDefault comes from.