Code

Small projects in VBA for Excel and other bits of programming.

VBA Examples

Reverse Margin Formula

Quick VBA module that writes you an Excel formula to calculate a margin based on your cost and sell price. It's almost like I get asked "how do I figure out a margin again?" all the time and just seeing if I could program it just popped into my head.

Public Sub reverse_margin_formula()
 
Dim Cost As String
Dim Margin As Integer
Dim FormulaOut As String
 
Cost = Application.InputBox("Select the cell with the cost price in.", "Select cost price cell", "A1", , , , , 2)
  If Cost = vbNullString Then
    MsgBox ("User canceled!")
    Exit Sub
  End If
 
Margin = Application.InputBox("Enter the desired margin. e.g. 20", "Enter desired margin as a number", "0", , , , , 2)
  If Margin = vbNullInterger Then
    MsgBox ("User canceled!")
    Exit Sub
  End If
 
FormulaOut = "=" & Cost & "/(1-" & (Margin / 100) & ")"
 
ActiveCell.Value = FormulaOut
 
End Sub

C++ Examples

Reverse Margin Formula

Quick C++ program that writes you an Excel formula to calculate a margin based on your cost and sell price. It's almost like I get asked "how do I figure out a margin again?" all the time and just seeing if I could program it just popped into my head.

#include iostream
 
using namespace std;
 
main() {
 
  string sell, cost;
  int row;
 
  cout << "What column is your sell price in?";
  cin >> sell;
 
  cout << "What column is your cost price in?";
  cin >> cost;
 
  cout << "What row are you pasting into?";
  cin >> row;
 
  cout << "Copy and paste the following:" << endl;
  cout << "=(" << sell << row << "-" << cost << row << ")/" << sell << row;
  
}