ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to Impot data from a text file to another worksheet. (https://www.excelbanter.com/excel-programming/299234-how-impot-data-text-file-another-worksheet.html)

TroyH

How to Impot data from a text file to another worksheet.
 
I have used the following code, which I found here. I am sorry I do not remember who the originator was, but the code works great except that I am having problems getting it to write to another worksheet

I need this macro to work in the background and copy the data from a text file to the worksheet "Names". The worksheet "Names" is hidden

I tried the following but it does not work. It will only copy the data onto the active worksheet

Public Sub ImportTextFile(FName As String, Sep As String

Dim RowNdx As Intege
Dim ColNdx As Intege
Dim TempVal As Varian
Dim WholeLine As Strin
Dim Pos As Intege
Dim NextPos As Intege
Dim SaveColNdx As Intege

Application.ScreenUpdating = Fals
'On Error GoTo EndMacro

SaveColNdx = Worksheets("Names").Range("A1").Colum
'SaveColNdx = ActiveCell.Column '<<< Original Cod
RowNdx = Worksheets("Names").Range("A1").Ro
'RowNdx = ActiveCell.Row '<<< Original Cod

Open FName For Input Access Read As #

While Not EOF(1
Line Input #1, WholeLin
If Right(WholeLine, 1) < Sep The
WholeLine = WholeLine & Se
End I
ColNdx = SaveColNd
Pos =
NextPos = InStr(Pos, WholeLine, Sep
While NextPos =
TempVal = Mid(WholeLine, Pos, NextPos - Pos
Cells(RowNdx, ColNdx).Value = TempVa
Pos = NextPos +
ColNdx = ColNdx +
NextPos = InStr(Pos, WholeLine, Sep
Wen
RowNdx = RowNdx +
Wen

EndMacro
On Error GoTo
Application.ScreenUpdating = Tru
Close #

End Su

Any help will be very welcome. Thanks.

Frank Kabel

How to Impot data from a text file to another worksheet.
 
Hi
untested but try the following:

Public Sub ImportTextFile(FName As String, Sep As String)

Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer
Dim wks as worksheet

set wks=activeworkbook.worksheets("Names")
Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = wks.Range("A1").Column
RowNdx = wks.Range("A1").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)
wks.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 Sub



--
Regards
Frank Kabel
Frankfurt, Germany


TroyH wrote:
I have used the following code, which I found here. I am sorry I do
not remember who the originator was, but the code works great except
that I am having problems getting it to write to another worksheet.

I need this macro to work in the background and copy the data from a
text file to the worksheet "Names". The worksheet "Names" is hidden.

I tried the following but it does not work. It will only copy the
data onto the active worksheet.

Public Sub ImportTextFile(FName As String, Sep As String)

Dim RowNdx As Integer
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 = Worksheets("Names").Range("A1").Column
'SaveColNdx = ActiveCell.Column '<<< Original Code
RowNdx = Worksheets("Names").Range("A1").Row
'RowNdx = ActiveCell.Row '<<< Original Code

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 Sub

Any help will be very welcome. Thanks.




All times are GMT +1. The time now is 11:01 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com