Lock the excel using script

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
Above code doesn’t locking the cells…
Please suggest on the above

I think you have missed to protect the sheet. You need to protect the sheet to lock the cells.
use syntax: ws1.protect “” '(inside double quotation you can give the password) before saving the workbook.

Hi @Demo_User

Use the below code to encrypt the excel file

Sub ProtectWorkbookWithPassword()
    Dim ws As Worksheet
    Dim wb As Workbook
    
    Set wb = ActiveWorkbook
    
    ' Replace "YourPassword" with the desired password
    wb.Protect Password:="YourPassword"
End Sub

Hope it helps!!