Perform text manipulation on each cell currently selected

z3115

Board Regular
Joined
Nov 1, 2013
Messages
71
I often times encounter raw data where the date in the format YYYYQ (year then quarter, ex: "20193" for the third quarter of 2019). For formatting/presentation purposes I have to add the letter "Q" before the quarter to make it clearer (ex 20193 becomes 2019Q3). If there a macro that would do this automatically for the current selected range? For example, if I have range A1:A10 selected (each containing a date in YYYYQ format), is there a way to have it add the Q in the correct position for all 10 cells in the range? Thank you!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
How about
VBA Code:
Sub z3115()
   With Selection
      .Value = Evaluate("if({1},replace(" & .Address & ",5,0,""Q""))")
   End With
End Sub
 
Upvote 0
This worked perfectly, thank you! for my own edification, can you explain how the portion below is working? Also how does the "with selection" approach differ from a "for each cell in selection" approach?

Sorry for the follow up questions, I just want to understand it better so I can become more independent.

VBA Code:
Evaluate("if({1},replace(" & .Address & ",5,0,""Q""))")



How about
VBA Code:
Sub z3115()
   With Selection
      .Value = Evaluate("if({1},replace(" & .Address & ",5,0,""Q""))")
   End With
End Sub
 
Upvote 0
Using Evaluate saves the need for looping through the cells. It can either be a dynamic range, or using Selection.

Evaluate is calculating the worksheet formula
=REPLACE(M2,5,0,"Q")
and putting the result into the cell, rather than a neighbouring cell.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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