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
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
```**