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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
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
 

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189
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
 

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
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
 

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189
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
 

SydneyGeek

MrExcel MVP
Joined
Aug 5, 2003
Messages
12,251
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
 

Forum statistics

Threads
1,141,842
Messages
5,708,916
Members
421,598
Latest member
NewHere

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