Delete 5th character in text string in range only when preceding characters are "Day "

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

I12:I8669 contains text entries and I need to delete the 5th character in all cells in that range but only where the preceding characters are "Day " (Day + space).

What follows those characters is a zero (which I need deleting), followed by a unique number. I can't use search and replace to remove the leading zero as the characters "Day " can also appear later in the text string in some cells.

Hope you can help?

Many thanks!
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try this:

VBA Code:
Sub deletechar5()
  With Range("I12:I8669")
    .Value = Evaluate(Replace("=IF({1},IF(@="""","""",IF(LEFT(@,4)=""Day "",REPLACE(@,5,1,""""),@)))", "@", .Address))
  End With
End Sub
 
Upvote 0
@Eric... base of the OP's hint that it is a leading 0 that he wants to delete, I think changing your formula to this might be better (just in case there is no leading zero)...
Excel Formula:
=IF(LEFT(I12,5)="Day 0",REPLACE(I12,5,1,""),I12)
 
Last edited:
Upvote 0
Solution
In case you decide on a formula, here is a slightly shorter version. Also consider if the cell is empty, the result will be empty, which with Erick's formula will probably return a 0 if you have configured: Show a zero in cells that have zero value.

varios 16sep2021.xlsm
IJK
12Day 0123Day 123Day 123
13Day 1234Day 1234Day 234
14somesomesome
15 0
16Day 999Day 999Day 99
Hoja2
Cell Formulas
RangeFormula
J12:J16J12=REPLACE(I12,5,IF(LEFT(I12,5)="Day 0",1),"")
K12:K16K12=IF(LEFT(I12,4)="Day ",REPLACE(I12,5,1,""),I12)
 
Upvote 0
Many thanks for your solutions gentlemen.

@DanteAmor: I ran your code and it unfortunately accumulated digits for the cells that didn't contain "Day 0". Your formula works as per Eric's (great minds think alike :) )
@eric: Thanks ever so much, your formula did the job with the leading zeros but unfortunately returned zeros for the other entries.
@Rick: Your formula worked perfectly - thank you ever so much!

Thanks once again for all your continued help and time gentlemen.
 
Upvote 0
I ran your code and it unfortunately accumulated digits for the cells that didn't contain "Day 0".
Try the updated code:

VBA Code:
Sub deletechar5()
  With Range("I12:I8669")
    .Value = Evaluate(Replace("=IF({1},IF(@="""","""",IF(LEFT(@,5)=""Day 0"",REPLACE(@,5,1,""""),@)))", "@", .Address))
  End With
End Sub

Your formula works as per Eric's

You mean this formula, I think it gets the expected result.
Excel Formula:
=REPLACE(I12,5,IF(LEFT(I12,5)="Day 0",1),"")
 
Upvote 0
Try the updated code:

VBA Code:
Sub deletechar5()
  With Range("I12:I8669")
    .Value = Evaluate(Replace("=IF({1},IF(@="""","""",IF(LEFT(@,5)=""Day 0"",REPLACE(@,5,1,""""),@)))", "@", .Address))
  End With
End Sub
That works perfectly now, thank you!

You mean this formula, I think it gets the expected result.

Excel Formula:
=REPLACE(I12,5,IF(LEFT(I12,5)="Day 0",1),"")
Yes, apologies, that works perfectly as well! Thanks again!
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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