Need a code

Bomba
3StarLounger
Posts: 281
Joined: 20 Jan 2019, 19:43

Need a code

Post by Bomba »

Hi,
I have a folder named "May" with 31 workbooks named (1,2,3,4,....31). Each workbook has a sheet named "SKY".
I need a code to clear contents in cells range (A2: A10 ) of these sheets named "SKY".

Thanks in advance

User avatar
HansV
Administrator
Posts: 78466
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Need a code

Post by HansV »

Code: Select all

Sub ClearCells()
    ' Change path as needed; keep the \ at the end
    Const strFolder = "C:\Excel\May\"
    Dim strFile As String
    Dim i As Long
    Dim wbk As Workbook
    Dim wsh As Worksheet
    Application.ScreenUpdating = False
    For i = 1 To 31
        strFile = i & ".xlsm"
        Set wbk = Workbooks.Open(Filename:=strFolder & strFile)
        Set wsh = wbk.Worksheets("SKY")
        wsh.Range("A2:A10").ClearContents
        wbk.Close SaveChanges:=True
    Next i
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

Bomba
3StarLounger
Posts: 281
Joined: 20 Jan 2019, 19:43

Re: Need a code

Post by Bomba »

Thanks a lot, Master. :cheers: