# Help with alignment

#### Lilly44

##### New Member
Hi,
I have cells with multiple entries as a string separated by spaces and I need to align the first character of each entry in one cell to the first character of each corresponding entry on the cell below so that after each space the characters from each of the two cells are in line.

For example
A1 has: 12 14.2 15 8 11
A2 has: 9 19 5 13 15
I need the 1 of the 12 aligned with the 9 the 1 of the 14.2 aligned with the 1 from the 19 the 1 of the 15 aligned with the 5, etc.

Then the next two cells need to be aligned and so forth.
Can this be done easily?
Thanks for your help!!
Lilly44

Last edited:

### Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi Lilly

I don't think it will be that easy the way you want it, but I also do not understand exactly the problem

- are the cells with these values a contiguous range or are there some cells in-between empty?
- are the data always numbers separated by spaces?
- have all the cells the same number of numbers (in your example 5 numbers each)?
- do you need the data in all the cells in the range aligned or just in groups or 2? (from your "Then the next two cells need to be aligned and so forth.")
- if they are numbers do you want them in the same format or keep the original format and just align them?

Remarks:
- just because you add spaces it doesn't follow that the numbers will be displayed aligned using the usual fonts, that are proportional fonts. You'd have to use a monospace font.
- I would use other type of solutions, these are 2 examples:
Solution 1 - split the string using space as separator. Each cell would have just 1 number. You'd just have to right align the cells and adjust the width of the cells (done in seconds)
Solution 2 - use fixed spacing, for ex. each number starts evely 6th character. Seems easier to read and to implement

Example:
Could it be these 6 cells? What would be the expected result?

12 14.2 15 8 11
9 19 5 13 15
1 22222.222 3
1111 2 33333
12 34
56 78 90

Hi PGC01,
Thanks for your prompt response and for trying to help me out.

There is one cell with a string of 23 numbers with spaces between them. Only one cell is involved, there are no multiple cells or empty cells in between.
Under that cell there is another one with 23 different numbers with spaces between them and so on.
I need the 23 numbers in cell A1 aligned to the 23 numbers in cell A2 and then the numbers in A3 aligned with those in A4, etc. so that every two cells are aligned with each other (A1 and A4 do not need to be aligned, just A1 with A2 and then A3 with A4, etc).

-Each entry (number) is separated by a space (12 9 9.3 14, etc) always.
-All of them have the exact number of numbers (23 each)
-Data needs to be aligned in groups of two (A1 with A2, A3 with A4, A5 with A6, etc)
-I'm not sure what you mean when you say keeping them in the same format. I guess it really doesn't matter as long as I can get them aligned.

-I will use whatever font you tell me is appropriate, it doesn't have to be that one.
-I think that I can't use solution one because that's how I have it set right now and the problem is that because some numbers are single digits and their 'partners' in the cell below might not be, it will throw off the alignment. The first number is aligned but as you read through the string they start to get misaligned when they don't have the same number of digits.

-For solution 2, is there a way I can have excel do that automatically?
Thanks again for all your help.

Hi

I'd use a udf, like the one in the example I post

With this data in A2:A7

12 14.2 15 8 11
9 19 5 13 15
1 22222.222 3
1111 2 33333
12 34 2
56 78 90

I used in B2:

=Align2(A2,IF(ISODD(ROWS(\$A\$2:A2)),A3,A1))

and copied down

The result was:

Code:
``````[FONT=courier new]12 14.2 15 8  11
9  19   5  13 15
1    22222.222 3
1111 2         33333
12 34 2
56 78 90
[/FONT]``````

as you see, each group of 2 lines has the numbers aligned

The udf has a third optional parameter that specifies the width of each number.

Used in B2:

=Align2(A2,IF(ISODD(ROWS(\$A\$2:A2)),A3,A1),10)

Copy down

Now each number will start each 10th character

Code:
``````[FONT=courier new]12         14.2       15         8          11
9          19         5          13         15
1          22222.222  3
1111       2          33333
12         34         2
56         78         90
[/FONT]``````

This is the udf I used:

Code:
``````Function Align2(s1 As String, s2 As String, Optional lFixed As Long = 0) As String
Dim v1 As Variant, v2 As Variant
Dim l1 As Variant, l2 As Variant, lMax As Variant
Dim j As Long

v1 = Split(s1)
v2 = Split(s2)

If UBound(v1) <> UBound(v2) Then
Align2 = "Error, strings with different number of elements"
Exit Function
End If

For j = 0 To UBound(v1)
l1 = Len(v1(j))
l2 = Len(v2(j))
lMax = IIf(l1 > l2, l1, l2)
lMax = IIf(lMax > lFixed, lMax, lFixed)
v1(j) = Left(v1(j) & Space(lMax), lMax)
Next j

Align2 = Join(v1)
End Function``````

P. S.

As I said, if you want to see them aligned you have to use a monospace font, for ex. Courier New

-I think that I can't use solution one because that's how I have it set right now and the problem is that because some numbers are single digits and their 'partners' in the cell below might not be, it will throw off the alignment. The first number is aligned but as you read through the string they start to get misaligned when they don't have the same number of digits.

Not true. Since the numbers are separated by a space, you can use Text to Columns and specify Space as delimiter.
This will split the string by the spaces and place each number in a cell.
You just left align the cells and that's it.

You can try this, as I posted you can do it in seconds

Notice that in this case each number is in a different cell and so it does not matter which font you use

For ex.:

Another option !!:-
This code will alter your data !!!
Thanks to PCG for the Font ???
Code:
``````[COLOR=Navy]Sub[/COLOR] MG05Sep02
[COLOR=Navy]Dim[/COLOR] Sp          [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]Dim[/COLOR] Txt         [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
[COLOR=Navy]Dim[/COLOR] n           [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] oMax        [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] Rw          [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] Ac          [COLOR=Navy]As[/COLOR] [COLOR=Navy]Integer[/COLOR]
[COLOR=Navy]Dim[/COLOR] Rng         [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
Rng.Font.Name = "Courier"
[COLOR=Navy]For[/COLOR] Rw = 1 To Rng.Count [COLOR=Navy]Step[/COLOR] 2
[COLOR=Navy]With[/COLOR] Range("A" & Rw)
.Value = Trim(.Value): .Offset(1).Value = Trim(.Offset(1).Value)
Sp = Split(Join(Application.Transpose(.Resize(2)), " "), " ")
[COLOR=Navy]For[/COLOR] n = 0 To UBound(Sp)
oMax = Application.Max(Len(Sp(n)), oMax)
[COLOR=Navy]Next[/COLOR] n

[COLOR=Navy]For[/COLOR] Ac = 0 To 1
Sp = Split(Trim(.Offset(Ac)), " ")
[COLOR=Navy]For[/COLOR] n = 0 To UBound(Sp)
Txt = Txt & Sp(n) & Application.Rept(" ", oMax - Len(Sp(n)) + 1)
[COLOR=Navy]Next[/COLOR] n
.Offset(Ac).Value = Txt: Txt = ""
[COLOR=Navy]Next[/COLOR] Ac
[COLOR=Navy]End[/COLOR] With
[COLOR=Navy]Next[/COLOR] Rw
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]``````
Regards Mick

Replies
1
Views
467
Replies
16
Views
827
Replies
15
Views
615
Replies
4
Views
202
Replies
1
Views
579

1,221,127
Messages
6,158,103
Members
451,464
Latest member
Holden3

### 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?

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