简体   繁体   中英

Run macro on newly created .xlsx file from another workbook

I am opening and converting a .csv to an .xlsx file on a shared drive location. Once the .xlsx is created it remains open. The macros to do this are in a master Workbook located on the shared drive as well. Right now I have it set for a user to open the workbook and select a command button that opens and converts the file.

The user the selects the template needed from a list box and clicks a second command button which runs the following macro that calls a specific macro which converts the .xlsx just created to the correct format.

All my macros work except the macro below. It stops at the first Workbooks.. Right now the newly created .xlsx is the only .xlsx in its folder but eventually there may be multiple .xlsx files created from multiple .csv files.

  Sub Run_Macros()

    'IF USER SELECTED FXX_Rejects
    If Range("A8").Value = Range("A2").Value Then
    'ACTIVATE OTHER WORKBOOK
    Workbooks("*.xlsx").Activate
    Workbooks("*.xlsx").Sheets("Sheet1").Select
    'AND THEN RUN APPROPRIATE MACRO
     Call aaLayout


    'IF USER SELECTED LXX_Rejects
    ElseIf Range("A8").Value = Range("A3").Value Then
    'ACTIVATE OTHER WORKBOOK
    Workbooks("*.xlsx").Activate
    Workbooks("*.xlsx").Sheets("Sheet1").Activate
    'AND THEN RUN APPROPRIATE MACRO
    Call abLayout


    'IF USER SELECTED HXXXX_Rejects
    ElseIf Range("A8").Value = Range("A4").Value Then
    'ACTIVATE OTHER WORKBOOK
    Workbooks("*.xlsx").Activate
    Workbooks("*.xlsx").Sheets("Sheet1").Activate
    'AND THEN RUN APPROPRIATE MACRO
    Call acLayout


    'IF USER SELECTED SXXX_Rejects
    ElseIf Range("A8").Value = Range("A5").Value Then
    'ACTIVATE OTHER WORKBOOK
    Workbooks("*.xlsx").Activate
    Workbooks("*.xlsx").Sheets("Sheet1").Activate
    'AND THEN RUN APPROPRIATE MACRO
    Call adLayout




    End If

    End Sub

I have edited my question post and added the macro below that locates and converts the .CSV files preparing them for templating. Maybe this can assist answering my original question.

    Sub CSVFiles()
        Dim MyFiles As String, ThisMonth As String
        Dim startPath As String
        Dim wb As Workbook
        ThisMonth = Format(Date, "mmmm")
        startPath = "\\XXX\2017\" & ThisMonth & "\"
        MyFiles = Dir(startPath & "*.csv")
        Do While MyFiles <> ""

        Set wb = Workbooks.Open(startPath & MyFiles)

        Call XLSXConvert
        'Converts all csv files, saves and closes files.  Prepares files for full template creation.
        wb.SaveAs fileName:=startPath & Replace  (MyFiles, ".csv", ".xlsx"),FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        wb.Close
        MyFiles = Dir
        Loop
    End Sub

Your problem is with "*.xlsx". Operating system commands accept the * as a wildcard but you're not calling an operating system command. You are calling a VBA method on the Workbooks collection of objects that uses a number, usually the order in which the files were opened, as its identifier. Workbooks().Activate does not go out to the folder and look at all the available files and pick one that matches. Workbooks.Open() would do that but it still doesn't accept wildcards, and you said the workbook you want is already open.

Workbooks().Activate looks through the list of open workbooks for an identifier. The object you're trying to Activate was created in the code that ran before you get to this point, that we can't see from your posting. Somewhere in that code, you could assign that object to a variable and then you could create an argument in this code so you could pass in that variable.

What you could also do, IF you know that the only .xlsx file you have open is the one you want, is loop through all the open files and check the name to see if it ends in .xlsx and if it does assign that to a variable. Illustrated below:

  Sub Run_Macros()


    Dim wb As Workbook
    Dim target As Workbook
    Dim wbs As Workbooks


    Set wbs = Workbooks
    For Each wb In wbs
        If LCase(Right(wb.FullName, 5)) = ".xlsx" Then
            Set target = wb
        End If
    Next wb

    'IF USER SELECTED FXX_Rejects
    If Range("A8").Value = Range("A2").Value Then
    'ACTIVATE OTHER WORKBOOK
    target.Activate
    target.Sheets("Sheet1").Select
    'AND THEN RUN APPROPRIATE MACRO
    Call aaLayout


    'IF USER SELECTED LXX_Rejects
    ElseIf Range("A8").Value = Range("A3").Value Then
    'ACTIVATE OTHER WORKBOOK
    target.Activate
    target.Sheets("Sheet1").Activate
    'AND THEN RUN APPROPRIATE MACRO
    Call abLayout


    'IF USER SELECTED HXXXX_Rejects
    ElseIf Range("A8").Value = Range("A4").Value Then
    'ACTIVATE OTHER WORKBOOK
    target.Activate
    target.Sheets("Sheet1").Activate
    'AND THEN RUN APPROPRIATE MACRO
    Call acLayout


    'IF USER SELECTED SXXX_Rejects
    ElseIf Range("A8").Value = Range("A5").Value Then
    'ACTIVATE OTHER WORKBOOK
    target.Activate
    target.Sheets("Sheet1").Activate
    'AND THEN RUN APPROPRIATE MACRO
    Call adLayout

    End If

    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM