VBA: Question

SteveOranjin

Board Regular
Joined
Dec 18, 2017
Messages
170
Hello There,

I'm learning a lot about VBA but I have a quick question.

If a macro can be performed more than once on a given cell, will it be performed as many times as the rules which govern it allow? Or will it only make that initial run through and then have to be run again to get the rest.

An example: If I have fifty spaces in a cell, and I set up a macro to turn all Double Spaces (that is "_""_" lines added for clarity) into single spaces - will the macro run and reduce the spaces in the cell from 50 to 25, and then proceed to the next cell and perform the function there, or if it reduces the number of spaces to 25, and detects that there are more spaces, it will continue to delete them until there are none left.

Steve
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
you could run this twice
Code:
    Range("A1:J10000").Select
    Selection.Replace What:="  ", Replacement:=" ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    Selection.Replace What:="  ", Replacement:=" ", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
 
Upvote 0
you could run this twice
Running it just twice won't get you down from 50 to 1.

You could make use of the Find command, which returns an error if it cannot find what it is looking for. Then, we could put the code in a Loop, and tell it to exit when it finds an error (meaning that there are no more to find).

That code might look something like this:
Code:
Sub GetRidOfMultipleSpaces()

    On Error GoTo exit_loop
    Do
        Range("A1:J10000").Find(What:="  ", After:=Range("A1"), LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).Activate
        Range("A1:J10000").Replace What:="  ", Replacement:=" ", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    Loop
    
exit_loop:
    Err.Clear
    On Error GoTo 0
    
    MsgBox "Done!"
        
End Sub
 
Last edited:
Upvote 0
If you are trying to trim the contents of cells, you can use Application.Trim in a loop through the cells, rather than having to process the cells repeatedly.
 
Upvote 0
Depending on what your actual goal is, you could use one of the built-in functions.

Code:
    Range("A1").Value = Worksheetfunction.Trim(Range("A1").Value)

will compress all multiple spaces down to 1. A string of all spaces will turn to an empty string though.
 
Upvote 0
If you are trying to trim the contents of cells, you can use Application.Trim in a loop through the cells, rather than having to process the cells repeatedly.
I have used TRIM many times for extra spaces at the beginning or end of entries. I am embarrassed to say that I never realized it would work on spaces in the middle of entries too!:oops:

So I learned something new today!
 
Upvote 0
I have used TRIM many times for extra spaces at the beginning or end of entries. I am embarrassed to say that I never realized it would work on spaces in the middle of entries too!:oops:

So I learned something new today!

I just want to make sure I am understanding this correctly. It will only go through the first iteration of that and reduce the number of spaces down to 25, correct?
 
Upvote 0
I am not sure which one you want to happen, just go down to 25 and go all the way down to 1.

If you just want 25, then simply run through it once, like:
Code:
        Range("A1:J10000").Replace What:="  ", Replacement:=" ", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False

If you want it to go down to 1, using the TRIM method mentioned by Rory and Fluff would be the most efficient way.
 
Last edited:
Upvote 0
If you want it to go down to 1, using the TRIM method mentioned by Rory and Fluff would be the most efficient way.
Shouldn't that be Rory & Eric ;)
 
Upvote 0
Shouldn't that be Rory & Eric ;)

I wasn't looking for an answer. What I was curious about was the answer to the question. I wanted to know if - from a conceptual standpoint - each VBA code is performed only once or if it was performed in each cell it evaluates until there are no more evaluations for it to make.

Steve
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top