Replace Value with Next if Zero

Caldrumr1234

New Member
Joined
Aug 29, 2019
Messages
1
I have an Excel question that I am afraid is out of my range of expertise.
I have a list of items with inventory values.
What I need is either a formula or script that will say if the value is zero, then grab the value from the next line for the same item number if it is not zero, and continue down until it gets one (and stop).
If lines for that item are zero, it should retain the zero.
Can someone point me in the correct direction?

[TABLE="width: 500"]
<tbody>[TR]
[TD]Item[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]124[/TD]
[TD]2[/TD]
[/TR]
</tbody>[/TABLE]


In the example above, the first and second lines should have the zero replaced with 4.

Thanks in advance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hey,

Try in a new cell:

=INDEX($B$2:$B$5,MATCH(1,($A$2:$A$5=A2)*($B$2:$B$5<>0),0))

Ctrl+Shift+Entered (CSE)

Then drag that formula down.

Assuming $A$2:$A$5 are your item numbers and $B$2:$B$5 are your values - then write this in to cell C2 and drag down to C5.
 
Last edited:
Upvote 0
Maybe


Book1
ABCD
1ItemValue123
212304
31230
41234
51242
Sheet2
Cell Formulas
RangeFormula
D2=INDEX(B2:B5,AGGREGATE(15,6,(ROW(B2:B5)-ROW(B2)+1)/((A2:A5=D1)*(B2:B5>0)),1))
 
Upvote 0
Here's a macro to try:
Code:
Sub a1108429a()
With Range("A2:B" & Cells(Rows.count, "A").End(xlUp).Row)
    va = .Value
    For i = UBound(va, 1) - 1 To 1 Step -1
        If va(i, 1) = va(i + 1, 1) And va(i, 2) = 0 Then va(i, 2) = va(i + 1, 2)
    Next
    .Value = va
End With
 
Upvote 0
Here's a macro to try:
Code:
Sub a1108429a()
With Range("A2:B" & Cells(Rows.count, "A").End(xlUp).Row)
    va = .Value
    For i = UBound(va, 1) - 1 To 1 Step -1
        If va(i, 1) = va(i + 1, 1) And va(i, 2) = 0 Then va(i, 2) = va(i + 1, 2)
    Next
    .Value = va
End With

Thanks, this looks great.
I'm trying to get it to fit my exact data.
Let me show you what the actual form looks like.
I tried changing the range from B to K, but it's not picking it up.
And thank you for all of the answers. You guys are great!

n3QSSMk

2019-08-29-7-42-57.jpg
 
Upvote 0
Let me show you what the actual form looks like.
I tried changing the range from B to K, but it's not picking it up.
Ok, try this one:

Code:
Sub a1108429b()
Dim va, vb
Dim i As Long, n As Long

n = Range("A" & Rows.count).End(xlUp).Row
va = Range("A11:A" & n)
vb = Range("K11:K" & n)

    For i = UBound(va, 1) - 1 To 1 Step -1
        If va(i, 1) = va(i + 1, 1) And vb(i, 1) = 0 Then vb(i, 1) = vb(i + 1, 1)
    Next
Range("A11:A" & n) = va
Range("K11:K" & n) = vb

End Sub
 
Upvote 0
Akuini, that is perfect.
Thank you so much for helping me with something that would have taken me numerous hours, if I had even been able to figure it out myself.
I really, really appreciate it! :D
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
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