VBA If statement w/multiple conditions

bobkap

Active Member
Joined
Nov 22, 2009
Messages
323
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I'm trying to perfect a simple macro that calculates profit margin based on 3 different conditions: 1) I have cost and desired margin but missing price; 2) I have price and desired margin but missing cost; 3) I have cost and price but missing margin calculation.

I've listed my code below but if my condition is met at either the first of second scenario I'm not sure how to end the routine there. Any help would be most appreciated.

VBA Code:
Sub GMBK()
'
' GMBK Macro
'

cost = Range("b4").Value
Price = Range("c4").Value
margin = Range("d4").Value


If IsEmpty(Cells(4, 4)) Then
netp = Price - cost
margin = (netp / Price)
Cells(4, 4).NumberFormat = "0.0%"
Cells(4, 4) = margin

Else
If IsEmpty(Cells(4, 2)) Then
cost = Price / (1 - margin)


Else
If Price = cost / (1 - margin) Then
Cells(4, 3) = Price
End If
End If
End If
'
'
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I've listed my code below but if my condition is met at either the first of second scenario I'm not sure how to end the routine there. Any help would be most appreciated.
You should not have to change anything. Since you have each succeeding IF condition buried in the ELSE of the previous IF, it should exit once the first one is met.
So your code should be fine as it is.

If you try stepping through your code, you should be able to see this.
 
Upvote 0
Thanks very much. I actually did step through it before I did that post and every time it bombed out with this message: Run-time error '13': Type Mismatch. And when I clicked on debug it highlighted this line of code towards in the last if statement area: If Price = cost / (1 - margin) Then.
 
Upvote 0
What are your values fro Price, Cost, and Margin?
 
Upvote 0
Thanks but I think I figured it out. I did not have an "isempty" test for my middle scenario. I corrected that and it works fine now.
 
Upvote 0
Thanks but I think I figured it out. I did not have an "isempty" test for my middle scenario. I corrected that and it works fine now.
If that is the case, "Debug" should have highlighted that line, and not the one you mentioned in your previous post.

Note that if you want to check for an empty cell, you can just use:
VBA Code:
If Cells(4, 4) = "" Then
 
Upvote 0
Nice! I did not know that. THANKS again!
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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