Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 160
Default Improve speed of data transfer loop

Can this be done more efficiently?

' Routine moves data from text file into excel spreadsheet cells
buildsheet:
RowNdx = 3
Open MTOFile 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 = 1
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
Close #1
Return
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 256
Default Improve speed of data transfer loop

On Feb 14, 3:34 pm, patrick wrote:
Can this be done more efficiently?

' Routine moves data from text file into excel spreadsheet cells
buildsheet:
RowNdx = 3
Open MTOFile 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 = 1
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
Close #1
Return


Something like this. Declare a variant

Dim v as Variant
v = Split(WholeLine, sep)
Cells(RowNdx, 1).Resize(1, Ubound(v)).Value = v

That way you avoid looping since the functionality is already built
in.

Here was my test:

Public Sub mySub1()
Dim WholeLine As String
Dim sep As String

sep = ","
RowNdx = 1
Dim rng As Range
Dim v As Variant


WholeLine = "abc,def,ghi,jkl,mno,pqr,"
v = Split(WholeLine, sep)
ColNdx = 1
Pos = 1
Cells(RowNdx, 1).Resize(1, UBound(v)).Value = v

End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 256
Default Improve speed of data transfer loop

Cleaning that up a bit:

Public Sub mySub1()
Dim WholeLine As String
Dim sep As String
Dim RowNdx As Long
Dim v As Variant

sep = ","
RowNdx = 1

WholeLine = "abc,def,ghi,jkl,mno,pqr,"
v = Split(WholeLine, sep)
ColNdx = 1
Cells(RowNdx, 1).Resize(1, UBound(v)).Value = v
End Sub



On Feb 14, 5:03 pm, ilia wrote:
On Feb 14, 3:34 pm, patrick wrote:



Can this be done more efficiently?


' Routine moves data from text file into excel spreadsheet cells
buildsheet:
RowNdx = 3
Open MTOFile 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 = 1
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
Close #1
Return


Something like this. Declare a variant

Dim v as Variant
v = Split(WholeLine, sep)
Cells(RowNdx, 1).Resize(1, Ubound(v)).Value = v

That way you avoid looping since the functionality is already built
in.

Here was my test:

Public Sub mySub1()
Dim WholeLine As String
Dim sep As String

sep = ","
RowNdx = 1
Dim rng As Range
Dim v As Variant

WholeLine = "abc,def,ghi,jkl,mno,pqr,"
v = Split(WholeLine, sep)
ColNdx = 1
Pos = 1
Cells(RowNdx, 1).Resize(1, UBound(v)).Value = v

End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Improve the speed of copy-paste Alex St-Pierre Excel Programming 3 March 30th 07 12:50 AM
Improve Speed by desabling Automatic Calculation Alex St-Pierre Excel Programming 4 March 14th 06 03:55 PM
Using an Array instead of a Vlookup to improve speed Frank & Pam Hayes[_2_] Excel Programming 4 August 10th 05 08:01 PM
How can I Improve query speed? John[_60_] Excel Programming 5 October 12th 04 01:00 PM
Howto Improve speed? Andy Excel Programming 0 January 19th 04 04:39 PM


All times are GMT +1. The time now is 02:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"