Basic Exercise
In this exercise, you have to acquire the total value of the addition of the column "A" numbers, whose absolute value is greater than 50.
MsgBox Abs(10) 'Will result in 10
MsgBox Abs(-10) 'Will result in 10
Remember that in most cases there is more than one way to solve the exercise.
Hint: The sum should have been positive.
Declare the variables and find the last data entry in the worksheet
Dim LastRow As Long
Dim CellVal As Long
Dim Result As Long
Dim i As Integer
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Make a loop For Next adding each constrained cell to a conditional (being greater than 50 in absolute terms) and associating the result with the variable Result.
Result = 0
For i = 3 To LastRow
CellVal = Cells(i, 1).Value
If Abs(CellVal) > 50 Then
Result = Result + CellVal
End If
Next i
Sub Solution()
Dim LastRow As Long
Dim CellVal As Long
Dim Result As Long
Dim i As Integer
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Result = 0
For i = 3 To LastRow
CellVal = Cells(i, 1).Value
If Abs(CellVal) > 50 Then
Result = Result + CellVal
End If
Next i
Cells(3, 5).Value = Result
End Sub
SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.
Excel ® is a registered trademark of the Microsoft Corporation.
© 2019 SuperExcelVBA | ABOUT
Thank you for contributing. A message was sent reporting your comment.