How to use GetOpenFileName

APML

Board Regular
Joined
Sep 10, 2021
Messages
216
Office Version
  1. 365
Hi All, just hoping someone can help. I pretty simple when it comes to vba, i generally look online a find the code and adjust to make it work. I'm trying to use GetOpenFile.
I'm hoping to open a box with all csv file from a specific folder but atm I'm unable to workout how to do that..(the specific folder part)
So far this is what I've done, it works, but hoping to get can help on how I adapt so it only opens a specific folder holding numerous csv files. The folder name is CSV_ALL

VBA Code:
Sub Get_Data_From_File()
Dim FileToOpen As Variant
Dim OpenBook As Workbook
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.csv*),*csv*")
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A1:g785").Copy
        ThisWorkbook.Worksheets("Red").Range("A3").PasteSpecial xlPasteValues
        OpenBook.Close False
    End If
    Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
What is the full path of the specific folder?
 
Upvote 0
Perhaps this:

VBA Code:
Sub Get_Data_From_File()
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Dim FolderPath As String
   
    FolderPath = "C:\CSV_ALL"
    VBA.ChDir FolderPath
   
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.csv*),*csv*")
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A1:g785").Copy
        ThisWorkbook.Worksheets("Red").Range("A3").PasteSpecial xlPasteValues
        OpenBook.Close False
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hey WOW. Thanks, works great.
I did get a popup message that says:

"There is a large amount of information on the Clipboard. Do you want to be able to paste this information into another program later?
to save it on the clipboard so that you can paste later, click Yes
to delete it from the clipboard and free memory click No

I don't need the info on the clipboard so I clicked No, just wonder if there is a setting to get rid of that message (I would never need to save the data to the clipboard)
 
Upvote 0
Try replacing:

VBA Code:
        OpenBook.Close False

with:

VBA Code:
    Application.DisplayAlerts = False
    OpenBook.Close False
    Application.DisplayAlerts = True
 
Upvote 0
You could also add this line, which should clear the clipboard.
VBA Code:
ThisWorkbook.Worksheets("Red").Range("A3").PasteSpecial xlPasteValues
Application.CutCopyMode = False
OpenBook.Close False
 
Upvote 0
You could also add this line, which should clear the clipboard.
VBA Code:
ThisWorkbook.Worksheets("Red").Range("A3").PasteSpecial xlPasteValues
Application.CutCopyMode = False
OpenBook.Close False
Works great, thx for your help
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,391
Members
448,957
Latest member
Hat4Life

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