How to remove blank cells in each row and align the cells left or right?

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hello,

In the attached XL2BB, I have a list of items, and I was wondering if I could write a function and get rid of the blanks in each row and align the cells left or right (as a spilled output), as indicated in the example outputs?

Blank power workbook1
ABCDEFGHIJKLMNOPQ
1
2InputDesired output 1Desired output 2
3SucroseGlycineDextranSucroseGlycineDextranSucroseGlycineDextran
4SucroseAlanineAlbuminSucroseAlanineAlbuminSucroseAlanineAlbumin
5SucroseProlineSucroseProlineSucroseProline
6SucroseAlbuminSucroseAlbuminSucroseAlbumin
7SucroseInsulinSucroseInsulinSucroseInsulin
8SucroseTweenSucroseTweenSucroseTween
9GlucoseGlycineGlucoseGlycineGlucoseGlycine
10GlucoseAlanineGlucoseAlanineGlucoseAlanine
11GlucoseProlineTweenDextranGlucoseProlineTweenDextranGlucoseProlineTweenDextran
12GlucoseAlbuminGlucoseAlbuminGlucoseAlbumin
13GlucoseGlucoseGlucose
14GlucoseTweenGlucoseTweenGlucoseTween
15GalactoseGlycineGalactoseGlycineGalactoseGlycine
16GalactoseAlanineGalactoseAlanineGalactoseAlanine
17GalactoseProlineGalactoseProlineGalactoseProline
18GalactoseAlbuminGalactoseAlbuminGalactoseAlbumin
19GalactoseInsulinGalactoseInsulinGalactoseInsulin
20GalactoseTweenDextranGalactoseTweenDextranGalactoseTweenDextran
21
Sheet3
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I know you said function (which would be pretty straightforward) but I'm sure you'd prefer to make a LAMBDA ;)

I might be overcooking the formula approach, but try:

=LET(Inp,B3:F20,LtoR,1,c,COLUMNS(Inp),t,TOCOL(IF(Inp="","",Inp)),WRAPROWS(SORTBY(t,INT(SEQUENCE(COUNTA(t),,0)/c)+LtoR*(LEN(t)=0)/2),c))

Set LtoR = -1 for the reverse sort.
 
Upvote 0
Like Stephen's, this may not be exactly as your desired outputs as our solutions both contain the same number of columns as the original data (indicated by border below) whereas the borders on your outputs indicate you may only want the minimum columns possible.

22 11 23.xlsm
ABCDEFGHIJKLMNOPQRS
1
2InputDesired output 1Desired output 2
3SucroseGlycineDextranSucroseGlycineDextran SucroseGlycineDextran
4SucroseAlanineAlbuminSucroseAlanineAlbuminSucroseAlanineAlbumin
5SucroseProlineSucroseProlineSucroseProline
6SucroseAlbuminSucroseAlbuminSucroseAlbumin
7SucroseInsulinSucroseInsulinSucroseInsulin
8SucroseTweenSucroseTweenSucroseTween
9GlucoseGlycineGlucoseGlycineGlucoseGlycine
10GlucoseAlanineGlucoseAlanineGlucoseAlanine
11GlucoseProlineTweenDextranGlucoseProlineTweenDextranGlucoseProlineTweenDextran
12GlucoseAlbuminGlucoseAlbuminGlucoseAlbumin
13GlucoseGlucoseGlucose
14GlucoseTweenGlucoseTweenGlucoseTween
15GalactoseGlycineGalactoseGlycineGalactoseGlycine
16GalactoseAlanineGalactoseAlanineGalactoseAlanine
17GalactoseProlineGalactoseProlineGalactoseProline
18GalactoseAlbuminGalactoseAlbuminGalactoseAlbumin
19GalactoseInsulinGalactoseInsulinGalactoseInsulin
20GalactoseTweenDextranGalactoseTweenDextranGalactoseTweenDextran
21
Remove Blanks
Cell Formulas
RangeFormula
H3:L20H3=TEXTSPLIT(TEXTJOIN(";",,BYROW(B3:F20,LAMBDA(rw,TEXTJOIN(",",1,rw)&REPT(",",COUNTBLANK(rw))))),",",";")
N3:R20N3=TEXTSPLIT(TEXTJOIN(";",,BYROW(B3:F20,LAMBDA(rw,REPT(",",COUNTBLANK(rw))&TEXTJOIN(",",1,rw)))),",",";")
Dynamic array formulas.


If you did want the reduced columns ..

22 11 23.xlsm
ABCDEFGHIJKLMNOPQR
1
2InputDesired output 1Desired output 2
3SucroseGlycineDextranSucroseGlycineDextran SucroseGlycineDextran
4SucroseAlanineAlbuminSucroseAlanineAlbuminSucroseAlanineAlbumin
5SucroseProlineSucroseProlineSucroseProline
6SucroseAlbuminSucroseAlbuminSucroseAlbumin
7SucroseInsulinSucroseInsulinSucroseInsulin
8SucroseTweenSucroseTweenSucroseTween
9GlucoseGlycineGlucoseGlycineGlucoseGlycine
10GlucoseAlanineGlucoseAlanineGlucoseAlanine
11GlucoseProlineTweenDextranGlucoseProlineTweenDextranGlucoseProlineTweenDextran
12GlucoseAlbuminGlucoseAlbuminGlucoseAlbumin
13GlucoseGlucoseGlucose
14GlucoseTweenGlucoseTweenGlucoseTween
15GalactoseGlycineGalactoseGlycineGalactoseGlycine
16GalactoseAlanineGalactoseAlanineGalactoseAlanine
17GalactoseProlineGalactoseProlineGalactoseProline
18GalactoseAlbuminGalactoseAlbuminGalactoseAlbumin
19GalactoseInsulinGalactoseInsulinGalactoseInsulin
20GalactoseTweenDextranGalactoseTweenDextranGalactoseTweenDextran
21
Remove Blanks (2)
Cell Formulas
RangeFormula
H3:K20H3=DROP(TEXTSPLIT(TEXTJOIN(";",,BYROW(B3:F20,LAMBDA(rw,TEXTJOIN(",",1,rw)&REPT(",",COUNTBLANK(rw))))),",",";"),,-MIN(BYROW(B3:F20,LAMBDA(rw,COUNTBLANK(rw)))))
N3:Q20N3=DROP(TEXTSPLIT(TEXTJOIN(";",,BYROW(B3:F20,LAMBDA(rw,REPT(",",COUNTBLANK(rw))&TEXTJOIN(",",1,rw)))),",",";"),,MIN(BYROW(B3:F20,LAMBDA(rw,COUNTBLANK(rw)))))
Dynamic array formulas.
 
Upvote 0
Solution
Ha, thanks Peter!

I started with the same approach, but got stuck, not having noticed that TEXTSPLIT allows two delimiters (row and column).

The new function is even more useful than I thought.
 
Upvote 0
Wow 😮, unbelievably fabulous formulas! You guys are both absolute geniuses 🧠 🧠

And yes, I was thinking of making LAMBDAs :) (I have a few being cooked and I'll also work on this one too 😅 will post them when ready)

And yes, I did want the reduced columns, but having both options is great for when needed 🍻
 
Last edited:
Upvote 0
I just realized that for extra learning, I'm gonna see if I can figure out aligning at top and bottom as well 😅 😅 Then the final LAMBDA can have all four orientation options.
 
Upvote 0

Forum statistics

Threads
1,215,593
Messages
6,125,715
Members
449,254
Latest member
Eva146

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