Results 1 to 4 of 4

Thread: help with code

  1. #1

    Thread Starter
    Lively Member fundean's Avatar
    Join Date
    Apr 2001
    Posts
    98

    help with code

    The following code:

    Public Function RemoveSpaces(strInput As String)
    ' Removes all spaces from a string of text
    Test:
    If InStr(strInput, " ") = 0 Then
    RemoveSpaces = strInput
    Else
    strInput = Left(strInput, InStr(strInput, " ") - 1) _
    & Right(strInput, Len(strInput) - InStr(strInput, " "))
    GoTo Test
    End If
    End Function

    will apprently remove spaces from a text string in an Excel spreadsheet. I would like to modify it so that it removes quotes from a text string, and removes it ONLY for a particular column (say, column A). when I replace the second parameter of the InStr function from " " to " "" ", it gives me a "stack overflow" error. Help !!!

    fundean

  2. #2
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: help with code

    How about just running a sub to kill it off?

    VB Code:
    1. Option Explicit
    2.  
    3. Sub KillTheQuotes()
    4.     Dim szKill As String
    5.     szKill = Chr(34)
    6.    
    7.     Dim r As Range
    8.    
    9.     For Each r In Range("A1:A100")
    10.    
    11.         r.Value = Replace(r.Value, szKill, Empty)
    12.        
    13.     Next r
    14.    
    15. End Sub


    I set the range at A1-A100, doing a For Each loop through the entire column will be a bit slow, but this can be modified for a varying range if that's what you need.
    Justin Labenne
    www.jlxl.net

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: help with code

    That's what I would recommend too, just with a couple of amendments:

    You can set the range of your For loop to automatically go through all the rows in the sheet that have data in them, by using UsedRange.

    Also, you should not use Empty as a value, you should use an empty string ( "" ) instead.
    VB Code:
    1. Option Explicit
    2.  
    3. Sub KillTheQuotes()
    4.     Dim szKill As String
    5.     szKill = Chr(34)
    6.    
    7.     Dim r As Range
    8.    
    9.     For Each r In Range("A1:A" & UsedRange.Rows.Count)
    10.    
    11.         r.Value = Replace(r.Value, szKill, "")
    12.        
    13.     Next r
    14.    
    15. End Sub

  4. #4
    Lively Member JustinLabenne's Avatar
    Join Date
    Jul 2005
    Location
    Ohio
    Posts
    64

    Re: help with code

    From msdn on using Empty:

    Indicates that no beginning value has been assigned to a Variant variable. An Empty variable is represented as 0 in a numeric context or a zero-length string ("") in a string context.
    Justin Labenne
    www.jlxl.net

Posting Permissions

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



Click Here to Expand Forum to Full Width