Import issues, excel creates new worksheet

JCLLE

New Member
Joined
Apr 17, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello everyone


Only been doing this a couple of months so very new to all of this.
Everytime I run the macro excel creates a new worksheet. Since I need to put formulas in the worksheet I need excel to put the data in the same worksheet everytime.
Thanks in advance!

VBA Code:
'
    Range("A1").Select
    ActiveWorkbook.Queries.Add Name:="son (3)", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    Source = Csv.Document(File.Contents(""L:\PRI_PRINGLES\C&S - customization&shipping\16. Scripts\Cans\son.txt""),[Delimiter=""|"", Columns=18, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Source,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}, {""Column4"", type text}, {""Column" & _
        "5"", type text}, {""Column6"", type text}, {""Column7"", type text}, {""Column8"", type text}, {""Column9"", type text}, {""Column10"", type text}, {""Column11"", type text}, {""Column12"", type text}, {""Column13"", type text}, {""Column14"", type text}, {""Column15"", type text}, {""Column16"", type text}, {""Column17"", type text}, {""Column18"", type text}})" & Chr(13) & "" & Chr(10) & "in" & _
        "" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""son (3)"";Extended Properties=""""" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [son (3)]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "son__3"
        .Refresh BackgroundQuery:=False
    End With
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Herakles

Board Regular
Joined
Jul 5, 2020
Messages
73
Office Version
  1. 365
Platform
  1. Windows
I am assuming that you want the data in the worksheet replaced when you import the csv file.

This works for me.

VBA Code:
Public Sub subCSV_Import()
Dim Ws As Worksheet
Dim varFile As Variant

    ActiveWorkbook.Save

    Set Ws = ActiveWorkbook.Sheets("PO Data")
        
    Ws.Activate
    
    Ws.Cells.ClearContents
    
    ' Hardcode the name of the file to import or
    ' varFile = "C:\Dump\ImportData.csv"
    
    ' select the file.
    varFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
    
    If varFile <> False Then
    
        With Ws.QueryTables.Add(Connection:="TEXT;" & varFile, Destination:=Ws.Range("A1"))
             .TextFileParseType = xlDelimited
             .TextFileCommaDelimiter = True
             .Refresh
        End With

    End If

End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,444
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
What happens if you just remove this line
VBA Code:
ActiveWorkbook.Worksheets.Add
 

Forum statistics

Threads
1,141,403
Messages
5,706,257
Members
421,435
Latest member
Yoga15

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
Top