Consulting

Results 1 to 5 of 5

Thread: Solved: Make Code run only on xls files

  1. #1

    Solved: Make Code run only on xls files

    I have this sub below.....
    When I run this it runs on all files in the folder (including .zip files)
    Can I run this sub only on ".xls" files

    HTML Code:
    Sub Remove_password()
     
    Dim wkb1 As Workbook
    Dim wksMACRO As Worksheet
    Dim fso, f, fs, f1
    Dim FileExtStr As String
    Dim Filter As String
    '------Password's need to be entered in Sheet1
    Set Pass1 = Sheet1.Range("B1")
    Set Pass2 = Sheet1.Range("B2")
     
    Application.DisplayAlerts = False
     
     
    ThisWorkbook.Activate
     
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.getfolder(ThisWorkbook.path)
    Set fs = f.Files
     
     
    For Each f1 In fs
    If ThisWorkbook.Name <> f1.Name Then
    Set wkb1 = Application.Workbooks.Open(ThisWorkbook.path & "\" & f1.Name, , , , Pass1, True)
     
    wkb1.Password = Pass2
    wkb1.Save
    wkb1.Close
     
     
     
    End If
    Next
     
    MsgBox "Congratulations!!!" & vbCrLf & "All Files in the path have been updated successfully"
     
    Application.DisplayAlerts = True
     
    End Sub
     
    Kindly help

    Thx-n-BR

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    For Each f1 In fs
    If Right(f1.Name,3) = "xls" Then
    If ThisWorkbook.Name <> f1.Name Then

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Hi MD,

    Thx for the tip - I used your lines in the sub & the code is working fine.

    I had some time to figure out there was an "End If" missing - but everything is sorted out.

    Thanks for your help.

    Best regards

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try Smart Indent (see my signature) and always use Option Explicit. These make spotting errors much easier
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Thx MD for that suggestion, I have downloaded the tool "Smart Indent" Tool.

    BR

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •