Merge cells in excel

Dear all,

I want to merge cells in an excel file.

I tried to invoke code that I took from excel after recording a macro.

But i get errors about not declaring in the code.

I can not find anywhere a solution.

I can not install belareva activities about excel in the machine, so I got to do it with code.

Could you please help? The code is the below:

Range("A1:C1").Select
Selection.NumberFormat = "@"
With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = True
End With
Range("D1:F1").Select
With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = True
End With

Did you record VBA and try to invoke it “Invoke Code” Activity?
Invoke Code starts only VB (for new version of UiPath, if Project is in C#, also C#).

You can create VBA (macro file) and run this from UiPath - Execute macro in Excel activities.

Hi @the.christopoulos

Try this

Thanks
Ashwin S

Yes, exactly. I recorded the VBA and then I user invoke code to run it.

He cannot install any packages

You can create VBA (macro file) and run this from UiPath - Execute macro in Excel activities.

Hi @the.christopoulos

you can use execute macro and pass the function name and try

Thanks
Ashwin S

@pllo2ptk @AshwinS2
I can not execute macros also because of security policy in the Organization.

I need to invoke vba code, but I do not know exactly how it should be. :frowning:

Does anybody know how to write vba code for invoking just to merge two different ranges of cells in the active excel which is already open , because I am inside excel application scope activity?

You can always create your own activity for merging files (write it in C#).
There is no possibility to invoke VBA code from the Uipath directly.

@pllo2ptk

Why not? Are you sure? Then, what is Invoke code activity for?

Invoke Code is for run Visual Basic (not Visual Basic for Application) code.
Anyway, you can try find guides how to manipulate excel file with VB.

Hi @the.christopoulos

Why don’t you use the merge activity on UiPath ?

@mz3bel Which one exactly is this ? I do not have any merge for excel. Only merge datatable

@the.christopoulos

Yes, you read the excel file and store your data on a datatable, than use merge to merge the data you want…

@mz3bel I just want to merge cells in excel, in order to make A1:C1 to show like 1 cell
Not in datatable.

will be like this:

Dim xl As Excel.Application = New Excel.ApplicationClass()
Dim wb As Excel.Workbook = xl.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorkshe, et)
Dim ws As Excel.Worksheet = CType(wb.ActiveSheet, Excel.Worksheet)
Dim range As Excel.Range = ws.get_Range(ws.Cells(1, 1), ws.Cells(1, 2))
range.Merge(True)

@bcorrea I put the code you mentioned and I get the below errors:

image

i imagined you had imported the namespace already, you need to add this import to your project:
Microsoft.Office.Interop.Excel

@bcorrea In the available namespaces for import there is not such namespace.

it does not begin with Excel… it is Microsoft.Office.Interop.Excel