importing file name to cell

MrSak87

New Member
Joined
Jan 8, 2015
Messages
44
Hi all,

Hopefully this will be an easy one for you guys although I can't find a solution anywhere. I have a simple macro which imports various data from RAW excel files and cleans it into a separate excel sheet.

Sub importpart2()
Application.ScreenUpdating = False

'' SETUP

Dim DestWB As Workbook: Set DestWB = ActiveWorkbook
Dim UserChoice

DestWB.Worksheets("Data").Activate
Range("A3").Select
Dim LastLine As Long
LastLine = Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row


'' OPEN FILE

FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Excel Files *.xlsx (*.xlsx),")


If FileToOpen = False Then
MsgBox "No file specified."
Exit Sub
Else
Workbooks.Open Filename:=FileToOpen
End If

Dim SrcWB As Workbook
Set SrcWB = Application.Workbooks.Open(FileToOpen)

The macro itself works fine. However I want the FILE NAME of the excel sheet i'm importing to be put into a cell. To make things easy just to be placed into cell A1. Is this possible?

Thanks for your time
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
using a formula in a cell in source workbook (as the workbook has to have been saved for this formula to work)

this will place the filename (only) in the cell

Code:
<code>=CELL("filename")</code>

to complete this in the VBA code as you have the variable FieToOpen you could just put this into a cell something like this

Code:
.range ("A1") .value = FileToOpen
<<< Untested

here is a function i found elsewhere

Code:
<code>Public Function GetMyProp(prop As String) As String
    GetMyProp = ThisWorkbook.BuiltinDocumentProperties(prop)
End Function</code>
 
Last edited:
Upvote 0
That easy eh!? How embarrassing!

One more thing, is it possible to not have the directory listing - i.e not include C:\Users\etc just the actual name of the file I'm opening?

Cheers for your reply
 
Upvote 0
once you have the filename in a cell try this in a helper cell. there may be a simpler way others will offer

Code:
=RIGHT(A1,FIND("#",SUBSTITUTE(A1,"\","#",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))-1)
 
Upvote 0
=MID(CELL("filename"),SEARCH("[",CELL("filename"))+1,SEARCH("]",CELL("filename"))-SEARCH("[",CELL("filename"))-1)

Cheers you pointed me in the right direction and I got it. Nice one
 
Upvote 0

Forum statistics

Threads
1,216,750
Messages
6,132,505
Members
449,730
Latest member
SeanHT

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