VBA tweak to include a second IF statement

chappy

New Member
Joined
Jul 18, 2006
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hi I would like to tweak some code that works well to add one additional IF statement. The code I am using is below. I would like to build in the following formula:

IF(appliedCommission >= 0.35, appliedCommission, 0.35) + (prEntry * Quantity * transactFee)

Any assistance would be most appreciated!


VBA Code:
'Vlook to calculate the commission payable'
Private Sub calculateCommissionPayable(actvSheet As Worksheet)
Dim rw As Range
Dim col As Range
Dim commPS As Double
Dim transactFee As Double
Dim quantity As Integer 'Current Quantity
Dim prEntry As Integer 'Share price

Set instructSheet = ActiveWorkbook.Sheets("DAS_DATA")
Set instructSheet = ActiveWorkbook.Sheets("INSTRUCTIONS")

commPS = instructSheet.Range("E47")
transactFee = instructSheet.Range("E49")

For Each rw In actvSheet.UsedRange.Rows 'Looping on each row of Sheet1'

If rw.row <> 1 Then 'Skip first header row'

Dim appliedCommission As Double 'Calculated commission'

quantity = convertToInt(Cells(rw.row, 5).Value)

appliedCommission = quantity * commPS 'references the named range "COMM_PER_SHARE" on "INSTRUCTIONS" sheet'

actvSheet.Cells(rw.row, 14).Value = appliedCommission 'applies commission per ticket

End If 'Check if not First Row'

Next rw
Exit Sub
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
@chappy Can you clarify what you are wanting?
Having computed appliedCommission, you want to ensure it is a minimum of 0.35 ???
Then something, ????? = appliedCommission + prEntry * Quantity * transactFee
 
Upvote 0
@chappy Can you clarify what you are wanting?
Having computed appliedCommission, you want to ensure it is a minimum of 0.35 ???
Then something, ????? = appliedCommission + prEntry * Quantity * transactFee


Yes I am very sorry for not being clear. I am trying to ensure that a minimum cost is applied. Currently the code calculates commission payable based on the number of shares traded (quantity) multiplied by the commission per share (CommPS). However, this may calculate a value lower than the real minimum commission which is 35 cents each way, i.e. 70 cents in total.

I would like to ensure that if the result of Quantity multiplied by CommPS is less than 70 cents, then use 70 cents otherwise use quantity multiplied by CommPS.

I will drop the additional transaction fee for the moment that I mentioned initially "+ (prEntry * Quantity * transactFee)" so please ignore than.
 
Upvote 0
Then just the one additional line should do it...

VBA Code:
'Vlook to calculate the commission payable'
Private Sub calculateCommissionPayable(actvSheet As Worksheet)
Dim rw As Range
Dim col As Range
Dim commPS As Double
Dim transactFee As Double
Dim quantity As Integer 'Current Quantity
Dim prEntry As Integer 'Share price

Set instructSheet = ActiveWorkbook.Sheets("DAS_DATA")
Set instructSheet = ActiveWorkbook.Sheets("INSTRUCTIONS")

commPS = instructSheet.Range("E47")
transactFee = instructSheet.Range("E49")

For Each rw In actvSheet.UsedRange.Rows 'Looping on each row of Sheet1'

If rw.Row <> 1 Then 'Skip first header row'

Dim appliedCommission As Double 'Calculated commission'

quantity = convertToInt(Cells(rw.Row, 5).Value)

appliedCommission = quantity * commPS 'references the named range "COMM_PER_SHARE" on "INSTRUCTIONS" sheet'
    
    '********
    If appliedCommission < 0.7 Then appliedCommission = 0.7     '**** ensure appliedCommission  minimum 0.70
    '********
    
actvSheet.Cells(rw.Row, 14).Value = appliedCommission 'applies commission per ticket

End If 'Check if not First Row'

Next rw
Exit Sub
End Sub
 
Upvote 0
Then just the one additional line should do it...

VBA Code:
'Vlook to calculate the commission payable'
Private Sub calculateCommissionPayable(actvSheet As Worksheet)
Dim rw As Range
Dim col As Range
Dim commPS As Double
Dim transactFee As Double
Dim quantity As Integer 'Current Quantity
Dim prEntry As Integer 'Share price

Set instructSheet = ActiveWorkbook.Sheets("DAS_DATA")
Set instructSheet = ActiveWorkbook.Sheets("INSTRUCTIONS")

commPS = instructSheet.Range("E47")
transactFee = instructSheet.Range("E49")

For Each rw In actvSheet.UsedRange.Rows 'Looping on each row of Sheet1'

If rw.Row <> 1 Then 'Skip first header row'

Dim appliedCommission As Double 'Calculated commission'

quantity = convertToInt(Cells(rw.Row, 5).Value)

appliedCommission = quantity * commPS 'references the named range "COMM_PER_SHARE" on "INSTRUCTIONS" sheet'
   
    '********
    If appliedCommission < 0.7 Then appliedCommission = 0.7     '**** ensure appliedCommission  minimum 0.70
    '********
   
actvSheet.Cells(rw.Row, 14).Value = appliedCommission 'applies commission per ticket

End If 'Check if not First Row'

Next rw
Exit Sub
End Sub


Thank you very much Snakehips it works perfectly!
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,319
Members
449,218
Latest member
Excel Master

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