Hello everyone
I am new to excel vba and I have a problem with the code and it gives me error run-time 91
In the code, I want the program to check the cells (A1:A5), if their value is zero or empty, give me this message (no reading) in cell (c1), and if there is a value in the cells, to start checking the cells from bottom to top so that it starts In cell (A5), and if there is a value in it, it subtracts it from the value in cell (b1) and puts the value of the answer in cell (c1), and stops checking the rest of the cells. If there is no value in cell (a5), it moves to cell (a4). And it does the same steps, and so on, until he reaches cell (A1).
Sub TEST()
With Worksheets(2).Range("A1:A5")
Dim x As Integer
Dim sheet(2) As Object
For Each cell In Worksheets(2).Range("A1:A5")
If cell.Value = "0" Or IsEmpty(cell.Value) = True Then
Range("C1").Value = " no reading "
Else
GoTo step2
End If
Next cell
step2:
x = 5
if sheet(2).Cells(x, 1).Value > 0
Range("C1").Value = Range("b1").Value - c
Else
x = x - 1
GoTo step2
End If
End With
End Sub
I am new to excel vba and I have a problem with the code and it gives me error run-time 91
In the code, I want the program to check the cells (A1:A5), if their value is zero or empty, give me this message (no reading) in cell (c1), and if there is a value in the cells, to start checking the cells from bottom to top so that it starts In cell (A5), and if there is a value in it, it subtracts it from the value in cell (b1) and puts the value of the answer in cell (c1), and stops checking the rest of the cells. If there is no value in cell (a5), it moves to cell (a4). And it does the same steps, and so on, until he reaches cell (A1).
Sub TEST()
With Worksheets(2).Range("A1:A5")
Dim x As Integer
Dim sheet(2) As Object
For Each cell In Worksheets(2).Range("A1:A5")
If cell.Value = "0" Or IsEmpty(cell.Value) = True Then
Range("C1").Value = " no reading "
Else
GoTo step2
End If
Next cell
step2:
x = 5
if sheet(2).Cells(x, 1).Value > 0
Range("C1").Value = Range("b1").Value - c
Else
x = x - 1
GoTo step2
End If
End With
End Sub