Consulting

Results 1 to 10 of 10

Thread: Solved: Assistance with character counting (Excel '97)

  1. #1
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location

    Solved: Assistance with character counting (Excel '97)

    Hi there,

    i'm using the following to count the amount of characters entered into a field.
    [vba]Private Sub txtComments_Change()
    Dim tCount
    tCount = Len(Me.txtComments.Value)
    Me.txtCount.Value = tCount
    End Sub[/vba]
    Using the above it counts line breaks as 2 characters.
    How can I modify the above code to either count them as 1 or ignore them completely?

    Thanks,
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How are you inserting linebreaks in the textbox?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Quote Originally Posted by xld
    How are you inserting linebreaks in the textbox?
    Yes, when the user presses the [Enter] key
    BTW, is your avatar coming back?
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub txtComments_Change()
    Dim tCount
    tCount = Len(Replace(Me.txtComments.Value, vbCrLf, ""))
    Me.txtCount.Value = tCount
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by phendrena
    BTW, is your avatar coming back?
    Unlikely, it was lost when VBAX was down recently, and I did not create it, so I don't have a copy.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Quote Originally Posted by xld
    [vba]

    Private Sub txtComments_Change()
    Dim tCount
    tCount = Len(Replace(Me.txtComments.Value, vbCrLf, ""))
    Me.txtCount.Value = tCount
    End Sub
    [/vba]
    Errors with "sub or function not defined" and highlights Replace.

    Is replace not supported in '97?

    Thanks,
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, actually it isn't.

    Put this code in a standard code module.

    [vba]

    '-----------------------------------------------------------------
    Function Replace(expression As String, _
    find_string As String, _
    replacement As String)
    '-----------------------------------------------------------------
    Dim i As Long
    Dim iLen As Long
    Dim iNewLen As Long
    Dim sTemp As String


    sTemp = expression
    iNewLen = Len(find_string)
    For i = 1 To Len(sTemp)
    iLen = Len(sTemp)
    If Mid(sTemp, i, iNewLen) = find_string Then
    sTemp = Left(sTemp, i - 1) & replacement & Right(sTemp, iLen - i - iNewLen + 1)
    i = i + iNewLen - 1
    End If
    Next i

    Replace = sTemp

    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    thanks. I'll have a crack tomorrow as i'm currently heading home on the bus and as much as i'd like it I don't have a VBA compatible office program - ms office mobile is pants. I do have softoffice which is much better but lacks a vba environment
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  9. #9
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Quote Originally Posted by xld
    Unlikely, it was lost when VBAX was down recently, and I did not create it, so I don't have a copy.
    Marble effect with embossed roman style typeface if I remember? i'll see what I can create although i'm no artist
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  10. #10
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Thanks xld, that does work quite nicely now.

    On a side note, i've created a basic 'xld' avatar.
    It'll need resizing to meet the board requirements but i thought you might want to see it larger first. As i said though i'm no artist.
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •