?
Solved

insert the excel file name into a range of cells

Posted on 2014-10-06
8
Medium Priority
?
578 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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 1000 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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

762 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