Import CSV into workbook tab

Tanquen

New Member
Joined
Dec 14, 2017
Messages
39
Office Version
  1. 365
Platform
  1. Windows
I have an existing xlsm file with a tab called "CSV".

I'd like to add a button and use VBA to open the CSV file and place the text into the CSV tab and then use the text to columns function.

I'd also like to place the button on a different Tab than the CSV Tab and have it place the CSV file data into the CSV Tab and not the current active tab.

I have been just opening the csv in notepad and then doing a cut and paste into the CSV tab and then running the Excel Text to Columns function. That works but it would be nice to just drag the file in or click a button and open the csv file from the same folder.

It would also be cool if I could get the Text to Columns to stay active. Like any time you paste text in column A it just uses the same settings as before.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You can do all that with Power Query (PQ) (no copy paste, notepad).
Once you do it once it automates all subsequent imports.


The first step depends a bit on Excel version.
The thing is to use the Import from .csv option from somewhere in Data menu.

Excel 2016 and above has pretty much the menus as below.
For Excel 2010-2013 you need to install Power Query add-inn, free from Msft. It becomes menu tab itself.


Step 1


1665618632657.png


Step 2 - Select path\File_xxy.csv. This is going to be the Source for PQ, which means PQ will remember exactly the path and the name of the file.


1665619207305.png


1665619477729.png



1665619705325.png



1665620034914.png


1665620136257.png


1665620308609.png


1665623105119.png


1665623320667.png



1665623406682.png


1665623567867.png




1665623696901.png



If you always save the csv files in the same folder there could be a different approach (use the folder as the Source):

 

Attachments

  • 1665619659557.png
    1665619659557.png
    78.7 KB · Views: 6
  • 1665620117857.png
    1665620117857.png
    14 KB · Views: 4
  • 1665620805637.png
    1665620805637.png
    7.4 KB · Views: 5
  • 1665621226770.png
    1665621226770.png
    81.9 KB · Views: 4
  • 1665621677419.png
    1665621677419.png
    55.9 KB · Views: 4
  • 1665622029357.png
    1665622029357.png
    91.6 KB · Views: 3
  • 1665622078379.png
    1665622078379.png
    16.8 KB · Views: 7
Last edited:
Upvote 0
Wow, thanks for the detailed info but it seems like a lot of work or steps. Isn't what I'm doing now, faster? I'm not sure how this would work with my current setup. You did say once you set it up once you should be good to go but only if the file and path to the CSV and its name never changes?

Currently the main Excel file is copied and then the accompanying CSV is added to the CSV sheet or tab. The names of both files change though they are normally located in the same folder. That's why I was thinking of a button that I could click on the main sheet in the workbook and have it default to the current Excel files folder and just click on or pick the CSV file.

The workbook has some other VBA scripts that manipulate the CSV tab and once finished I then have another VBA script that grabs a few of the key columns from the CSV tab and creates a new CSV file. That part works pretty well, it's set up to create the new CSV files name based on object names in the original CSV and give it a date and place it in the same location as the current main Excel file.
 
Upvote 0
Hi T,

All those steps only once if source fixed. You can change the source in a few clicks if addresses change. Let me know if yo are interested and I'll write it down.
One way or another you need to import the csv in Excel. Power Query (an ETL tool) is better than the legacy text import wizard.

Success!
 
Upvote 0
Hi T,

All those steps only once if source fixed. You can change the source in a few clicks if addresses change. Let me know if yo are interested and I'll write it down.
One way or another you need to import the csv in Excel. Power Query (an ETL tool) is better than the legacy text import wizard.

Success!
I'll give it a shot. I just thought it would be easier to have VBA automate what I do manually. Just open a file and cut and paste the text into a TAB and then run the text to columns function.
 
Upvote 0
Change ranges to your need.

Assign it to button.
It will open browser for you to choose CSV file you want to import.
And insert it to DATA sheet.
VBA Code:
Sub ImportData()

    Dim xFileName As Variant
    Dim rg As Range
    Dim xAddress As String
    xFileName = Application.GetOpenFilename(, , , False, False)
    If xFileName = False Then Exit Sub
    On Error Resume Next
    Set rg = ThisWorkbook.Worksheets("DATA").Range("A24:A2000")
    On Error GoTo 0
    If rg Is Nothing Then Exit Sub
    xAddress = rg.Address
    With Sheets("DATA")
        With .QueryTables.Add("TEXT;" & xFileName, .Range(xAddress))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 65001
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileTrailingMinusNumbers = False
        .Refresh BackgroundQuery = False
        End With
    End With
End Sub
 
Upvote 0
Solution
Thanks! That seems to work.

It is removing a character from the first "; " line but I think that is all it's changing.

; Created on: 10/12/2022
Created on: 10/12/2022
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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