VBA find first non 0 cell per row and update firs cell in row with that value - looping

paulq

New Member
Joined
Mar 24, 2022
Messages
2
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Good day

I have an excel spreadsheet with about 2000 rows and 200 columns, I already have the code right to find 0 values and update with the previous value thats not 0. But i am struggling when the first three values in a row is zero to find the firts none zero value and update the first zero values with these values.

Code that works.
VBA Code:
   Sub colb()
   Dim lRow As Long
Dim lCol As Long
    
    'Find the last non-blank cell in column A(1)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    'Find the last non-blank cell in row 1
    lCol = Cells(6, Columns.Count).End(xlToLeft).Column

    Dim cell As Range
    For Each cell In Range(Cells(4, 4), Cells(lRow, lCol))
    If cell.Value = 0 Then
    cell.Value = cell.Offset(columnOffset:=-1).Value
    End If
   Next
  
End Sub
This gets all the zeros in sheet and changes them.

But now my first 3 cells in the row is 0.

Here is the code

VBA Code:
Dim lRow As Long
Dim lCol As Long
    
    'Find the last non-blank cell in column A(1)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    'Find the last non-blank cell in row 1
    lCol = Cells(6, Columns.Count).End(xlToLeft).Column
    

     ' format cells
    Dim cell As Range
            Range(Cells(4, 4), Cells(lRow, lCol)).Select
    Selection.NumberFormat = "0.0000000000"
    ' end cell change
    
    
    For Each c In Range(Cells(4, 4), Cells(lRow, lCol)).Cells
  If c.Value > 0 Then
    firstValue = c.Value
    firstAddress = c.Address
    Exit For
  End If
Next

MsgBox "The first value greater than zero is in cell " & firstAddress & _
       "; - it has value " & firstValue
       
       
     For Each cell In Range(Cells(3, 2), Cells(lRow, 5))
   


       If cell.Value = 0 Then
      cell.Value = firstValue

      End If

    Next
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
something like this, i didn't test it ...
VBA Code:
Sub colb()
     Dim lRow  As Long
     Dim lCol  As Long
     Dim i As Integer, b As Boolean
     'Find the last non-blank cell in column A(1)
     lRow = Cells(Rows.Count, 1).End(xlUp).Row

     'Find the last non-blank cell in row 1
     lCol = Cells(6, Columns.Count).End(xlToLeft).Column

     Dim cell  As Range
     For Each cell In Range(Cells(4, 4), Cells(lRow, lCol))
          If cell.Value = 0 Then
               b = False                                        'flag down
               For i = 1 To cell.Column - 1                     'loop from previous cell to 1st column
                    If cell.Offset(, -i).Value > 0 Then         '
                         cell.Value = cell.Offset(, -i).Value   'take that value
                         b = True                               'flag up
                         Exit For                               'stop loop
                    End If
               Next
               If Not b Then MsgBox "no value found for " & cell.Address, vbInformation     'flag down = msgbox
          End If
     Next

End Sub
 
Upvote 0
Hi and welcome to MrExcel

when the first three values in a row is zero to find the firts none zero value
If there are no values other than zero, then what should you do, leave them at that.

If you have this data, what result would you need.
varios 24mar2022.xlsm
ABCDEF
6010030
7100050
8000020
sheet12
 
Upvote 0
I'm confused, where your data really is. I checked your macros and you have different start cells:

lRow = Cells(Rows.Count, 1).End(xlUp).Row 'column A
lCol = Cells(6, Columns.Count).End(xlToLeft).Column 'row 6, I guess A6 cell

but...
For Each c In Range(Cells(4, 4), Cells(lRow, lCol)).Cells 'D4 cell

For Each cell In Range(Cells(3, 2), Cells(lRow, 5)) 'B3 cell

Fit the start cell on this line of the macro:
Set ini = Range("A6") 'Start cell


VBA Code:
Sub replace_Zero()
  Dim i As Long, j As Long, lc As Long
  Dim ini  As Range
  Set ini = Range("A6")   'Start cell
 
  lc = Cells(ini.Row, Columns.Count).End(1).Column
  For i = ini.Row To Cells(Rows.Count, ini.Column).End(3).Row
    For j = ini.Column To lc
      If Cells(i, j).Value = 0 Then
        Cells(i, j).Value = findvalue(i, j, lc)
      Else
        Exit For
      End If
    Next
  Next
End Sub

Function findvalue(i As Long, j As Long, lc As Long)
  Dim k As Long
  For k = j + 1 To lc
    If Cells(i, k) > 0 Then
      findvalue = Cells(i, k)
      Exit Function
    End If
  Next
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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