Find and Load a CSV

WolfsInk

New Member
Joined
Jan 24, 2012
Messages
14
Greetings Mr. Excel,

I'm working on a macro that will open a prompt window to select any csv file, then run it through a text to columns, but having issues after finding the file. Here's what I have so far. Also, I was wondering if there was a way for me to get the file name into a cell after the file was selected.

With ActiveSheet.QueryTables.Add(Filename = Application.GetOpenFilename("School Back up (*.csv), *.csv"), Destination:=Range("$A$1"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ","
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Where is the file path? I don't see anything in the code telling the system where to locate the file?
 
Upvote 0
WolfsInk,

When Excel opens a .csv file, it already separates it into different cells based on the comma delimiter. Because of this, you can simply copy its contents and paste it into a worksheet. The following does just that:
Code:
Sub tgr()
    
    Dim ws As Worksheet: Set ws = ActiveSheet
    
    On Error Resume Next
    With Workbooks.Open(Application.GetOpenFilename("School Back up, *.csv"))
        .Sheets(1).UsedRange.Copy ws.Range("A1")
        .Close False
    End With
    
End Sub
 
Upvote 0
ooooh, that worked perfectly! Though onto the second part of the inquiry: Is there a way to get the name of the file into a cell?
 
Upvote 0
Code:
Sub tgr()
    
    Dim ws As Worksheet: Set ws = ActiveSheet
    
    On Error Resume Next
    With Workbooks.Open(Application.GetOpenFilename("School Back up, *.csv"))
        ws.Range("A1").Value = .Name
        .Sheets(1).UsedRange.Copy ws.Range("A2")
        .Close False
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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