A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

CSV Import Issue (Newbie)



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old September 28th 06, 10:43 PM posted to microsoft.public.outlook.program_vba
John V
external usenet poster
 
Posts: 3
Default CSV Import Issue (Newbie)

The routine below is used to import the contents of several files and paste
those contents into a worksheet. All worked fine until the source files
changed from tab delimited to CSV files (nothing I can do about the change).
I changed two lines of code to recognize .csv file instead of a .txt file,
then imported.

The resulting destination worksheet has unexpected results. 1) All text
cells are now preceeded by a single quote. 2) Any commas embedded in a source
cell cause the text to be parsed into two or more destination cells, and 3)
non printing characters remain in the destination cells. None of these
phenomena appear to occur when I simply open the csv file in Excel, but it is
the copy/paste routine below that seems to be the culprit (I think).

Any help much appreciated.
John

Code starts he

Sub Firstattempt()
Dim rng, rng1 As Range
Dim FNames(1 To 100, 1 To 2) As Variant
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim FN As String ' For File Name
Dim ThisRow As Long
Dim MyFileLocation As String
MyFileLocation = ThisWorkbook.Path & "\*.csv"
' Changed from Tab to Comma delimited 9/06
FN = Dir(MyFileLocation)
FNum = 0
Do Until FN = ""
FNum = FNum + 1
Workbooks.OpenText Filename:=ThisWorkbook.Path & "\" & FN, _
DataType:=xlDelimited, Comma:=True
' Changed from Tab to Comma delimited 9/06
ActiveCell.CurrentRegion.Copy

Workbooks(FN).Close SaveChanges:=False
Worksheets("Raw Data").Activate
Set rng = Worksheets("Raw Data").Cells(Rows.Count, 1).End(xlUp)(2)
Set rng1 = rng.Offset(1, 0)

If FNum = 1 Then
Set rng = Range("A1")
Set rng1 = Range("a2")
End If

'Range("myRange").Columns(1).Value = 0
rng.Activate
ActiveCell.PasteSpecial
If FNum 1 Then
rng.Activate
ActiveCell.EntireRow.Delete
End If
rng1.Activate
FNames(FNum, 1) = Left(FN, Len(FN) - 4)
Do While True
If ActiveCell = "" Then Exit Do
ActiveCell.Value = FNames(FNum, 1)
ActiveCell.Offset(1, 0).Activate
FNames(FNum, 2) = FNames(FNum, 2) + 1
Loop

FN = Dir
Loop
Worksheets("Files Imported").Activate
Range("a1:b" & FNum).Value = FNames
Application.ScreenUpdating = True

End Sub
  #2  
Old September 28th 06, 11:21 PM posted to microsoft.public.outlook.program_vba
John V
external usenet poster
 
Posts: 3
Default CSV Import Issue (Newbie)

Phenomenon 3) above was in the source document already, but other results are
confirmed.
  #3  
Old September 29th 06, 06:44 AM posted to microsoft.public.outlook.program_vba
Michael Bauer [MVP - Outlook]
external usenet poster
 
Posts: 1,885
Default CSV Import Issue (Newbie)

Am Thu, 28 Sep 2006 14:43:02 -0700 schrieb John V:

John, your chance for a good answer is better in an Excel group.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.VBOffice.net --


The routine below is used to import the contents of several files and

paste
those contents into a worksheet. All worked fine until the source files
changed from tab delimited to CSV files (nothing I can do about the

change).
I changed two lines of code to recognize .csv file instead of a .txt file,
then imported.

The resulting destination worksheet has unexpected results. 1) All text
cells are now preceeded by a single quote. 2) Any commas embedded in a

source
cell cause the text to be parsed into two or more destination cells, and

3)
non printing characters remain in the destination cells. None of these
phenomena appear to occur when I simply open the csv file in Excel, but it

is
the copy/paste routine below that seems to be the culprit (I think).

Any help much appreciated.
John

Code starts he

Sub Firstattempt()
Dim rng, rng1 As Range
Dim FNames(1 To 100, 1 To 2) As Variant
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim FN As String ' For File Name
Dim ThisRow As Long
Dim MyFileLocation As String
MyFileLocation = ThisWorkbook.Path & "\*.csv"
' Changed from Tab to Comma delimited 9/06
FN = Dir(MyFileLocation)
FNum = 0
Do Until FN = ""
FNum = FNum + 1
Workbooks.OpenText Filename:=ThisWorkbook.Path & "\" & FN, _
DataType:=xlDelimited, Comma:=True
' Changed from Tab to Comma delimited 9/06
ActiveCell.CurrentRegion.Copy

Workbooks(FN).Close SaveChanges:=False
Worksheets("Raw Data").Activate
Set rng = Worksheets("Raw Data").Cells(Rows.Count,

1).End(xlUp)(2)
Set rng1 = rng.Offset(1, 0)

If FNum = 1 Then
Set rng = Range("A1")
Set rng1 = Range("a2")
End If

'Range("myRange").Columns(1).Value = 0
rng.Activate
ActiveCell.PasteSpecial
If FNum 1 Then
rng.Activate
ActiveCell.EntireRow.Delete
End If
rng1.Activate
FNames(FNum, 1) = Left(FN, Len(FN) - 4)
Do While True
If ActiveCell = "" Then Exit Do
ActiveCell.Value = FNames(FNum, 1)
ActiveCell.Offset(1, 0).Activate
FNames(FNum, 2) = FNames(FNum, 2) + 1
Loop

FN = Dir
Loop
Worksheets("Files Imported").Activate
Range("a1:b" & FNum).Value = FNames
Application.ScreenUpdating = True

End Sub

  #4  
Old September 29th 06, 01:02 PM posted to microsoft.public.outlook.program_vba
John V
external usenet poster
 
Posts: 3
Default CSV Import Issue (Newbie)

LOL! My bad. Thanks for being so kind. One might have said, "Hey goober!
You're in the wrong place! No wonder your code doesn't work."

"Michael Bauer [MVP - Outlook]" wrote:

Am Thu, 28 Sep 2006 14:43:02 -0700 schrieb John V:

John, your chance for a good answer is better in an Excel group.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.VBOffice.net --


The routine below is used to import the contents of several files and

paste
those contents into a worksheet. All worked fine until the source files
changed from tab delimited to CSV files (nothing I can do about the

change).
I changed two lines of code to recognize .csv file instead of a .txt file,
then imported.

The resulting destination worksheet has unexpected results. 1) All text
cells are now preceeded by a single quote. 2) Any commas embedded in a

source
cell cause the text to be parsed into two or more destination cells, and

3)
non printing characters remain in the destination cells. None of these
phenomena appear to occur when I simply open the csv file in Excel, but it

is
the copy/paste routine below that seems to be the culprit (I think).

Any help much appreciated.
John

Code starts he

Sub Firstattempt()
Dim rng, rng1 As Range
Dim FNames(1 To 100, 1 To 2) As Variant
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim FN As String ' For File Name
Dim ThisRow As Long
Dim MyFileLocation As String
MyFileLocation = ThisWorkbook.Path & "\*.csv"
' Changed from Tab to Comma delimited 9/06
FN = Dir(MyFileLocation)
FNum = 0
Do Until FN = ""
FNum = FNum + 1
Workbooks.OpenText Filename:=ThisWorkbook.Path & "\" & FN, _
DataType:=xlDelimited, Comma:=True
' Changed from Tab to Comma delimited 9/06
ActiveCell.CurrentRegion.Copy

Workbooks(FN).Close SaveChanges:=False
Worksheets("Raw Data").Activate
Set rng = Worksheets("Raw Data").Cells(Rows.Count,

1).End(xlUp)(2)
Set rng1 = rng.Offset(1, 0)

If FNum = 1 Then
Set rng = Range("A1")
Set rng1 = Range("a2")
End If

'Range("myRange").Columns(1).Value = 0
rng.Activate
ActiveCell.PasteSpecial
If FNum 1 Then
rng.Activate
ActiveCell.EntireRow.Delete
End If
rng1.Activate
FNames(FNum, 1) = Left(FN, Len(FN) - 4)
Do While True
If ActiveCell = "" Then Exit Do
ActiveCell.Value = FNames(FNum, 1)
ActiveCell.Offset(1, 0).Activate
FNames(FNum, 2) = FNames(FNum, 2) + 1
Loop

FN = Dir
Loop
Worksheets("Files Imported").Activate
Range("a1:b" & FNum).Value = FNames
Application.ScreenUpdating = True

End Sub


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to import CSV file to contacts Gavin Outlook - Using Contacts 3 October 16th 09 09:38 PM
Import from Excel issue Jules Outlook - Using Contacts 1 September 13th 06 12:00 AM
How do I import a .csv file into Contact using a customized form Yin Outlook - Using Contacts 1 September 5th 06 06:26 PM
Trying to import CSV file into Outlook Contacts Nbebei Outlook - Using Contacts 1 June 30th 06 02:55 PM
How to import 3 CSV fields into Outlook Contacts ? Will Outlook - General Queries 1 May 16th 06 11:39 PM


All times are GMT +1. The time now is 10:45 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.