+ Reply to Thread
Results 1 to 5 of 5

[SOLVED]:Opening workbooks and enabling user defined macros to run in those workbooks

  1. #1
    Registered User
    Join Date
    02-21-2006
    Posts
    27

    [SOLVED]:Opening workbooks and enabling user defined macros to run in those workbooks

    Hi

    I have a piece of VBA code residing in workbook A which scans the contents of a different directory looking
    for .xlsm workbooks to process and updates the contents of an existing workbook B.

    All the .xlsm workbooks to be processed are of the same format and structure with multiple tabs. Each tab
    contains user defined macros which are stored in the Module of each .xlsm workbook.

    The VBA code uses workbooks.open() to open each .xlsm workbook for processing. Running the workbook A VBA code in debugging mode, I noticed that for each .xlsm workbook that is opened for processing, the user defined macros in each of the tabs are shown as #NAME?.

    This causes the processing VBA code to fail when processing each .xlsm workbook.

    Opening those .xlsm workbooks from windows explorer and clicking on 'Enable Macro" do not produce the #NAME? error.

    I inserted a workbook Activate statement in workbook A VBA code immediately after the workbooks.open() statement but to no avail. From the user help guide, it says that workbooks open by workbooks.open() will have macros enabled by default but it does not appear to be so in my case.

    Appreciate any help or tips from all. Thanks.

    Regards
    Phil
    Last edited by vbarookie; 01-04-2012 at 11:38 PM.

  2. #2
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Opening workbooks and enabling user defined macros to run in those workbooks

    It might be useful to see the calling code. Does it, for instance, turn off calculations to improve speed? Furthermore, you may wish to explicitly set Application.AutomationSecurity to low (msoautomationsecuritylow I think is the constant) just to see if that helps. If it does not, then macro enablement would not appear to be the issue.

  3. #3
    Registered User
    Join Date
    02-21-2006
    Posts
    27

    Re: Opening workbooks and enabling user defined macros to run in those workbooks

    @OnErrorGoto0, thanks for the tip on turning off calculation. Yes it was done only for the workbook the VBA is updating to, ie Workbook B. Here is a fragment of the code which is located in Module1 of Workbook A:

    ' Macro to update changed data to Changed Data Tracking workbook B
    Sub MergeData()
    Dim cmFile As String
    Dim wwb As Workbook
    Dim rwb As Workbook

    ' Disable screen updates to eliminate flashing
    Application.ScreenUpdating = False

    On Error GoTo errhandler

    If IsWorkbookOpen(WORKBOOK_B) Then ' WORKBOOK_B is defined as global constant
    ' Since already opened, point wwb to opened workbook
    Set wwb = Workbooks(WORKBOOK_B)
    Else
    ' Open tracking workbook to write to
    Set wwb = Workbooks.Open(TPATH) ' Full pathname for WORKBOOK_B defined as a global constant
    End If
    wwb.Application.Calculation = xlCalculationManual ' set formula in Tracking workbook to calculate manually
    wwb.Application.CalculateBeforeSave = True

    ' Setup to look for .xlsm workbooks in cost changes directory
    cmFile = Dir(CMPATH & "*.xlsm", vbNormal) ' CMPATH is cost changes directory defined as a global constant
    If cmFile = "" Then
    Res = MsgBox("No workbooks found in cost changes directory. Exiting macro.", vbCritical)
    wwb.Close SaveChanges:=False
    Exit Sub
    End If
    FailCount = 0

    ' Loop until no .xlsm workbooks to process
    Do Until cmFile = ""
    ' Open each .xlsm workbook for processing with password
    Set rwb = Workbooks.Open(CMPATH & cmFile, , , , "password123")

    ' Call procedure to read from .xlsm workbook & update tracking workbook B
    Call CopyData(wwb, rwb, cmFile)

    ' Close each .xlsm workbook without saving
    rwb.Close SaveChanges:=False

    errhandler:
    If Err.Number > 0 Then
    Res = MsgBox("Error processing " & cmFile & ": Write down error message & inform macro administrator")
    Err.Clear
    End If

    ' Look for next .xlsm workbook
    cmFile = Dir
    Loop
    wwb.Close SaveChanges:=True

    Res = MsgBox("Completed processing")
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Registered User
    Join Date
    02-21-2006
    Posts
    27

    Re: Opening workbooks and enabling user defined macros to run in those workbooks

    Thanks to the reply from OnErrorGoto0, I managed to resolve the issue.

    I did some testing of the xlCalculationManual setting within the code. It appears that when the first workbook is set to xlCalculationManual, all subsequently opened workbooks inherit that setting even though the workbooks themselves may have been set to xlCalculationAutomatic.

    To overcome the issue, I added the statement

    rwb.Application.Calculation = xlCalculationManual
    immediately after opening each .xlsm workbook with the workbook handler assigned to rwb.

    This enabled the user defined macros within each opened .xlsm workbook to be calculated.

  5. #5
    Registered User
    Join Date
    02-21-2006
    Posts
    27

    Re: [SOLVED]:Opening workbooks and enabling user defined macros to run in those workb

    Typo in my last reply. The code fragment I added to resolve the issue is:

    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)

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