Invoke vba - delete last rows

HI. I’m new, and I’m doing my first automation. I can’t share the code, but what I need to do is to delete some useless rows in an Excel file at the end of it. Basically, I have a report with some rows of date. I copy these rows in another file used as template with some formulas in the columns AR-BB. Unfortunately, my template with the formulas has more rows (because it’s used for more reports) than the report itself. I want to delete these final rows with useless formulas. I want to use a macro.

The code is:
Sub eliminaRighe()
Worksheets(“Sheet1”).Activate
Dim lastRow As Integer
lastRow = ActiveSheet.UsedRange.Rows.Count
Do While IsEmpty(Range(“A” & lastRow).Value)
Range(“a” & lastRow).EntireRow.Delete
lastRow = lastRow - 1
Loop
End Sub

It works fine as macro in Excel, but it doesn’t work (it doesn’t do anything) when I invoke it through UiPath. I invoke it inside an “excel scope application” activity I specify the path and the name “eliminaRighe”. Can someone help me?

Hi @massimilianolorenzin

welcome them to our community!

Please try this

Sub eliminaRighe()
   Sheets(“Sheet1”).select
   Dim lastRow1 As Integer
   Dim lastRow2 As Integer
   lastRow1 = cells(rows.count,1).end(xlup).row 
   lastRow2 = cells(rows.count,44).end(xlup).row 
   Range(cells((lastRow1+1) ,1),cells(lastRow2,54)).clearcontents
End Sub

Thanks

Hi. Thank you for answering. I tried your code, like mine it works if I use it manually, but when I use it as macro in uiPath It doesn’t do anything. I mean, I put a msgBox and I saw that UiPath runs the macro because it creates the message box but it looks like it doesn’t save the modifies. Could it be this the problem?

It will automatically save let’s try this also

Sub eliminaRighe()
   Sheets(“Sheet1”).select
   Dim lastRow1 As Integer
   Dim lastRow2 As Integer
   lastRow1 = cells(rows.count,1).end(xlup).row 
   lastRow2 = cells(rows.count,44).end(xlup).row 
   Range(cells((lastRow1+1) ,1),cells(lastRow2,54)).clearcontents
   Activeworkbook.save
End Sub

Hi @massimilianolorenzin
Use Invoke vba activity and save the below code in the text file and the call text file in the invoke vba activity.

Sub DeleteRow()

Dim LastRow As Integer

      LastRow = Worksheets("Sheet1").Range("AA" & Rows.Count).End(xlUp).Row
      Worksheets("Sheet1").Rows(LastRow & ":" & LastRow).Delete shift:=xlUp

End Sub

Please refer the screen shot.

Thanks

2 Likes

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