SOLVED

Excel formula help - populating cells between two cells that contain '1' by row

%3CLINGO-SUB%20id%3D%22lingo-sub-3325793%22%20slang%3D%22en-US%22%3EExcel%20formula%20help%20-%20populating%20cells%20between%20two%20cells%20that%20contain%20'1'%20by%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3325793%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22%22%3E%3CSPAN%3EHi%20all%2C%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22%22%3E%3CSPAN%3ECurrently%20I%20am%20working%20with%20a%20very%20large%20dataset%20which%20looks%20like%20this%20(see%20photo%20attached).%20I%20am%20trying%20to%20find%20a%20way%20to%20automatically%20fill%20cells%20with%20a%20'1'%20in%20rows%20after%20the%20first%20'1'%20and%20before%20the%20last%20'1'.%20For%20example%2C%20the%20first%20row%20would%20have%20Tue%20and%20Wed%20populated%20with%20'1'%20because%20Mon%20and%20Thu%20have%20'1'%20filled.%20Each%20row%20has%202%20'1'%20values.%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22%22%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%3E%3CDIV%20class%3D%22%22%3E%3CSPAN%3EDoes%20anyone%20have%20a%20solution%20for%20this%3F%20This%20is%20just%20a%20small%20snippet%20of%20my%20data%2C%20and%20it%20is%20a%20big%20dataset%20to%20I%20would%20like%20to%20automate%20it%20rather%20than%20doing%20it%20manually.%20Thanks%20in%20advance.%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%20class%3D%22%22%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%26nbsp%3B%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3CTD%3E1%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3CTD%3E1%3C%2FTD%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3325793%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3326673%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formula%20help%20-%20populating%20cells%20between%20two%20cells%20that%20contain%20'1'%20by%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3326673%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1382134%22%20target%3D%22_blank%22%3E%40dweatherley99%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20one()%0A%0ADim%20j%20As%20Integer%0ADim%20i%20As%20Integer%0ADim%20z%20As%20Integer%0ADim%20k%20As%20Integer%0A%0AFor%20i%20%3D%202%20To%2011%0A%0AFor%20j%20%3D%207%20To%201%20Step%20-1%0A%0AIf%20Cells(i%2C%20j).Value%20%3D%201%20Then%0Az%20%3D%20j%0A%0AFor%20k%20%3D%201%20To%207%0A%0AIf%20Cells(i%2C%20k).Value%20%3D%201%20And%20k%20%26lt%3B%20z%20Then%0ACells(i%2C%20k%20%2B%201).Value%20%3D%201%0A%0AIf%20k%20%3D%20z%20Then%0AExit%20For%0A%0AElse%0AEnd%20If%0A%0AElse%0AEnd%20If%0A%0ANext%20k%0A%0AElse%0AEnd%20If%0A%0ANext%20j%0ANext%20i%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EMaybe%20with%20this%20code.%20You%20can%20click%20the%20button%20in%20cell%20I3%20in%20the%20attached%20file%20in%20sheet%20%22Tabelle1%22%20to%20start%20the%20macro.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3328637%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formula%20help%20-%20populating%20cells%20between%20two%20cells%20that%20contain%20'1'%20by%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3328637%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1382134%22%20target%3D%22_blank%22%3E%40dweatherley99%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAttached%20is%20a%20formula%20solution.%26nbsp%3B%20Must%20have%20access%20to%20LAMBDA.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20curious%20what%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%20might%20come%20up%20with%20in%20this%20situation.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3331968%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formula%20help%20-%20populating%20cells%20between%20two%20cells%20that%20contain%20'1'%20by%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3331968%22%20slang%3D%22en-US%22%3EHi%2C%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%20ever%20so%20much%20for%20this%2C%20this%20worked%20perfectly%20for%20what%20I%20wanted%2C%20appreciate%20it!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3331973%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formula%20help%20-%20populating%20cells%20between%20two%20cells%20that%20contain%20'1'%20by%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3331973%22%20slang%3D%22en-US%22%3EThanks%20for%20the%20formula%2C%20Patrick.%20I%20did%20manage%20to%20achieve%20my%20goal%20by%20using%20VBA%20which%20the%20above%20commenter%20assisted%20me%20with%2C%20but%20I%20have%20taken%20note%20of%20your%20formula%20for%20future%20reference.%20Much%20appreciated!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3336115%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formula%20help%20-%20populating%20cells%20between%20two%20cells%20that%20contain%20'1'%20by%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3336115%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F428790%22%20target%3D%22_blank%22%3E%40Patrick2788%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20is%20much%20of%20your%20solution%20in%20this%20but%20the%20detail%20is%20different.%26nbsp%3B%20The%20solution%20clearly%20%3CU%3E%3CSTRONG%3Eis%3C%2FSTRONG%3E%3C%2FU%3E%20a%20nested%20array%20so%20the%20problem%20is%20working%20around%20that.%26nbsp%3B%20I%20adopted%20your%20solution%20of%20calculating%20start%20and%20end%20columns%20as%20distinct%201D%20arrays%20but%20using%20XMATCH%20to%20locate%20the%20first%20and%20last%20instances%20of%20'1'.%26nbsp%3B%20Again%2C%20I%20adopted%20your%20idea%20of%20treating%20the%20next%20stage%20of%20the%20problem%20as%20a%202D%20array%20problem%2C%20but%20was%20able%20to%20dispense%20with%20the%20MAP%20and%20use%20a%20simple%20array%20formula.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20LET(%0A%20%20%20%20k%2C%20%20%20%20%20SEQUENCE(1%2C7)%2C%0A%20%20%20%20start%2C%20BYROW(rng%2C%20Match%CE%BB(1))%2C%0A%20%20%20%20end%2C%20%20%20BYROW(rng%2C%20Match%CE%BB(-1))%2C%0A%20%20%20%20(k%26gt%3B%3Dstart)*(k%26lt%3B%3Dend)%0A%20%20)%0A%0A%2F%2Fwhere%0A%0AMatch%CE%BB%20%3D%20LAMBDA(mode%2C%20LAMBDA(row%2C%20XMATCH(1%2Crow%2C%2Cmode)))%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3336954%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formula%20help%20-%20populating%20cells%20between%20two%20cells%20that%20contain%20'1'%20by%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3336954%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1382134%22%20target%3D%22_blank%22%3E%40dweatherley99%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20accept%20my%20apologies%20for%20treating%20this%20as%20a%20formula%20challenge%20rather%20that%20a%20request%20for%20help!%26nbsp%3B%20Following%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F428790%22%20target%3D%22_blank%22%3E%40Patrick2788%3C%2FA%3E's%20use%20of%20MAP%20over%20a%202D%20array%2C%20I%20decided%20to%20try%20SCAN%2C%20allowing%20it%20to%20run%20through%20the%20entire%20array%20row%20by%20row%20switching%201%20and%200%20every%20time%20it%20came%20to%20a%20cell%20with%20a%201.%26nbsp%3B%20That%20almost%20solved%20the%20problem%20except%20for%20the%20final%20value%20of%20each%20series%20which%20showed%20as%200.%26nbsp%3B%20An%20OR%20('%2B')%20operation%20with%20the%20initial%20range%20sorted%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%2F%2FWorksheet%20formula%0A%3D%20SIGN(rng%20%2B%20SCAN(0%2Crng%2C%20XOR%CE%BB))%3B%0A%2F%2Fwhere%0AXOR%CE%BB%20%3D%20LAMBDA(x%2Cy%2CXOR(x%2Cy))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPerhaps%20reintroducing%20MAP%20might%20offer%20a%20more%20consistent%20programming%20approach%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20--MAP(rng%2C%20SCAN(0%2Crng%2C%20XOR%CE%BB)%2C%20OR%CE%BB)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3337210%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formula%20help%20-%20populating%20cells%20between%20two%20cells%20that%20contain%20'1'%20by%20row%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3337210%22%20slang%3D%22en-US%22%3EI%20like%20it.%20It's%20a%20clever%20workaround%20using%20SEQUENCE%20to%20create%20the%20array.%20I%20was%20trying%20to%20avoid%20using%20INDEX%20but%20used%20it%20because%20I%20couldn't%20line%20up%20(or%20maybe%20nest%20is%20a%20better%20word)%20'start'%20and%20'end'%20with%20the%20COLUMN%20result%20of%20each%20cell%20as%20MAP%20worked%20its%20way%20through%20the%20array.%3C%2FLINGO-BODY%3E
New Contributor
Hi all,
 
Currently I am working with a very large dataset which looks like this (see photo attached). I am trying to find a way to automatically fill cells with a '1' in rows after the first '1' and before the last '1'. For example, the first row would have Tue and Wed populated with '1' because Mon and Thu have '1' filled. Each row has 2 '1' values.
 
Does anyone have a solution for this? This is just a small snippet of my data, and it is a big dataset to I would like to automate it rather than doing it manually. Thanks in advance.
 
MonTueWedThuFriSatSun
1  1   
 11    
 1   1 
1 1    
1   1  
   1 1 
    1 1
1 1    
 1 1   
 1   1 
7 Replies
best response confirmed by dweatherley99 (New Contributor)
Solution

@dweatherley99 

Sub one()

Dim j As Integer
Dim i As Integer
Dim z As Integer
Dim k As Integer

For i = 2 To 11

For j = 7 To 1 Step -1

If Cells(i, j).Value = 1 Then
z = j

For k = 1 To 7

If Cells(i, k).Value = 1 And k < z Then
Cells(i, k + 1).Value = 1

If k = z Then
Exit For

Else
End If

Else
End If

Next k

Else
End If

Next j
Next i

End Sub

Maybe with this code. You can click the button in cell I3 in the attached file in sheet "Tabelle1" to start the macro.

@dweatherley99 

Attached is a formula solution.  Must have access to LAMBDA.

 

I'm curious what @Peter Bartholomew  might come up with in this situation.

 

 

Hi,

Thanks ever so much for this, this worked perfectly for what I wanted, appreciate it!
Thanks for the formula, Patrick. I did manage to achieve my goal by using VBA which the above commenter assisted me with, but I have taken note of your formula for future reference. Much appreciated!

@Patrick2788 

There is much of your solution in this but the detail is different.  The solution clearly is a nested array so the problem is working around that.  I adopted your solution of calculating start and end columns as distinct 1D arrays but using XMATCH to locate the first and last instances of '1'.  Again, I adopted your idea of treating the next stage of the problem as a 2D array problem, but was able to dispense with the MAP and use a simple array formula.

= LET(
    k,     SEQUENCE(1,7),
    start, BYROW(rng, Matchλ(1)),
    end,   BYROW(rng, Matchλ(-1)),
    (k>=start)*(k<=end)
  )

//where

Matchλ = LAMBDA(mode, LAMBDA(row, XMATCH(1,row,,mode)));

@dweatherley99 

Please accept my apologies for treating this as a formula challenge rather that a request for help!  Following @Patrick2788's use of MAP over a 2D array, I decided to try SCAN, allowing it to run through the entire array row by row switching 1 and 0 every time it came to a cell with a 1.  That almost solved the problem except for the final value of each series which showed as 0.  An OR ('+') operation with the initial range sorted it.

 

//Worksheet formula
= SIGN(rng + SCAN(0,rng, XORλ));
//where
XORλ = LAMBDA(x,y,XOR(x,y))

 

Perhaps reintroducing MAP might offer a more consistent programming approach

 

= --MAP(rng, SCAN(0,rng, XORλ), ORλ)

 

 

I like it. It's a clever workaround using SEQUENCE to create the array. I was trying to avoid using INDEX but used it because I couldn't line up (or maybe nest is a better word) 'start' and 'end' with the COLUMN result of each cell as MAP worked its way through the array.