|
-
Jun 9th, 2010, 02:31 PM
#1
Thread Starter
Hyperactive Member
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
-
Jun 10th, 2010, 07:41 AM
#2
Re: Looping Until Text Not Found
 Originally Posted by fEtchboi88
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
-
Jun 10th, 2010, 07:46 AM
#3
Thread Starter
Hyperactive Member
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?
-
Jun 10th, 2010, 08:03 AM
#4
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
-
Jun 10th, 2010, 08:08 AM
#5
Thread Starter
Hyperactive Member
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
-
Jun 10th, 2010, 08:31 AM
#6
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
-
Jun 10th, 2010, 08:46 AM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|