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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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,215,108
Messages
6,123,132
Members
449,097
Latest member
mlckr

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