Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Excel VBA populates one set of cells from Textstream but ignores the next 22 sets. Tried everything I can think of

Trying to populate some cells from a TSV file. I can find no reason why this doesn't work. "Break on all errors" is set - it throws no errors. The array is perfectly populated from the textstream. The guard clauses work as intended. The column offset variable incrementing and resulting address are correct. The cells are not locked and sheet protection is not enabled anyway. And it works fine for the very first set of numbers/text/date. But for the remaining 22 sets it does nothing. I've tried the .Range("A1") and .MergedArea devices after the .Offset(...). Office365 Pro. I'm stumped.

Sub chartTextData(ByVal pathToData As String, dateEarliest As Date, Optional ByVal strFile As String)
    
    On Error GoTo LoopExit
                            Debug.Print Now() & " chartTextData BEGIN"
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    Dim TxtStream As textStream
    Dim linebuffer
    Dim myArray
    Dim cellAnchor As Range
    Dim i As Long
    Debug.Assert i = 0

    Set cellAnchor = ActiveSheet.Range("D39")   '38 on some
    
         Set TxtStream = fso.OpenTextFile(pathToData & strFile, ForReading, False, TristateUseDefault)
Do While Not TxtStream.AtEndOfStream

    linebuffer = TxtStream.ReadLine
    '  0     1     2        3            4        5   6 7
    '33.19,$F$38,good,No Need to Act,11/20/2014,2100,DB,2
    myArray = Split(linebuffer, vbTab, , vbTextCompare)
    'header row, skip it
    If myArray(1) Like "*DATE*" Then GoTo JumpHereToBypassOlderThanDateEarliest
    If myArray(1) < dateEarliest Then GoTo JumpHereToBypassOlderThanDateEarliest

    'Set cellAnchor = ActiveSheet.Range(myArray(1))  'eg, "$D$39"
    cellAnchor.Offset(0, i).Value2 = CDbl(myArray(0))  'test value
    cellAnchor.Offset(3, i).Value2 = CDate(myArray(1))  'Date
    cellAnchor.Offset(2, i).Value2 = myArray(2)  'time
    cellAnchor.Offset(4, i).Value2 = myArray(3)  'Tech
                                'Debug.Print myArray(0)
    i = i + 2                   'merged cells, 2 per
                                'Debug.Print i & " <--i"
    If i >= 46 Then GoTo LoopExit
                                    
JumpHereToBypassOlderThanDateEarliest:
Loop
    
LoopExit:
    TxtStream.Close
    Set TxtStream = Nothing
    Set fso = Nothing
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.Calculate
            Debug.Print Now() & " chartTextData END"
End Sub
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This works, and I don't know why. Not using i for column-offset, Activating top cell of each vertical range to be populated, and re-Setting cellAnchor to it.

Sub chartTextData(ByVal pathToData As String, dateEarliest As Date, Optional ByVal strFile As String)
    
    On Error GoTo LoopExit
                            Debug.Print Now() & " chartTextData BEGIN"
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    Dim TxtStream As textStream
    Dim linebuffer
    Dim myArray
    Dim cellAnchor As Range
    Dim i As Long

    Set cellAnchor = ActiveSheet.Range("D39")   '38 on some
        cellAnchor.Activate
        
         Set TxtStream = fso.OpenTextFile(pathToData & strFile, ForReading, False, TristateUseDefault)
Do While Not TxtStream.AtEndOfStream

    linebuffer = TxtStream.ReadLine
    '  0     1     2        3            4        5   6 7
    '33.19,$F$38,good,No Need to Act,11/20/2014,2100,DB,2
    myArray = Split(linebuffer, vbTab, , vbTextCompare)
    'header row, skip it
    If myArray(1) Like "*DATE*" Then GoTo JumpHereToBypassOlderThanDateEarliest
    If myArray(1) < dateEarliest Then GoTo JumpHereToBypassOlderThanDateEarliest

    cellAnchor.Offset(0, 0).Value2 = CDbl(myArray(0))  'test value
    cellAnchor.Offset(3, 0).Value2 = CDate(myArray(1))  'Date
    cellAnchor.Offset(2, 0).Value2 = myArray(2)  'time
    cellAnchor.Offset(4, 0).Value2 = myArray(3)  'Tech
                                'Debug.Print myArray(0)
    i = i + 2                   'merged cells, 2 per
    
        cellAnchor.Offset(0, 2).Activate
    Set cellAnchor = ActiveCell
    
    If i >= 46 Then GoTo LoopExit
                                    
JumpHereToBypassOlderThanDateEarliest:
Loop
    
LoopExit:
    TxtStream.Close
    Set TxtStream = Nothing
    Set fso = Nothing
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.Calculate
            Debug.Print Now() & " chartTextData END"
End Sub
```**
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!