Copy File Name Into Sheet

ai1094

Board Regular
Joined
Aug 23, 2018
Messages
92
Hi I have part of my macro pasted below. The problem I'm having is I'm getting the full path name of the file, which is not what I want. How can i get ONLY the file name itself pasted into cell A1?

Set Wbk1 = ActiveWorkbook
Fname = Application.GetOpenFilename
Set Wbk2 = Workbooks.Open(Fname)
Wbk2.Sheets(1).Copy , Wbk1.Sheets(Wbk1.Sheets.Count)
Range("A1") = Fname
Wbk2.Close False
Wbk1.Sheets(Wbk1.Sheets.Count).Name = "REF"
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try:
Code:
[COLOR=#333333]Range("A1") = [/COLOR]ActiveWorkbook.Name
 
Upvote 0
In my macro, I already have a workbook opened. What I am doing is opening another workbook and copying the data from that workbook into the current. So I need the current workbook to have the other workbook's file name in cell A1. Does that make sense?

Try:
Code:
[COLOR=#333333]Range("A1") = [/COLOR]ActiveWorkbook.Name
 
Upvote 0
How about
Code:
Range("A1").Value=Wbk1.Name
 
Upvote 0
Didn't work. It copied the workbook's file name that is already opened. I need Workbook2's file name inside A1 in Workbook1. Workbook2 is the file that gets selected when the prompt comes out.

How about
Code:
Range("A1").Value=Wbk1.Name
 
Upvote 0
In that case change Wbk1 to Wbk2
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
In my macro, I already have a workbook opened. What I am doing is opening another workbook and copying the data from that workbook into the current. So I need the current workbook to have the other workbook's file name in cell A1. Does that make sense?
When you open Wkbk2, that is then the ActiveWorkbook (until you close it or move off of it). So if you place it where you had you previous line, it should work.
Still, it is safer to follow Fluff's advice, and use the explicit workbook reference from whose name you wish to retrieve.

I am not 100% clear which workbook you want to place the value in, so you may need to account for that too, if you want to put it in Wkbk1.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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