Consulting

Results 1 to 17 of 17

Thread: Removing spaces

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Removing spaces

    I have a cell that text is entered into that also is used as the name of the workbook at the time the file closes. Is there a way to let it print say "this is the file name" and when the code needs it to be the file name strip out the spaces and make it "thisisthefilename"?

    Thanks in advance


    BTW I have really learned a lot from this board and want to pass along my thanks to everyone that has contributed...

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi Austen,

    Long time no hear.

    So when is this supposed to fire? Is this a change event? Is this just a standar macro? Is this a Function?

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    In your code, wherever it is, use ..

    Replace(Range("sheet1!a1"), " ", "")
    (obviously with your own cell)
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Hey.. That won't compile. Says it is missing a =.

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    The long way ...

    Sub giveNoSpaces()
        Dim i As Long, fileName As String, newName As String
        fileName = Range("A1").Text
        For i = Len(fileName) To 1 Step -1
            Select Case Mid(fileName, i, 1)
            Case Chr$(160), Chr$(32)
                If newName = "" Then
                    newName = Left(fileName, i - 1) & Right(fileName, _
                        Len(fileName) - i)
                Else
                    newName = Left(newName, i - 1) & Right(newName, _
                        Len(newName) - i)
                End If
            Case Else
            End Select
        Next i
        MsgBox newName
    End Sub[/vba]
    The short way:
    [vba]Sub NoSpaces()
        MsgBox Replace(Range("A1").Value, " ", "")
    End Sub

  6. #6
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    This will not compile for me. Still get the "Expecting =" error


    If OptionButton1 = True Then
        Replace (Range("sheet1!b5"), " ", "")
        ThisWorkbook.SaveAs Range("B5").Value & ".xls"
        Unload Me
    End If
     
    If OptionButton2 = True Then
        Replace(Range("B5").Value, " ", "")
        ThisWorkbook.SaveAs Range("B5").Value & ".xls"
        Sheets.PrintOut
        Unload Me
    End If

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Are you trying this from a UserForm? Maybe something like this ...


    If OptionButton1 = True Then
    ThisWorkbook.SaveAs Replace(Range("sheet1!b5"), " ", "") & ".xls"
    Unload Me
    End If
    
    If OptionButton2 = True Then
    ThisWorkbook.SaveAs Replace(Range("B5").Value, " ", "") & ".xls"
    Sheets.PrintOut
    Unload Me
    End If

  8. #8
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Austen,

    The short way was giving me compile errors also, try this

    Option Explicit
    
    Sub ReplaceText()
    Range("Sheet1!A1").Replace What:=" ", Replacement:=""
    End Sub

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi John,

    I wasn't aware that you could refer to a sheet object along with the actual range like that in VBA w/o doing so with the Sheets Object (or implied); you learn something new everyday!

  10. #10
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Hey Johnske,

    while your code complies a run time error occurs:

    Run Time Error '1004'

    Method 'Range' of Object' _ Global' failed

  11. #11
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Sorry Austen,

    Forgot you're using '97, try this then

    ActiveSheet.Range("A1").Replace What:=" ", Replacement:=""
    @ Zack, yeah, only just found out 2000 accepts it

  12. #12
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by firefytr
    Hi John,

    I wasn't aware that you could refer to a sheet object along with the actual range like that in VBA w/o doing so with the Sheets Object (or implied); you learn something new everyday!
    Hmmmm,

    Just checked, it accepts it without any errors, but doesn't 'activate' Sheet1 if it's not the active sheet - oh well - better stick with the old way...
    EDIT: Ooops! Sorry, yes it DOES change sheet1 when run from Sheet2, I was trying it with the wrong macro

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Just skimmed the thread.

    Replace came in with VBA 6 (Office 2K)

    If you're using Excel 97, use ..

    WorksheetFunction.Substitute(Range("A1")," ","")
    .. instead
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Austen
    I came across the same error recently (sorry I lost my examples at home)
    MD

    Help suggests: - Replace("mystring", "ys", "xx")

    I think you need

    Sub tries()
    test = Replace("mystring", "ys", "xx")
    MsgBox test
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by austenr
    Hey Johnske,

    while your code complies a run time error occurs:

    Run Time Error '1004'

    Method 'Range' of Object' _ Global' failed
    What version did you run with? Ran good on my 2002. Curious.

  16. #16
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by firefytr
    Hi John,

    I wasn't aware that you could refer to a sheet object along with the actual range like that in VBA w/o doing so with the Sheets Object (or implied); you learn something new everyday!
    Hi Zack,

    Outta curiosity (playing around with this reference style), I tried the code below, the sub "TryIt" wrote the sheet name (with a space) in A1 on every sheet, the sub "RemoveSpaces" then removed the spaces - On office 2k - dunno if it'd work in '97

    Option Explicit
    Sub TryIt()
    Dim N%
    For N = 1 To 3
    Range("Sheet" & N & "!A1") = "Sheet " & N
    Next N
    End Sub
     
    Sub RemoveSpaces()
    Dim N%
    For N = 1 To 3
    Range("Sheet" & N & "!A1").Replace What:=" ", Replacement:=""
    Next N
    End Sub

  17. #17
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    PS

    Zack, this also works. (i.e. runs the code in another workbook if that workbook is open) - it's the same referencing as is used on a worksheet when pasting a link

    Option Explicit

    Sub TryIt()
    Dim N%
    For N = 1 To 3
    Range("[AnotherWorkbook.xls]Sheet" & N & "!A1") = "Sheet " & N
    Next N
    End Sub
     
    Sub RemoveSpaces()
    Dim N%
    For N = 1 To 3
    Range("[AnotherWorkbook.xls]Sheet" & N & "!A1").Replace What:=" ", Replacement:=""
    Next N
    End Sub
    (the use of .xls appears to be optional)

Posting Permissions

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