BalloutMoe
Board Regular
- Joined
- Jun 4, 2021
- Messages
- 137
- Office Version
- 365
- Platform
- Windows
Hello All,
I need some help with my code if possible or making it a function but I am having issues. I have Cell A3 with a certain Value say 600, I would like to loop through all the cells in Column D to Last Row and subtract from each cell but do not exceed cell value to be 7.
So InitialSubtracted = 600 and loop until it reaches total subtracted to be 600. With the below code however sometimes its going above 600 and its entering an endless loop. Any help would be appreciated for a way around this as I am stuck. The initial subtracted varies but its always 100 increments. So 300,400, 500,600 and so on. Below is the code I have.
For example this is a total of 836.28 and I need the new total to be 236.28
I need some help with my code if possible or making it a function but I am having issues. I have Cell A3 with a certain Value say 600, I would like to loop through all the cells in Column D to Last Row and subtract from each cell but do not exceed cell value to be 7.
So InitialSubtracted = 600 and loop until it reaches total subtracted to be 600. With the below code however sometimes its going above 600 and its entering an endless loop. Any help would be appreciated for a way around this as I am stuck. The initial subtracted varies but its always 100 increments. So 300,400, 500,600 and so on. Below is the code I have.
$141.30 |
$89.79 |
$81.66 |
77.33 |
$77.33 |
$73.00 |
$70.32 |
$69.21 |
50.84 |
$40.00 |
$40.00 |
$25.50 |
For example this is a total of 836.28 and I need the new total to be 236.28
VBA Code:
Sub SubtractFromRange()
lastR = ThisWorkbook.Worksheets("Sheet2").Range("B" & ThisWorkbook.Worksheets("Sheet2").Rows.Count).End(xlUp).Row
InitialSubtracted = 0
Do
For i = 2 To lastR
If ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value >= 107 And ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value <= 2000 Then
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 100
ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value = ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 100
InitialSubtracted = InitialSubtracted + 100
Debug.Print InitialSubtracted
End If
If ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value >= 57 And ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value <= 106.99 Then
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 50
ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value = ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 50
InitialSubtracted = InitialSubtracted + 50
Debug.Print InitialSubtracted
End If
If ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value >= 32 And ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value <= 56.99 Then
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 25
ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value = ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 25
InitialSubtracted = InitialSubtracted + 25
Debug.Print InitialSubtracted
End If
If ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value >= 17 And ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value <= 31.99 Then
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 10
ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value = ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 10
InitialSubtracted = InitialSubtracted + 10
Debug.Print InitialSubtracted
End If
If ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value >= 12 And ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value <= 16.99 Then
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value
Debug.Print ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 5
ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value = ThisWorkbook.Worksheets("Sheet2").Range("D" & i).Value - 5
InitialSubtracted = InitialSubtracted + 5
Debug.Print InitialSubtracted
End If
If InitialSubtracted = ThisWorkbook.Worksheets("Sheet2").Range("A3").Value Then
MsgBox ("We took out " & ThisWorkbook.Worksheets("Sheet2").Range("A3").Value)
Exit Sub
End If
Next
Loop Until InitialSubtracted = ThisWorkbook.Worksheets("Sheet2").Range("A3").Value
End Sub
Last edited: