I need to protect some cells in excel using UiPath/Macros

I have one Input Excel file where I’m getting data for my process. Every time I read each row
I need to update status column as well.
After completion of my process I need to protect the cells which are updated in status column.
I’m unable to protect those cells. Can anyone help to resolve this issue?

Hi @Syed_Javeed ,

We can use VB code to protect the range of cells in excel.

  1. Use invoke code activity and paste the code into the activity and make sure that you have imported microsoft.office.interop.excel namespace.

  2. You have to use dynamic row count to use specific range for that you can store your final excel which is having status into data table using workbook read range activity. and after that calculate row count using YourDT.rows.count and save it in a variable.

YourDatatablerowcount = YourDT.rows.count.tostring

i am not sure on the column reference so i have used A for sample you can change whtever column you want. please refer the code and let us know. thanks.

VB code for protecting range of cells.

Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb1 As Microsoft.Office.Interop.Excel.Workbook
Dim ws1 As Microsoft.Office.Interop.Excel.Worksheet
excel = New Microsoft.Office.Interop.Excel.ApplicationClass
wb1 = excel.Workbooks.Open(Workbook1CopyFrom)
ws1 = CType(wb1.Sheets(Workbook1CopyFromSheet), Microsoft.Office.Interop.Excel.Worksheet)
ws1.Range(“A2:A”+YourDatatablerowcount).Locked=True
wb1.Save
wb1.Close
excel.Quit
ws1 = Nothing
wb1 = Nothing
excel = Nothing
GC.Collect