VBA add up code

Strooprover

New Member
Joined
Jul 21, 2017
Messages
25
Hi,

I want to look up a couple of values based on a If then code, for instance:

If Cells(x, 1) = Cells(1,4).Value Then
mycount= mycount + Cells(x,2).Value
End if
Cells(1,5).Value = mycount

Then I want to see if there's a difference between the Values in E1 and F1 (Value of F1 is given), so
Cells(1,6).Value = Cells(1,5).Value - Cells(1,6).Value

But now comes my problem:

I want to adjust the values in Column 2 that came up out my If formula depending on the value in Cells(1,6).

So If the if function resulted in 5 hits I want the difference that showed up in Cells(1,6) divided by 5 AND it has to rounded off to a multiple of 5. When Cells(1,6) is negative it has to make the values lower, with mutiples of 5 and a total that equal to F1.

Can someone help me with this?

Thanks in advance!
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
By now, I came up with this, but the Loop and if statements dont work together. Can someone take a quick look and see how I can adjust it?

Code:
Sub adding()


Dim ws1 As Worksheet
Dim ws2 As Worksheet


Set ws1 = Sheets("Sheet1")
Set ws = Sheets("Sheet2")


counter = 0
i = 1
For x = 1 To 5
If ws1.Cells(x, 1) = ws1.Cells(1, 5) Then
ws1.Cells(x, 1).Copy Destination:=ws.Range("A" & i)
ws1.Cells(x, 2).Copy Destination:=ws.Range("B" & i)


  counter = counter + ws.Range("B" & i).Value
   i = i + 1
  
End If
Next x


If counter < ws1.Range("F1") Then
Do Until counter > ws1.Cells("F1").Value
For i = 1 To ws2.Range("B" & Rows.Count).End(xlUp).Row
ws2.Cells(i, 2).Value = ws2.Cells(i, 2).Value + 5
Next i
Loop
End If






End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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