PDA

View Full Version : Command Bttn import .csv file to new worksheet



canselmi
04-05-2011, 12:27 PM
I've used the "Macro Record" to help with the VBA, but I need more help.

What I'm trying to do is this:
1. UserForm command button that will only allow the selection of only one .csv file. But I need the user to be able to browse to the csv.
2. Import the contents into a new (or existing) hidden worksheet called "Import".

I've found quit a few threads that kind of address this, but not exactly.

This is what the record macro creates:
Sub ImportCSV()
'
' ImportCSV Macro
'

'
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;R:\R\River Ranch Radiology\Sales\38347401016.csv", Destination:=Range( _
"$A$1"))
.Name = "38347401016"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Application.Goto Reference:="ImportCSV"
End Sub

GTO
04-06-2011, 08:35 AM
I've used the "Macro Record" to help with the VBA, but I need more help.

What I'm trying to do is this:
1. UserForm command button that will only allow the selection of only one .csv file. But I need the user to be able to browse to the csv....

What does that part mean? Do you mean to browse folders and pick a file to import?

Thanks,

Mark

shrivallabha
04-06-2011, 09:46 AM
If you are looking for opening a specific file then use following method:

sGetOpenFile = Application.GetOpenFilename("CSV Files (*.csv), *.csv")

This will open a dialogbox wherein you'd be able to select the file.