How to import a CSV into an existing worksheet? (Macros)

sjaakie

New Member
Joined
Aug 31, 2018
Messages
3
Hello,

I am working with the code below I found on the internet. The purpose is to automatically import a CSV file with some parameters, that's working like a charm!

Code:
Sub ImportTextFile()'Import a text file using Excel's own import function.
Dim vFileName


On Error GoTo ErrorHandle


vFileName = Application.GetOpenFilename("CSV bestanden (*.csv),*.csv")


If vFileName = False Or Right(vFileName, 3) <> "csv" Then
   GoTo BeforeExit
End If


Application.ScreenUpdating = False


Workbooks.OpenText Filename:=vFileName, _
    Origin:=437, StartRow:=1, DataType:=xlDelimited, _
    TextQualifier:=xlTextQualifierNone, _
    ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=True, Space:=False, _
    Other:=False, TrailingMinusNumbers:=True, _
    Local:=True


Columns("A:A").EntireColumn.AutoFit


BeforeExit:
Application.ScreenUpdating = True
Exit Sub
ErrorHandle:
MsgBox Err.Description
Resume BeforeExit
End Sub

Right now I have the wish to import the intended CSV file into an existing worksheet what already contains a predefined tab. I just could make such a worksheet but I don't know how to get the macro above get the job done. It should import the CSV in tab1 while tab2 already exists.

Could someone helps me out?
Thanks in advance! ;)
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You can import a CSV into an existing sheet.
You can get the code for this by turning on the Macro Recorder, and performing the following steps manually:

1. Turn on the Macro Recorder
2. Go into the Workbook you wish to import the CSV to
3. Go to the sheet you wish to import the CSV to
4. Go to the Data menu
5. On the "Get External Data" ribbon, select "From Text"
6. Browse and select your file
7. Go through the "Text Import Wizard", setting the proper values
8. Click ""Finish" when complete
9. Stop the Macro Recorder and view your code
 
Upvote 0
This is the code I got:

Code:
Sub Macro1()
'
' Macro1 Macro
'


'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;[COLOR=#ff0000]LOCATION[/COLOR]" _
        , Destination:=Range("$A$1"))
        .Name = "[COLOR=#ff0000]NAME[/COLOR]"
        .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 = xlTextQualifierNone
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Sheets("BronCSV").Select
    ActiveWindow.SmallScroll Down:=-30
End Sub

Is there a possibility to change the code so it enabled users to add files they can search by themselves with a Open File prompt?
 
Upvote 0

Forum statistics

Threads
1,215,111
Messages
6,123,159
Members
449,098
Latest member
Doanvanhieu

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