Extracting File Name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • atr8340
    New Member
    • Sep 2011
    • 3

    Extracting File Name

    I am writing a macro to preform processes on an excel spreadsheet. I'm trying to use the file name string to input into cells. I have tried the following code:

    Code:
    MyName = ThisWorkbook.Name
    The workbook I am working out of is called "2008 02.xlsx", but when I run this code the variable "MyName" returns "PERSONAL.X LS". It my be something about the version of excel I am using (2003 version). When I choose Tools/Macro/Record New Macro in excel, there is a drop down called "Store macro in:" and has three options: "Personal Macro Workbook", "New Workbook", and "This Workbook". I choose "Personal Macro Workbook" so I can use this macro on multiple workbooks. If I were to use "This workbook" I wouldn't be able to use it on any other workbook. I feel like this choice is why I cannot return "2008 02" when I run this code. Does anyone have an insight on what code to use to get "2008 02.xlsx" instead of "PERSONAL.X LS"?
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    This is in Office 2003:
    2 Excel files:
    book1 with the macro to get the filename:
    Code:
    Sub macro_GET_FILENAME()
    MsgBox Mid(ActiveWorkbook.FullName, InStrRev(ActiveWorkbook.FullName, "\") + 1)
    End Sub
    book2 with the macro for running macro in book1 to give the filename of book2:
    Code:
    Sub macro_RUN()
        Application.Run "Book1.xls!macro_GET_FILENAME"
    End Sub

    Comment

    Working...