+ Reply to Thread
Results 1 to 5 of 5

Use VBA to get excel file name into a cell

Hybrid View

DonkeyKong222 Use VBA to get excel file... 06-06-2022, 12:05 PM
cytop Re: Use VBA to get excel file... 06-06-2022, 12:29 PM
DonkeyKong222 Re: Use VBA to get excel file... 06-06-2022, 01:18 PM
cytop Re: Use VBA to get excel file... 06-06-2022, 01:32 PM
jolivanes Re: Use VBA to get excel file... 06-06-2022, 01:44 PM
  1. #1
    Registered User
    Join Date
    08-28-2020
    Location
    United States, Ohio
    MS-Off Ver
    2010
    Posts
    12

    Use VBA to get excel file name into a cell

    Hi everyone,

    I am trying to write a VBA to get the file name of the active workbook in worksheets(1). Split that file name in sections with the hyphens that separate each section. then paste each part of the array into specific cells of the active workbook worksheets(1).

    In the example file the file has been named

    name - color - weight - clothes

    I want the vba to take this name and separate each part by where the hyphens are and place each one in the cells below

    "name" into cell B2
    "color" into cell B3
    "weight" into cell B4
    "clothes" into cell B5

    This VBA will be part of a bigger VBA where I will be looping through multiple excel files in a folder to extract the information from the file name and place them in specific cells. Below is the code I tried and I keep getting an "object required" error and the debugger does not tell me anything.

    Sub excelfilepath()

    Dim filename() As String
    Dim fname1 As String, fname2 As String, fname3 As String, fname4 As String
    Dim fpath As String

    With ActiveWorkbook.Worksheets(1)

    'get file path with file name
    fpath = aplication.ActiveWorkbook.FullName

    'split by hyphens into array
    filename() = Split(fpath, " - ")

    'set each part of array equal to variable
    fname1 = filename(0)
    fname2 = filename(1)
    fname3 = filename(2)
    fname4 = filename(3)

    'put each variable into desired cell range
    .Range("B2").Value = fname1
    .Range("B3").Value = fname2
    .Range("B4").Value = fname3
    .Range("B5").Value = fname4
    End With

    End Sub


    All help is appreciated. Thanks in advanced!

    Had application misspelled in the original code. Once fixed the code worked as it should.
    Attached Files Attached Files
    Last edited by DonkeyKong222; 06-06-2022 at 01:19 PM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Use VBA to get excel file name into a cell

    Let's point out the obvious first... a typo
    fpath = aplication...
    Just the thing for an 'Object Required' error

  3. #3
    Registered User
    Join Date
    08-28-2020
    Location
    United States, Ohio
    MS-Off Ver
    2010
    Posts
    12

    Re: Use VBA to get excel file name into a cell

    Wow cant believe I missed that. Works perfect once the typo was fixed. Thanks!

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Use VBA to get excel file name into a cell

    You might consider setting the 'Require Variable Declaration' setting in the VBE options. That'll catch all misnamed variables (and objects) when the project is compiled before running.

    Untitled.png

    ETA: The downside is you'll have to declare all variables before using them but that is nothing more than good practise in any case.
    Last edited by cytop; 06-06-2022 at 01:37 PM.

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Use VBA to get excel file name into a cell

    Dim a, i As Long
    a = Split(Left(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".") - 1), "-")
        For i = LBound(a) To UBound(a)
            Cells(i + 2, 2).Value = Trim(a(i))
        Next i

+ 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. Copy a sheet to another Excel file (file name linked to the cell)
    By wess2016 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-18-2019, 02:32 AM
  2. How to copy a cell from a closed excel file to another open excel file
    By anup_keshri in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-22-2013, 08:48 AM
  3. Replies: 5
    Last Post: 08-27-2013, 03:15 PM
  4. [SOLVED] Code to open another excel file, file name depending on cell contents
    By L-Drr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2013, 11:09 AM
  5. Update cell links between 2 excel file without opening source file
    By ferry in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-26-2012, 03:03 PM
  6. Replies: 1
    Last Post: 07-14-2007, 07:25 AM
  7. [SOLVED] Drag and drop to create a link to a file in a cell of an Excel file
    By paulaugust2003@yahoo.com in forum Excel General
    Replies: 2
    Last Post: 09-18-2005, 11:05 AM

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