Solved

insert the excel file name into a range of cells

Posted on 2014-10-06
8
437 Views
Last Modified: 2014-10-19
I would like to have a macro that would insert the name of the open file (example: DMAP_10-14.xlsx) into a range of cells
0
Comment
Question by:Dan Purcell
8 Comments
 
LVL 12

Expert Comment

by:James Elliott
ID: 40363934
Open your VB Editor (Alt+F11)

Insert a new module

Paste the following code

Option Explicit

Public Function MyName() As String

    MyName = ThisWorkbook.Name

End Function

Open in new window


Save your workbook.

Use in any cell(s) like this:

=MyName()
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 250 total points
ID: 40363936
Why not use a formula instead of VBA?

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

Open in new window

0
 
LVL 27

Assisted Solution

by:Glenn Ray
Glenn Ray earned 250 total points
ID: 40364028
I like Phillip's solution, but if you indeed need the name of the active file inserted as a value in cells, then the VBA line would be:
Range(range_reference).Value = ActiveWorkbook.Name
where range_reference is either a specific cell (ex., "A2") or range of cells (ex., "A2, B4").  You could also use the cells(r,c) method if you wished.

-Glenn
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Closing Comment

by:Dan Purcell
ID: 40364130
Thanks, I did want it as a macro!
0
 
LVL 12

Expert Comment

by:James Elliott
ID: 40364334
Eh?
0
 

Author Comment

by:Dan Purcell
ID: 40373107
I tried running it and couldn't get it to work. You can give him point's, I certainly didn't mean anything personal.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Meetings to discuss business process can waste time, and often do .  The meeting's dialog can get confusing when participants have different professional perspectives and backgrounds.  A jointly-developed process picture helps wade through the confu…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now