Results 1 to 7 of 7

Thread: Looping Until Text Not Found

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Looping Until Text Not Found

    I have some code that I want to continuously loop while double spaces are found, and replace them with a single space.

    Is there a way to do this? I can't seem to find any information on what the Find method in VBA returns (or what the replace method returns)

    Here's my code (note, this is actually in Visual Studio 2008 in an excel addin):
    Code:
            bDoubles = Globals.ThisAddIn.Application.Selection.find(What:=Space(2))
            If Not bDoubles Is Nothing Then
                Do Until bDoubles Is Nothing
                    System.Windows.Forms.Application.DoEvents()
                    bDoubles.Activate()
                    Globals.ThisAddIn.Application.Selection.replace(What:=Space(2), Replacement:=Space(1))
                    bDoubles = Globals.ThisAddIn.Application.Selection.find(What:=Space(2))
                Loop
            End If

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Looping Until Text Not Found

    Quote Originally Posted by fEtchboi88 View Post
    I can't seem to find any information on what the Find method in VBA returns (or what the replace method returns)
    Excel VBA Find
    Excel VBA Replace

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: Looping Until Text Not Found

    That's not quite what I'm looking for (I don't think).

    That Find function returns the position within a string that the text is found - is there a way to search the entire spreadsheet, and return the Cell that the text is found in?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Looping Until Text Not Found

    and return the Cell that the text is found in
    i believe, should be bdoubles.address
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: Looping Until Text Not Found

    I'll give that a shot, thank you.

    I have a temporary work-around in place at the moment, but it's pointing to all of the cells that contain dates in the format:
    mm/dd/yyyy hh:mm:ss

    ...since there are two spaces between the year and the hour. The only problem is, when it replaces them with one space they don't actually replace resulting in an infinite loop.

    I tried throwing in a "if blahblah.NumberFormat = "m/d/yyyy"" (that's how they're formatted), but it's not going to help for dates formatted a different way.

    Does anyone have any thoughts on either how to ignore these, or get the 2 spaces to replace with one?

    Thanks,
    fEtch

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Looping Until Text Not Found

    Try this:
    Code:
    With Globals.ThisAddIn.Application.ActiveSheet.Cells
        Do Until .Find(What:="  ", LookIn:=xlValues, LookAt:=xlPart, SearchFormat:=False) Is Nothing
            .Replace What:="  ", Replacement:=" ", LookAt:=xlPart, SearchFormat:=False, ReplaceFormat:=False
        Loop
    End With
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: Looping Until Text Not Found

    That worked beautifully for the dates - however another issue was born from that.

    I now have cells with data formatted as "Accounting", which is displayed as
    "$ 1.24"

    If I change your code to LookIn:=Excel.XlFindLookIn.xlFormulas, the "Accounting" cells are not an issue, but the dates are - if I leave it as LookIn:=Excel.XlFindLookIn.xlValues, the dates are an issue and the Accounting cells are not. Any other suggestions? lol

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