+ Reply to Thread
Results 1 to 13 of 13

Loop through in folder, unlock VBAproject and Run Function

  1. #1
    Registered User
    Join Date
    11-08-2020
    Location
    London
    MS-Off Ver
    2016
    Posts
    4

    Exclamation Loop through in folder, unlock VBAproject and Run Function

    Hi,

    I need a VBA code which would loop through in a specified folder which contains around 1000 excel macro files. The code will open an excel file, unlock VBA Project with a password and run Function from specified Module from the opened file, and close the workbook. This code should do the looping of the same code in all 1000 files one by one.

    I really appreciate any help anyone can provide.

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Loop through in folder, unlock VBAproject and Run Function

    hmmmmm....sounds like a hacking effort to me. I'm sure you're not one though. the question isn't can you do this?, because that answer is probably. but rather the question is why would anyone ever have to do this?

  3. #3
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Loop through in folder, unlock VBAproject and Run Function

    Why not just run the function from an external file
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  4. #4
    Registered User
    Join Date
    11-08-2020
    Location
    London
    MS-Off Ver
    2016
    Posts
    4

    Re: Loop through in folder, unlock VBAproject and Run Function

    I don’t understand why someone would have not build a tool with password protected VBA project, later he wants to used a function from same workbook? the issue was that I never thought about I will need to do this(looping and unlock project and run a function).

    Sintek,
    That’s what I am doing problem here is locked VBAProject and it’s stopping me to run function.

  5. #5
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Loop through in folder, unlock VBAproject and Run Function

    here is locked VBAProject and it’s stopping me to run function.
    This I do not understand...If you are running code from an external file, why would this be an issue..

  6. #6
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

  7. #7
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Loop through in folder, unlock VBAproject and Run Function

    Quote Originally Posted by Frm123 View Post
    I don’t understand why someone would have not build a tool with password protected VBA project ...
    If you know the password that protects the vba module of these files, use "SendKeys" statement, otherwise this topic is not suitable for this forum, because the rules of this forum prohibit topics about breaking passwords.

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Loop through in folder, unlock VBAproject and Run Function

    Adam...Be careful...Rule 7...Contribution...
    Last edited by sintek; 11-08-2020 at 12:12 PM.

  9. #9
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Loop through in folder, unlock VBAproject and Run Function

    Quote Originally Posted by sintek View Post
    Adam...Be careful...Rule 7...Contribution...
    what? the password cracking links I posted? that's publicly available on the web. violation of policy here? let me read it....

  10. #10
    Registered User
    Join Date
    11-08-2020
    Location
    London
    MS-Off Ver
    2016
    Posts
    4

    Re: Loop through in folder, unlock VBAproject and Run Function

    Attachment 703222

    Thanks Guys, I know the password so I am not trying to hack.

    Sintek, That's what I thought as well, I should be able to call a function from another workbook without unlocking VBAProject. I tried my code again and it seems to work. but it failing certain occasion when file opened but macro is not enable. Do you have a fix for this?

    I have attached the screen shot of an error.

  11. #11
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Loop through in folder, unlock VBAproject and Run Function

    I meant...Place new code in blank workbook and run from there...Why don't you supply the code you are wanting to run on these 1000 files

  12. #12
    Registered User
    Join Date
    11-08-2020
    Location
    London
    MS-Off Ver
    2016
    Posts
    4

    Re: Loop through in folder, unlock VBAproject and Run Function

    I have provided the code below which I am running from the blank workbook. As I have said previously that, the code works fine when macro is enabled on the open workbook(from a specified folder). but it fails when workbook's macro is not enabled.






    Sub LoopAllExcelFilesInFolder()
    'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
    'SOURCE: WWW dot TheSpreadsheetGuru dot com

    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog

    'Optimize Macro Speed
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    'Retrieve Target Folder Path From User
    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

    With FldrPicker
    .Title = "Select A Target Folder"
    .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
    End With

    'In Case of Cancel
    NextCode:
    myPath = myPath
    If myPath = "" Then GoTo ResetSettings

    'Target File Extension (must include wildcard "*")
    myExtension = "*.xls*"

    'Target Path with Ending Extention
    myFile = Dir(myPath & myExtension)

    'Loop through each Excel file in folder
    Do While myFile <> ""
    'Set variable equal to opened workbook
    Set wb = Workbooks.Open(filename:=myPath & myFile)

    'Ensure Workbook has opened before moving on to next line of code
    DoEvents

    'Open the file and run the macro
    Set objExcel = CreateObject("Excel.Application")
    Application.Run wb.Name & "!Functions_ALL.Database"
    DisplayAlerts = False

    'Save and Close Workbook
    wb.Close SaveChanges:=True

    'Ensure Workbook has closed before moving on to next line of code
    DoEvents

    'Get next file name
    myFile = Dir
    Loop

    'Message Box when tasks are completed
    MsgBox "Task Complete!"

    ResetSettings:
    'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    End Sub

  13. #13
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Loop through in folder, unlock VBAproject and Run Function

    Your post does not comply with Rule # 2
    2. Programming code must be enclosed in code tags to improve readability. (A, Z)
    Please Login or Register  to view this content.
    So...Edit your post...Highlight the code and press the # button
    We can only assist further once this has been done…

    What I meant was...

    Put this entire code in blank workbook...Not in modules of each individual workbook...
    What is it this code is supposed to do anyway...
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to call a function in VBAProject(PERSONAL.XLSB)?
    By coffent in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-18-2020, 01:49 PM
  2. Replies: 1
    Last Post: 11-08-2018, 03:43 AM
  3. Get Value Function Loop through all files in folder and its subfolders
    By Cheeseburger in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-27-2016, 06:19 PM
  4. [SOLVED] Loop Through Folder, Create Emails with Sub Folder Names in Subject, Attach files in sub
    By Rschwar23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2015, 10:06 AM
  5. Replies: 12
    Last Post: 03-09-2015, 05:52 PM
  6. Replies: 1
    Last Post: 10-18-2014, 05:04 PM
  7. Folder lock & unlock in the server
    By sugavanam in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-02-2011, 09:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1