VBA for mulitple if statements

Godders199

Active Member
Joined
Mar 2, 2017
Messages
313
Office Version
  1. 2013
Good morning, i am trying to get the following code to work, have tried various combinations of if ,or, else, elseif but to no success.

I have not copied all the statements , but effectively what i am tying to do is. i column 6 (f)is between 1 an 8 it completes the relevant sum. If greater than 8 sum slighty different and if column 6 is zero it just returns zero. currently only the final line works. I cannot get if 1,2,3,4,5,6,7,8 to work.

If Cells(i, 6) = 1 Then Range("ab" & i).Value = Range("p" & i).Value + Range("s" & i).Value + Range("v" & i).Value + Range("y" & i).Value + Range("f" & i).Value
If Cells(i, 6) = 2 Then Range("ab" & i).Value = Range("p" & i).Value + Range("s" & i).Value + Range("v" & i).Value + Range("y" & i).Value + Range("f" & i).Value
If Cells(i, 6) > 8 Then Range("ab" & i).Value = Range("p" & i).Value + Range("s" & i).Value + Range("v" & i).Value + Range("y" & i).Value + Sheets("Homepage").Range("o4").Value Else Cells(i, 28).Value = 0


Can someone say what i am doing wrong.

thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Something like this?

If (Cells(i, 6) >= 1 And Cells(i, 6) <= 8) Then Range("ab" & i).Value = _
Range("p" & i).Value + Range("s" & i).Value + Range("v" & i).Value + Range("y" & i).Value + Range("f" & i).Value
Else
If Cells(i, 6) > 8 Then Range("ab" & i).Value = _
Range("p" & i).Value + Range("s" & i).Value + Range("v" & i).Value + Range("y" & i).Value + Sheets("Homepage").Range("o4").Value
Else: Cells(i, 28).Value = 0
End If
End If
 
Upvote 0
Hi,
untested but see if this does what you want

Code:
Dim AddValues
If Cells(i, 6).Value >= 1 Then
AddValues = Range("p" & i).Value + Range("s" & i).Value + Range("v" & i).Value + Range("y" & i).Value
Range("ab" & i).Value = AddValues + IIf(Cells(i, 6) <= 8, Range("f" & i).Value, Sheets("Homepage").Range("o4").Value)
Else
Cells(i, 28).Value = 0
End If

I have declared AddValues as Variant but update to the appropriate data type (Double?) as required

Dave
 
Upvote 0

Forum statistics

Threads
1,216,067
Messages
6,128,590
Members
449,460
Latest member
jgharbawi

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