Extract predefined numbers of characters, and join them in one cell

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000</SPAN></SPAN>

Hi,</SPAN></SPAN>

1-My data columns C:P in cells</SPAN></SPAN>
2-Predefined numbers columns R:Z</SPAN></SPAN>
3-Extract and join them in columns AB:AJ</SPAN></SPAN>

Extract predefined number of characters from columns C:P (as per specified in columns R:Z) and join them in columns AB:AJ</SPAN></SPAN>

Example for row6 R6:Z6 predefine number is 1, 2, 2, 2, 3, 1, 3, 0 & 0.</SPAN></SPAN>
So far extract 1 character from C6 = "X" join in cell AB6</SPAN></SPAN>
So far extract 2 character from D6, E6 = "12" join in cell AC6</SPAN></SPAN>
So far extract 2 character from F6, G6 = "12" join in cell AD6</SPAN></SPAN>
So far extract 2 characters from H6, I6 = "XX" join in cell AE6</SPAN></SPAN>
So far extract 3 character from J6, K6, L6 = "112" join in cell AF6</SPAN></SPAN>
So far extract 1 character from M6 = "X" join in cell AG6</SPAN></SPAN>
So far extract 3 character from N6, O6, P6 = "112" join in cell AH6</SPAN></SPAN>

And the same method will be followed for rest of the data rows</SPAN></SPAN>

Example data...</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKAL
1
2
3
4
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14B1B2B3B4B5B6B7B8B9B1B2B3B4B5B6B7B8B9
6X1212XX112X112122231300X1212XX112X112
71X2221XX22XX115522000001X2221XX22XX11
81X11XX22XX11XX2624000001X11XX22XX11XX
91X1221X11X12212323310001X1221X11XX1221
10X1X1X11X111XX1122351000X1X1X11X111XX1
11XXX1111X1X212X353210000XXX1111X1X212X
122X1112XXX111211143410002X1112XXX11121
13X1XX1111111X11138200000X1XX1111111X11
141XX12121112X2X3224210001XX12121112X2X
15X2X11X1111X11X213530000X2X11X1111X11X
16111111XXX1112X941000000111111XXX1112X
171212X112X2111X2213240001212X112X2111X
181XX112XX1X11X13322310001XX112XX1X11X1
1911111XX211111286000000011111XX2111112
2011111111112XX1112100000011111111112XX1
21
22
23
24
Sheet8


Thank you in advance</SPAN></SPAN>

Regards,</SPAN>
Kishan</SPAN></SPAN>
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I know you are using Excel 2000 which I do not have, so I cannot test this, but does the following macro work for you...
Code:
[table="width: 500"]
[tr]
	[td]Sub ExtractAndJoin()
  Dim R As Long, C As Long, LastRow As Long
  Dim Chars As String, Pattern As String
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  For R = 6 To LastRow
    Chars = ""
    Pattern = ""
    For C = 3 To 16
      Chars = Chars & Cells(R, C).Value
    Next
    For C = 18 To 26
      Pattern = Pattern & " " & String(Cells(R, C).Value, "@")
    Next
    Cells(R, "AA").Resize(, 9) = Split(Format(Chars, "!" & Pattern))
  Next
End Sub[/td]
[/tr]
[/table]


Just so I know for future questions, if you execute the following line of code in the VB editor's Immediate Window with the data sheet you posted active, what happens...
Code:
[table="width: 500"]
[tr]
	[td]? Join(WorksheetFunction.Index(Range("C6:P6").Value, 1, 0), "")[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
I know you are using Excel 2000 which I do not have, so I cannot test this, but does the following macro work for you...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub ExtractAndJoin()
  Dim R As Long, C As Long, LastRow As Long
  Dim Chars As String, Pattern As String
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  For R = 6 To LastRow
    Chars = ""
    Pattern = ""
    For C = 3 To 16
      Chars = Chars & Cells(R, C).Value
    Next
    For C = 18 To 26
      Pattern = Pattern & " " & String(Cells(R, C).Value, "@")
    Next
    Cells(R, "AA").Resize(, 9) = Split(Format(Chars, "!" & Pattern))
  Next
End Sub[/TD]
[/TR]
</tbody>[/TABLE]



Excellent!! Rick, code is running like magic, tried with my original data 9000+ rows , and take a few second to join result data in columns AB:AK

Thank you very much for giving a quick and spot on solution

I do appreciate your kind help for solving my issue

Have a good weekend

Kind regards,
Kishan
:)

[/CODE]
Just so I know for future questions, if you execute the following line of code in the VB editor's Immediate Window with the data sheet you posted active, what happens...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]? Join(WorksheetFunction.Index(Range("C6:P6").Value, 1, 0), "")[/TD]
[/TR]
</tbody>[/TABLE]
Regarding the line above if I understood correctly, I placed the line activating posted sheet in the VB editor's Immediate Window I see does not happen any action. Did I do it correctly not sure?
 
Last edited:
Upvote 0
Regarding the line above if I understood correctly, I placed the line activating posted sheet in the VB editor's Immediate Window I see does not happen any action. Did I do it correctly not sure?
I am sure you did do it correctly. I did not think it would work for you, but I wanted to be sure. If it had worked, then there would have been a more compact solution available (two loops would have been eliminated). The following won't work with your version of Excel, but it runs fine in my version of Excel (XL2010). I am posting it for anyone reading this thread who has Excel 2003 or later who might want to see the technique I used to avoid those loops...
Code:
[table="width: 500"]
[tr]
	[td]Sub ExtractAndJoin()
  Dim R As Long, C As Long, LastRow As Long
  Dim Chars As String, Pattern As String
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  For R = 6 To LastRow
    Chars = Join(WorksheetFunction.Index(Range("C" & R & ":P" & R).Value, 1, 0), "")
    Pattern = Join(WorksheetFunction.Index(Evaluate(Replace("IF({1},REPT(""@"",R#:Z#)&"" "")", "#", R)), 1, 0), "")
    Cells(R, "AB").Resize(, 9) = Split(Format(Trim(Chars), Pattern))
  Next
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
I am sure you did do it correctly. I did not think it would work for you, but I wanted to be sure. If it had worked, then there would have been a more compact solution available (two loops would have been eliminated). The following won't work with your version of Excel, but it runs fine in my version of Excel (XL2010). I am posting it for anyone reading this thread who has Excel 2003 or later who might want to see the technique I used to avoid those loops...
Hi Rick, just wanted to let you know code you posted in the post#4 worked smooth with my version 2000, no problem at all. So I did adapt this the last one without the loops.

Thank you

Kind regards,
Kishan
:)

 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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