Sunday, December 18, 2022

RPA - Excel Manipulations - Deleting Rows - Except Header to last Used Row.

RPA - Excel Manipulations -  Deleting Rows  - Except Header to last Used Row.


'Declare Excel Application Object
Set oExcel = createobject ("Excel.Application")

'Make it Visible False .. So that excel won't appear in screen 
oExcel.visible=False

'Assign Display Alerts False.. So that excel won't give any alert while running
oExcel.DisplayAlerts=False 

'Create Excel Work book object
Set oWorkBook-=oExcel.Workbooks.open ("C:\Siva\Template.xlsx") ' Open a Template Excel from C:\Siva Folder

'Create a Excel Sheet object to use the Sheet1 
Set oSheet=oWorkBook.Worksheets ("Sheet1")

'Active the Sheet using the Sheet Object
oSheet.Activate

'Initialize the LastRow variable value to 1   

LastRow=1
' To get the Last Row Value
LastRow=oSheet.Cells.SpecialCells (11).Row

' Assign the Delete Range from A2 to last Row No of A Column
DeleteRange="A2:A" & LastRow

MsgBox " Last Row : " & LastRow & " Delete Range : " & DeleteRange

' i.e., Used Row Numbers are greater than 1 then Delete the Rang of Rows used
If LastRow > 1 Then
  oSheet . Range (DeleteRange) . EntireRow. Delete
Else
    'Msgbox "Only Header Available"
End IF
'To Verify all rows are cleared using UsedRange again
UsedRange=oSheet . UsedRange . Address (xlR1C1)
UsedRow=oSheet.UsedRange.Rows.Count
MagBox "Used Range: " & UsedRange & "used Row:" & UsedRow

'Now the Save, Close the workbook  and Cleaup the all the objects used
oWorkBook. Save
oExcel.DisplayAlerts=True
oWorkBook.Close
oExcel.Quit
Set oSheet=Nothing
Set oWorkBook=Nothing
Set oExcel =Nothing

No comments:

Post a Comment