Reading in csv file into an open workbook

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189
Hi,

I am trying to create a macro which reads in the selected .csv file into a worksheet called CRData. When I run the macro which is attached to a worksheet, I would like the data to be added to the CRData worksheet. I have only written the code below and am stuck right now. Any advice would be appreciated.

Code:
' get name of the .csv file from the user
Dim lstrFileName As String
lstrFileName = Application.GetOpenFilename(FileFilter:="All Files(*.*), *.*", Title:="MyTitle:::: Select the File to Load")

' not sure if I should use Query tables
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & lstrFileName & "", _
Destination:=Range("AD5"))

Worksheets("CRData").Cells.Clear

-Eileen
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi Eileen, this version opens the CSV, copies the data across to AD5, and closes the CSV:
Code:
Sub ImportCSV()
Dim wbk As Workbook
Dim sht As Worksheet
Dim lstrFileName As String

Set wbk = ThisWorkbook
Set sht = wbk.Sheets("CRData")
sht.Cells.Clear

' get name of the .csv file from the user
lstrFileName = Application.GetOpenFilename(FileFilter:="Text Files (*.csv),*.csv", _
Title:="MyTitle:::: Select the File to Load")

'open CSV, copy
Workbooks.Open lstrFileName
Range("A1").CurrentRegion.Copy Destination:=sht.Range("AD5")
ActiveWorkbook.Close False

End Sub
Denis
 
Upvote 0
Hi Denis,

It worked well. Is there some way to configure it so that when it copies, autowrap is disabled in the destination worksheet (CRData)? Thanks.

-Eileen
 
Upvote 0
Hi Eileen,

Yep -- it drives me crazy when I import data from Access too.
Add this line just before End Sub:

Code:
Range("AD5").CurrentRegion.WrapText = False

Denis
 
Upvote 0
It is very odd. When I tried the above, I found that the wrap text configuration for the imported data was set to false except for column P. Any ideas what is going on?

The only difference from what was specified above was that I changed AD5 to A1.

Sub ImportCSV()
Dim wbk As Workbook
Dim sht As Worksheet
Dim lstrFileName As String

Set wbk = ThisWorkbook
Set sht = wbk.Sheets("CRData")
sht.Cells.Clear

' get name of the .csv file from the user
lstrFileName = Application.GetOpenFilename(FileFilter:="Text Files (*.csv),*.csv", _
Title:="MyTitle:::: Select the File to Load")

'open CSV, copy
Workbooks.Open lstrFileName
Range("A1").CurrentRegion.Copy Destination:=sht.Range("A1")
ActiveWorkbook.Close False
Range("A1").CurrentRegion.WrapText = False
End Sub
 
Upvote 0
If column P is not part of Range("A1").CurrentRegion then you won't remove the wrap on P -- I assume it's the last column in the import.

To check, put this in the Immediate window and press Enter, then check the worksheet:

Code:
Range("A1").CurrentRegion.Select

If P is not selected, you can change to

Code:
Range("A:P").WrapText = False

Denis
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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