PC Review


Reply
Thread Tools Rate Thread

CPearsons import text code

 
 
test1@junkmail.com
Guest
Posts: n/a
 
      2nd Mar 2008
Using the code below works well except that it drops the lead zeros
for a couple of the columns. Is their a way to retrieve exactly whats
in the text file.

Thanks

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ImportTextFile
' This imports a text file into Excel.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub ImportTextFile(FName As String, Sep As String)

Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' END ImportTextFile
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub

 
Reply With Quote
 
 
 
 
Hemant_india
Guest
Posts: n/a
 
      2nd Mar 2008
hi
instead of Cells(RowNdx, ColNdx).Value = TempVal
use Cells(RowNdx, ColNdx).text = TempVal
might be helpfull ...not sure though
--
hemu


"(E-Mail Removed)" wrote:

> Using the code below works well except that it drops the lead zeros
> for a couple of the columns. Is their a way to retrieve exactly whats
> in the text file.
>
> Thanks
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ' ImportTextFile
> ' This imports a text file into Excel.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> Public Sub ImportTextFile(FName As String, Sep As String)
>
> Dim RowNdx As Long
> Dim ColNdx As Integer
> Dim TempVal As Variant
> Dim WholeLine As String
> Dim Pos As Integer
> Dim NextPos As Integer
> Dim SaveColNdx As Integer
>
> Application.ScreenUpdating = False
> 'On Error GoTo EndMacro:
>
> SaveColNdx = ActiveCell.Column
> RowNdx = ActiveCell.row
>
> Open FName For Input Access Read As #1
>
> While Not EOF(1)
> Line Input #1, WholeLine
> If Right(WholeLine, 1) <> Sep Then
> WholeLine = WholeLine & Sep
> End If
> ColNdx = SaveColNdx
> Pos = 1
> NextPos = InStr(Pos, WholeLine, Sep)
> While NextPos >= 1
> TempVal = Mid(WholeLine, Pos, NextPos - Pos)
> Cells(RowNdx, ColNdx).Value = TempVal
> Pos = NextPos + 1
> ColNdx = ColNdx + 1
> NextPos = InStr(Pos, WholeLine, Sep)
> Wend
> RowNdx = RowNdx + 1
> Wend
>
> EndMacro:
> On Error GoTo 0
> Application.ScreenUpdating = True
> Close #1
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ' END ImportTextFile
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> End Sub
>
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      2nd Mar 2008
You could format the range that gets thes values as text.

with Cells(RowNdx, ColNdx)
.numberformat = "@"
.Value = TempVal
end with

But I'm not sure that I'd do that for all the fields--if you're importing dates
or times or money or ... (where you don't want any leading 0's), it might cause
problems down the road.

Have you though of leaving the cells formatted as General while you're doing the
import, then changing the number format for the fields you want to display
leading 0's?

After the import...
activesheet.range("a1,e1,j1").entirecolumn.numberformat = "00000"
(to get 3 columns to show 5 digits)

"(E-Mail Removed)" wrote:
>
> Using the code below works well except that it drops the lead zeros
> for a couple of the columns. Is their a way to retrieve exactly whats
> in the text file.
>
> Thanks
>
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ' ImportTextFile
> ' This imports a text file into Excel.
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> Public Sub ImportTextFile(FName As String, Sep As String)
>
> Dim RowNdx As Long
> Dim ColNdx As Integer
> Dim TempVal As Variant
> Dim WholeLine As String
> Dim Pos As Integer
> Dim NextPos As Integer
> Dim SaveColNdx As Integer
>
> Application.ScreenUpdating = False
> 'On Error GoTo EndMacro:
>
> SaveColNdx = ActiveCell.Column
> RowNdx = ActiveCell.row
>
> Open FName For Input Access Read As #1
>
> While Not EOF(1)
> Line Input #1, WholeLine
> If Right(WholeLine, 1) <> Sep Then
> WholeLine = WholeLine & Sep
> End If
> ColNdx = SaveColNdx
> Pos = 1
> NextPos = InStr(Pos, WholeLine, Sep)
> While NextPos >= 1
> TempVal = Mid(WholeLine, Pos, NextPos - Pos)
> Cells(RowNdx, ColNdx).Value = TempVal
> Pos = NextPos + 1
> ColNdx = ColNdx + 1
> NextPos = InStr(Pos, WholeLine, Sep)
> Wend
> RowNdx = RowNdx + 1
> Wend
>
> EndMacro:
> On Error GoTo 0
> Application.ScreenUpdating = True
> Close #1
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> ' END ImportTextFile
> ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
> End Sub


--

Dave Peterson
 
Reply With Quote
 
ward376
Guest
Posts: n/a
 
      3rd Mar 2008
Try using .formula instead of .value

Cliff Edwards
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
import from text file with VBA code sheela Microsoft Excel Programming 5 18th Aug 2009 08:10 PM
VBA code to import stacked text files =?Utf-8?B?U3BvcnR5Smlt?= Microsoft Excel Programming 3 23rd Feb 2007 09:50 PM
How to Use Chip Pearson's Text Import Code socrtwo@gmail.com Microsoft Excel Programming 12 23rd Aug 2006 02:28 PM
Import HTML code from a text file. nathpai@walla.com Microsoft Excel Programming 2 1st Apr 2005 07:47 AM
import delimited text via code? SirPoonga Microsoft Access 5 25th Feb 2005 08:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:48 AM.