Open files in a folder and format

Halley yenn

New Member
Joined
Mar 17, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

I have 10k excels in a folder where I have to open each excel and Lock Columns : Col C , Col D, Col E, Col F, Col G, Col H, Col I , Col J , Col K, Col L, Col M, Col N and apply password say "GRUG" and make Col A and Col B editable or Unlocked.

The no of rows in each column will be around 20k rows and I recieve 10k files on weekly basis.

Any macro to open each excel and format accordingly will be appreciated.

Thanks
H
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi Team,

I have 10k excels in a folder where I have to open each excel and Lock Columns : Col C , Col D, Col E, Col F, Col G, Col H, Col I , Col J , Col K, Col L, Col M, Col N and apply password say "GRUG" and make Col A and Col B editable or Unlocked.

The no of rows in each column will be around 20k rows and I recieve 10k files on weekly basis.

Any macro to open each excel and format accordingly will be appreciated.

Thanks
H
is this for all sheets in the workbook or just one sheet?
 
Upvote 0
Hi,

These are different workbooks. like 10k workbooks in a folder
yes i know there are 10k workbooks but is it for every worksheetsheet in the workbook or for just 1 sheet in each workbook
 
Upvote 0
The below may work for you IF the following is correct
1) there is only 1 sheet in the workbook
2) in ColA-ColN there are no merged cells
3) the worksheets are not already password protected

I suspect it will be very slow with the number of files
VBA Code:
Option Explicit
Option Compare Text
Sub CellLocking()

  Dim FSO As Object, MyFolder As Object
  Dim fname As String, fpath As String, pwrd As String
  Dim wb As Variant
  Dim ws As Worksheet
   
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual
       
  pwrd = ("GRUG")
  fpath = "enter you folderpath here" '  change as required
 
  Set FSO = CreateObject("scripting.FileSystemObject")
  Set MyFolder = FSO.GetFolder(fpath)
 
  For Each wb In MyFolder.Files
   
     If wb.name Like "*.xl*" Then
        Set wb = Workbooks.Open(fpath & "\" & wb.name)
        For Each ws In wb.Worksheets
         
           ws.Range("A:B").Locked = False
           ws.Range("C:N").Locked = True
           ws.Protect (pwrd)
         
        Next ws
           
     End If
     wb.Save
  Next wb
     
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Solution
The below may work for you IF the following is correct
1) there is only 1 sheet in the workbook
2) in ColA-ColN there are no merged cells
3) the worksheets are not already password protected

I suspect it will be very slow with the number of files
VBA Code:
Option Explicit
Option Compare Text
Sub CellLocking()

  Dim FSO As Object, MyFolder As Object
  Dim fname As String, fpath As String, pwrd As String
  Dim wb As Variant
  Dim ws As Worksheet
  
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual
      
  pwrd = ("GRUG")
  fpath = "enter you folderpath here" '  change as required
 
  Set FSO = CreateObject("scripting.FileSystemObject")
  Set MyFolder = FSO.GetFolder(fpath)
 
  For Each wb In MyFolder.Files
  
     If wb.name Like "*.xl*" Then
        Set wb = Workbooks.Open(fpath & "\" & wb.name)
        For Each ws In wb.Worksheets
        
           ws.Range("A:B").Locked = False
           ws.Range("C:N").Locked = True
           ws.Protect (pwrd)
        
        Next ws
          
     End If
     wb.Save
  Next wb
    
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Hey ,
Thanks for the code , as you said it is too slow but working well.

KR,
H
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,749
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top