Can I reference any workbook containing a certain phrase in the name?

  • Many of the macros I use require a step where it copies information from a daily/weekly report to be copied into another workbook. These reports are downloaded from a web browser. Previously, chrome would download them all with the exact same name so it was easy to reference them in the code, however now it seems to be storing these and adding a number to the end.


    Is there a way I can tell the macro to select a workbook containing a phrase? For example - any workbook named "Resolved Cases Report" regardless of if it is called "Resolved Cases Report (12)"


    There would only be one version of the report open at any given time. I hope this makes sense

  • Use the LIKE operator while looping the workbooks collection


    Code
    ' But just pseudo code
    For each wb in Application.Workbooks
       ' You must terminate the partial name with *
       If wb.Name Like "WhatEver*" then
          Got it, do something. Maybe assign to a variable
          Exit for - maybe. You may want to continue the loop to get a list of all like.
       End if
    Next
  • Use the LIKE operator while looping the workbooks collection


    Code
    ' But just pseudo code
    For each wb in Application.Workbooks
    ' You must terminate the partial name with *
    If wb.Name Like "WhatEver*" then
    Got it, do something. Maybe assign to a variable
    Exit for - maybe. You may want to continue the loop to get a list of all like.
    End if
    Next


    Thanks so much for this, can you advise on how I would actually make that into a variable?

  • Sorry - I don.'t understand.


    You declare a variable as type X (Workbook is this case)
    You assign a workbook object to the variable if the test for wb.name like "whatever*" is True
    You use that variable to refer to your workbook in code.


    Maybe if you post your code.

  • Sorry - I don.'t understand.


    You declare a variable as type X (Workbook is this case)
    You assign a workbook object to the variable if the test for wb.name like "whatever*" is True
    You use that variable to refer to your workbook in code.


    Maybe if you post your code.


    So if I used your above piece of code, I then need to actually set that workbook and a worksheet within it as a variable so that I can actually use it - so would I need to do something like this, for example?



    Code
    Set wbReport = Workbooks("Whatever*")
    Set wsReport = wbReport.Sheets("Sheet1")
    End If
    Next



    This doesn't seem to be working, and I'm a little confused


  • Very rough. If finding a workbook is used in multiple places in your code then turn it into a function.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!