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.
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
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
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