Expand Range of Values to Individual Values

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello Friends, I need a solution for my complicated requirement. I have specific format of value range. I need to expand the values as individual values.
Example: Input - 1to10. Output - 1 2 3 4 5 6 7 8 9 10 this is a simple example. My specific format is **150**155. The ** replaces by any of the value it may be numerical or text but the last 3 digits is always numerical value which is the range to expand.
Example: Input - WK150WK155. Output - WK150WK150 WK151WK151 WK152WK152 WK153WK153 WK154WK154 WK155WK155. Please anyone heads up and solve my issue. Thank you.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this UDF:-
Where R = the cell with the string
Code:
Function Mystr(R [COLOR=navy]As[/COLOR] Range) [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]Dim[/COLOR] n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] nStr [COLOR=navy]As[/COLOR] [COLOR=navy]String,[/COLOR] nWd [COLOR=navy]As[/COLOR] Variant
[COLOR=navy]Dim[/COLOR] wSt [COLOR=navy]As[/COLOR] [COLOR=navy]String,[/COLOR] Ed [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Num1 [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] num2 [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]With[/COLOR] R
    [COLOR=navy]For[/COLOR] n = 1 To Len(.Value)
        [COLOR=navy]If[/COLOR] n = 1 [COLOR=navy]Then[/COLOR] wSt = Left(.Value, 1)
        If Mid(.Value, n + 1, 1) = wSt Then Ed = n 
    [COLOR=navy]Next[/COLOR] n
    Num1 = Mid(.Value, Ed - 2, 3): num2 = Right(.Value, 3)
    [COLOR=navy]For[/COLOR] nWd = Num1 To num2
        nStr = nStr & Left([a1], Ed - 3) & nWd & Left([a1], Ed - 3) & nWd & " "
    [COLOR=navy]Next[/COLOR] nWd
Mystr = Trim(nStr)
[COLOR=navy]End[/COLOR] With
[COLOR=navy]End[/COLOR] Function
Regards Mick
 
Last edited:
Upvote 0
Hello Mick, Thanks for your solution. I don't have much knowledge in macros. Could you please guide me how to use this.
 
Upvote 0
Try this:-

To Save and Run Function:-
Copy code from Thread
In Your Data sheet , Click"Alt+F11",:- Vb Window appears.
From the VBWindow toolbar, Click"Insert" ,"Module":- New VBwindow appears .
Paste Code into this window.
Close Vbwindow.

Assuming Basic string is in"A1".

Then In "B1" Write :- =Mystr(a1)
Click "Enter"
"B1" should now show Expectedresults.

Regrds Mick
 
Last edited:
Upvote 0
Hi Mick, i did as per your instructions but i am not getting any results. I have doubt on the second paragraph you entered. Where i have to give my input? Could you please clarify on this?
 
Upvote 0
Thanks Mick. Working Great. First time seeing this type of codes. I am just familiar with basic macros which starts with Sub and end with End Sub. Thanks again.
 
Upvote 0
Hello Mick this value 1A5501A551 is not expanding. I cant figure out the issue. One more issue if i have multiple values in a cell it is not working for example WK150WK151 WK155WK156 the mentioned value should expand like this WK150WK150 WK151WK151 WK155WK155 WK156WK156. But its not expanding like i mentioned. Please help me. Thank you.
 
Upvote 0
Hello Mick this value 1A5501A551 is not expanding. I cant figure out the issue. One more issue if i have multiple values in a cell it is not working for example WK150WK151 WK155WK156 the mentioned value should expand like this WK150WK150 WK151WK151 WK155WK155 WK156WK156. But its not expanding like i mentioned. Please help me. Thank you.
Try replacing the function Mick gave you with this one and see if it works the way you want...
Code:
[table="width: 500"]
[tr]
	[td]Function Mystr(S As String) As String
  Dim X As Long, Z As Long, Start As Long, Finish As Long, Txt As String, Parts() As String
  Parts = Split(S)
  For X = 0 To UBound(Parts)
    Txt = Left(Parts(X), Len(Parts(X)) / 2 - 3)
    Start = Right(Left(Parts(X), Len(Parts(X)) / 2), 3)
    Finish = Right(Parts(X), 3)
    For Z = Start To Finish
      Mystr = Mystr & " " & Txt & Z & Txt & Z
    Next
  Next
  Mystr = Trim(Mystr)
End Function[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,978
Members
448,934
Latest member
audette89

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