Import filename of .GetOpenFilename into specific sheet/cell

Koosbie

New Member
Joined
Oct 31, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hey! I'm pretty new to VBA, but I just need to add a code to import the filename of the selected file into A1 of sheet2. Here's what I have currently;

Sub ImportData()
Dim FileLocation As String

FileLocation = Application.GetOpenFilename
If FileLocation = "False" Then
Beep
Exit Sub
End If

Application.ScreenUpdating = False
Set ImportWorkbook = Workbooks.Open(Filename:=FileLocation)

ImportWorkbook.Worksheets(1).Range("A1:Z1000").Copy ThisWorkbook.Worksheets(1).Range("A1")

ImportWorkbook.Close
Application.ScreenUpdating = True

Application.ScreenUpdating = True

Worksheets("Scan sheet").Activate
Worksheets("Scan sheet").Select

Worksheets("Input data").Visible = xlSheetHidden
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Sheets(2).Range("A1").Value = FileLocation
Thanks for the quick reply!

I tried literally copy/paste that line and did not work, I also tried this and did not work.... :(

Worksheets("Scan sheet").Range("A1").Value = FileLocation
 
Upvote 0
if FileLocation have a value? its too simple not to work.
:ROFLMAO: The FileLocation value would be the file thats selected, then automatically closed onces its data is imported. Ive tried the code in the VBA before the file is closed and its not working.. like i said, your talking to a rookie here hahaha
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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