How to merge the cell with LINQ and UiPath?

I am unable to find the solution for merging the cell by LINQ and as well in UiPath.
Check the data in a specific column(“ID”), and if they are the same, please merge the cells.

This is the current form

ID Code
A AB-776
A ABC-123
B BBB-666
B DDD-8888

Output…
I am unable to fully convey the image I want.

ID Code
AB-776
A ABC-123
B BBB-666
DDD-8888

Just need the solution in LINQ or UiPath.
Thanks in advance!

HI,

How about the following using ClosedXML? This assumes ID column is sorted in advance.

Using wb As New ClosedXML.Excel.XLWorkbook(filename)
Dim sheet As ClosedXML.Excel.IXLWorksheet = wb.Worksheet(sheetname)
Dim previous As String =""
Dim r As Int32 =1
Dim c As Int32 =targetColumnIndex
Dim cLetter As String = UiPath.Excel.Helpers.ExcelUtilities.ConvertColumnIndexToColumnLetter(c)
Dim startRow As Int32 =1
Dim mergeHeight As Int32 =1
While(True)
Dim cell As  ClosedXML.Excel.IXLCell  = sheet.Cell(r,c)
If (cell.Value.ToString<>previous) Then
	If mergeHeight>1 Then
		Dim range As ClosedXML.Excel.IXLRange = sheet.Range(cLetter+startRow.ToString+":"+cLetter+(startRow+mergeHeight-1).ToString)
	    	range.Merge()
	End If
    mergeHeight =1
	
    startRow=r
	r=r+1
	previous =cell.Value.ToString
Else
	r=r+1
    mergeHeight =mergeHeight+1		
End If
If(cell.Value.ToString="") Then Exit While
End While
wb.Save()
End using

Sample
Sample20240625-1.zip (9.9 KB)

Regards,

1 Like

Thank you very much for your response.
However, it displays an error message that the document is invalid.

Hi,

Does the above sample work? or throw error?
If you use another input file, can you share it? It’s no problem dummy data if the issue can be reproduced.

Regards,

I encountered the aforementioned issue when using the file you provided after extracting it.

Hi,

What version of Studio do you use? The above is created in Studio 23.10.3

Regards,

My software version is too old.
But I can’t update it at work. No worries, I’ll try to find another way

I got your situation. The following step may help you.

  1. Create a new project.

  2. Put InvokeCode activity then open editor.

  3. Copy the above code to the editor and set argument as the following image.

  1. Put Assign activity to set filename, sheetname etc.

Regards,

1 Like

I found the article you previously shared, which resolved the system version issue. Thank you.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.