Len function puzzle

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Len function puzzle

Post by LisaGreen »

Hi,

I'm reading a file line by line using
Open spFile For Binary Lock Read Write As #1

I'm using this code to print out the line..

Code: Select all

Sub subDumpStringToExcelSheet( _
    strpLine As String, _
    Optional vpLineNum As Variant _
    )
' Dump a string.

Dim intlI As Integer
Dim strlLine As String
Dim intlICols As Integer
Dim intlRows As Integer
Dim objlRange As Object
Dim intlCol As Integer
Dim intlTableNum As Integer
Dim intlRemainder As Integer
Dim intlColsDone As Integer
Dim strlLineNum As String
Dim slChr As String * 1

If IsMissing(vpLineNum) Then
  strlLineNum = "No Line number given"
Else
  strlLineNum = "Line " & CStr(vpLineNum)
End If

intlTableNum = 0
strlLine = strpLine
Sheets.Add

ActiveCell.Value = "Line >" & strlLine & "<"  '& vbCrLf & vbCrLf
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = "Length =" & Len(strlLine)  '& vbCrLf & vbCrLf
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = strlLineNum  '& vbCrLf & vbCrLf
ActiveCell.Offset(2, 1).Select

intlI = 1
intlRows = 1
intlCol = 12
Do
  If intlCol > 11 Then
    ActiveCell.Offset(4, -1).Select

    ActiveCell.Value = intlI & " To " & intlI + 9  '& vbCrLf
    ActiveCell.Offset(1, 0).Select

    intlCol = 2
    intlTableNum = intlTableNum + 1

    ActiveCell.Value = "Chr"
    ActiveCell.Offset(1, 0).Select

    ActiveCell.Value = "Asc"
    ActiveCell.Offset(1, 0).Select

    ActiveCell.Value = "Chr #"
    ActiveCell.Offset(-2, 1).Select

  End If

  slChr = Mid$(strlLine, intlI, 1)

  ActiveCell.Offset(0, intlCol) = slChr

  ActiveCell.Offset(1, intlCol) = Asc(slChr)

  ActiveCell.Offset(2, intlCol) = intlI

  intlCol = intlCol + 1
  intlI = intlI + 1

Loop Until intlI > Len(strlLine)

MsgBox "@Done."
' ***********************************************************************
End Sub
I'm getting this result..


Line ><
Length =0
No Line number given





1 To 10
Chr
Asc 32
Chr # 1

?Len(line)
.. in the immediate window also gives zero length.
Can anyone tell me why a line with a space on it has length 0 please?

TIA
Lisa

User avatar
HansV
Administrator
Posts: 78465
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Len function puzzle

Post by HansV »

Line >< indicates that strpLine is empty.

The 32 is an artefact - the line

slChr = Mid$(strlLine, intlI, 1)

refers to a non-existing character, so apparently it defaults to a space. Instead of using

Do
...
Loop Until intlI > Len(strlLine)

use

Do While intlI <= Len(strlLine)
...
Loop

or

For intlI = 1 to Len(strlLine)
...
Next intlI
Best wishes,
Hans

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Len function puzzle

Post by LisaGreen »

Thank you Hans..

I'm altering the counter in the loop so Can't ues for..Next.

Lisa

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Len function puzzle

Post by rory »

Why do you declare slChr as a 1 character fixed length string?
Regards,
Rory

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Len function puzzle

Post by LisaGreen »

Hi Rory.... Meant to be a single character..

... And you've made me think now.

Reading byte by byte is sloooow.

Lisa

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Len function puzzle

Post by rory »

If you declare it as a fixed-length, it will always be that length, even if you try and assign a zero-length string to it...
Regards,
Rory

LisaGreen
5StarLounger
Posts: 964
Joined: 08 Nov 2012, 17:54

Re: Len function puzzle

Post by LisaGreen »

Thank you Rory!

Lisa