Add Space after second "*" if there is no space afterwards

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
Hi,
I asked that same question on my previous post but thought it would be better to start a new post for this.

I got in one column "Names" they can start with an asterix "*p* Sometext"

this can very from "*p* sometext
to *E* Sometext

or other text with *different length* somthing like "*TOP S+E* sometext"

So I like to find the second "*" from left and add a space if there is no space.

"*TOP S+E*textwithoutSpace after the asterisk
of course I need to keep the remaining Text.

So I guess I should use instr Function to find the second "*" but then how to I find if there is a space or no space after the asterisk and how to add one if there is non ??

Hope that is clear what I mean?

Cheers ..)
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Could we have some varied sample data and expected results with XL2BB?

Does this have to be with vba or would a formula solution be acceptable?
 
Upvote 0
You can easily achieve this using formulas.

Here is the Logic

1. Find the first "*". You can do that using the formula SEARCH()

Excel Formula:
=SEARCH("~*",A1,1)

2. Find then 2nd "*". You can do that using the formula

Excel Formula:
=SEARCH("~*",A2,SEARCH("~*",A2,1)+1)

3. To find if the character after the 2nd "*" is space or not you can use MID() with the above.

Excel Formula:
=IF(MID(A2,SEARCH("~*",A2,SEARCH("~*",A2,1)+1)+1,1)=" ","There is space","There is no space")

Here is an example

1661248873729.png


PS: I am not doing any error handling. I am sure you can take care of it?
 
Upvote 0
Alternative 2

After the first "*", do not search for "*" but search for "* ". Notice the SPACE after "*"?

Here is the Logic

1. Find the first "*". You can do that using the formula SEARCH()

Excel Formula:
=SEARCH("~*",A1,1)

2. Find the asterix with SPACE ("* "). You can do that using the formula

Excel Formula:
=SEARCH("~* ",A2,SEARCH("~*",A2,1)+1)

3. Final formula

Excel Formula:
=IFERROR(IF(SEARCH("~* ",A1,SEARCH("~*",A1,1)+1)>0,"There is space","There is no space"),"There is no space")

1661249240033.png
 
Upvote 0
Hi thanks for your replies!

I need to add a space if there is no space and if there is a space just leave it like the text is.

I would prefer it in vba to be able to use it in future as there is every few month new prices and I need to update those.

*P* BGR. Drucktasterbedienfeld 6-Punkte incl. Steuerung Ergo+ 2015

*P* BGR. Drucktasterbedienfeld 6-Punkte incl. Steuerung Ergo+ 2015

*P*BGR. Drucktasterbedienfeld 6-Punkte incl. Steuerung Ergo+ 2015

*P* BGR. Drucktasterbedienfeld 6-Punkte incl. Steuerung Ergo+ 2015
*E*Abdeckring VS-Düse
*E* Abdeckring VS- Düse
*P*Heizkörper 1 1/2" 4500 W 230/400 V -Restbestand-nicht mehr lieferbar-*P* Heizkörper 1 1/2" 4500 W 230/400 V -Restbestand-nicht mehr lieferbar-
*TOP-S+E*Fernbedienung Funk Reviva*TOP-S+E* Fernbedienung Funk Reviva


Left is the Data and right is how I like to have it in the hole Column )


So all of the "Text" needs to have a space after the Second "*"
 
Upvote 0
If you want VBA then you can simply use Peter's formula and adapt it to VBA?
 
Upvote 0
Hi Peter,

Yes that works in a formula ! Thanks just had to updated it to a german version of Excel but it is all good!

Still it would be nice to have that in a function so i could used it to automatically update the received Sheets in future.

Cheers
 
Upvote 0
Assuming that data is in column A, starting in row 2, try this with a copy of your data. (It over-writes the original data)

VBA Code:
Sub SpaceAfterSecondAsterisk()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("trim(substitute(" & .Address & ",""*"",""* "",2))")
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

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