Reference file name in Excel function

ajorlov

New Member
Joined
Feb 3, 2005
Messages
29
Is there an Excel function which can say "If the name of the file I am in is X, then return value Y, otherwise return value Z."?

In other words, =IF([name of file]="Today", "OK", "Not OK")

Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi,


You can either use a series of worksheet functions to retreive the filename, e.g. ..


=MID(CELL("filename"),FIND("[",CELL("filename"),1)+1,LEN(CELL("filename"))-FIND("]",CELL("filename"),1)-1)


Or you can write your own UDF. This example would go into a standard module. ..


Code:
Option Explicit

Public Function BOOKNAME(Optional celRef As Range) As String
    If Not celRef Is Nothing Then
        BOOKNAME = celRef.Parent.Parent.Name
    Else
        BOOKNAME = Application.Caller.Parent.Parent.Name
    End If
End Function


Note that the two solutions above will appear different. The native formula result will not have a .xls on the end, whilst the UDF function will. For a UDF that does not have that, you could put the option of it in there ...


Code:
Option Explicit

Public Function BOOKNAME(Optional celRef As Range, _
    Optional theType As Integer) As String
    Dim myStr As String
    If Not celRef Is Nothing Then
        myStr = celRef.Parent.Parent.Name
        Select Case theType
        '1 = Book1.xls
        '2 = Book1
        Case 1, 0
            BOOKNAME = myStr
        Case 2
            BOOKNAME = Left$(myStr, _
                Len(myStr) - 4)
        End Select
    Else
        myStr = Application.Caller.Parent.Parent.Name
        Select Case theType
        Case 1, 0
            BOOKNAME = myStr
        Case 2
            BOOKNAME = Left$(myStr, Len(myStr) - 4)
        End Select
    End If
End Function

Then you could use something like this ...


=BOOKNAME(,2)

This will give you something like "Book1". Optionally ..

=BOOKNAME(,1)
or
=BOOKNAME(,0)
or
=BOOKNAME()

Will give you "Book1.xls" type results. You can specify other workbooks also with the first (optional) syntax.


HTH
 
Upvote 0

Forum statistics

Threads
1,203,269
Messages
6,054,482
Members
444,727
Latest member
Mayank Sharma

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top