Sie sind auf Seite 1von 6

Calculations using macros in Microsoft Excel

Once you access the Excel cells using the range or cells property, you can easily perform calculations using macros based on your knowledge of the domain, example finance, in whcih you may be working. You can add, subtract, average, multiply or divide according to the needs of the sitauation. Of course, you can use the Excel inbuilt functions or your own functions to perform different calculations. The main idea here is to automate the calculations and hide the formulas from prying eyes.

Click on the developer tab Select Visual Basic from the Code group In the Microsoft Visual basic Window click on insert in the menu bar Select Module In the workspace enter the code using the range or cell property and apply an appropriate formula Click on 'Run' to view the result Of course, using the cells or range property you could have also added the labels The code is given below: Sub calculate() Range("A4") = "Tom" Range("B4") = 5000 Range("C4") = Range("B4") * 0.5 Range("D4") = Range("C4") + Range("B4") End Sub Watch the video below to see the implementation.

Interactive Calculations Macro Using The Activecell and Offset Properties and The Inputbox Function

Today we will see how we can create an interactive calculations macro using the Activecell and Offset properties and the Inputbox function. Intuitively most people know what is an activecell. It is the cell which you have selected with the mouse or keyboard and is outlined dark and whose address is dispalyed in the name box. You can enter data only in the selected or active cell.

Offset just means how far, row-wise and column-wise, you are or want to move from an active cell. Offset takes two parameters. For example: ActiveCell.Offset(0,1). This means that you wish to be in the same row but one column to the right of the activecell ActiveCell.Offset(2,-3). This means you wish to move two rows below and three columns to the left of the activecell. ActiveCell.Offset(-2,-1): here you wish to move two rows above and one column to the left of the active cell. The general Syntax of the Offset property is: Offset(RowOffset,ColumnOffset): RowOffset equals number of rows (positive, negative, or 0 (zero)) by which the range is to be offset. Positive values are offset downward, and negative values are offset upward. The default value is 0. ColumnOffset: The number of columns (positive, negative, or 0 (zero)) by which the range is to be offset. Positive values are offset to the right, and negative values are offset to the left. The default value is 0. Inputbox Function: Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box. Let's take a look at the macro's code below: Sub test() Dim qty As Integer Dim price, amount As Single Range("A5") = "Item" Range("B5") = "UnitPrice" Range("C5") = "Quantity" Range("D5") = "Amount" ActiveCell.Offset(1, 0).Select ActiveCell = InputBox("Enter the name of item") ActiveCell.Offset(0, 1).Select price = InputBox("Enter the price") ActiveCell = price ActiveCell.Offset(0, 1).Select qty = InputBox("Enter the qty") ActiveCell = qty ActiveCell.Offset(0, 1).Select amount = price * qty ActiveCell = amount

ActiveCell.Offset(0, -3).Select End Sub After the name of the macro, we define the data type for qty (quantity), price and amount so that the right kind of data is transferred from the inputbox to the Excel worksheet cells. Next we assign where the headers or labels will be located. Once the inputbox pops up and data is entered into it, it is transferred to the worksheet and using the offset property we move proper rows and columns away from the activecell. After the calculations we move again to the proper cell so that data entry can take place at the correct location under the correct labels. IMPORTANT: You must position your cursor at cell A5 before you run the macro. Let's watch the video below and see the macro in action.

IF-ELSE FUNCTION MACRO

The IF-ELSE function in Microsoft Excel is one of the most versatile functions and it is very difficult to avoid using this function in solid calculations or analysis. 'If' helps the user to calculate a value if a condition evaluates to TRUE and another value if the condition evaluates to FALSE. Syntax: IF (logical_test,assign_value_if_true,assign_value_if_false) For example, you could give the buyer of your goods a discount of 10% if she purchases a quantity greater than or equal to 12 of a product and a discount of 5% if she purchases a quantity less than 12. The code for the above macro is given below. Sub how_to_use_if_else() Range("D4") = Range("C4") * Range("B4") If Range("B4") >= 12 Then Range("E4") = Range("D4") * 0.1 Else Range("E4") = Range("D4") * 0.05 End If Range("F4") = Range("D4") - Range("E4") Range("F4") = Round(Range("F4"), 2) End Sub The if-else function macro first calculates the 'Amount' based on the quantity

purchased and the price of the item. The next step determines whether the purchaser bought a quantity greater than or equal to 12. If true the discount of 10% is given else a discount of 5% is given. Next the final amount due from the customer is calculated. The final amount is then formatted to two deicmal places. Watch the training video below to see the macro in action.

Payroll program using VBA and Vlookup


Create a user-form in the Visual Basic Editor with the same fields as in the Employee Information and Payroll Calculator worksheets in Excel. Now using a command button transfer the appropriate data from the filled user-form into the relevant worksheets. UsingVlookup then create a stub from the poulated worksheets. The complete code is given below: Private Sub CommandButton2_Click() TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = "" TextBox7.Text = "" TextBox8.Text = "" TextBox9.Text = "" TextBox11.Text = "" TextBox12.Text = "" TextBox14.Text = "" TextBox16.Text = "" TextBox19.Text = "" End Sub Private Sub CommandButton3_Click() End 'You can also use Unload Me End Sub Private Sub CommandButton5_Click() Dim erow As Long Dim ws As Worksheet Set ws = Worksheets("EmployeeInformation") ws.Select ws.Activate erow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row Cells(erow, 1) = TextBox1.Text

Cells(erow, 2) = TextBox2.Text Cells(erow, 3) = TextBox3.Text Cells(erow, 4) = TextBox4.Text Cells(erow, 5) = TextBox5.Text Cells(erow, 6) = TextBox6.Text Cells(erow, 6).Select Selection.NumberFormat = "0.00%" Cells(erow, 6) = Cells(erow, 6) / 100 Cells(erow, 7) = TextBox7.Text Cells(erow, 7).Select Selection.NumberFormat = "0.00%" Cells(erow, 7) = Cells(erow, 7) / 100 Cells(erow, 8) = TextBox8.Text Cells(erow, 8).Select Selection.NumberFormat = "0.00%" Cells(erow, 8) = Cells(erow, 8) / 100 Cells(erow, 9) = TextBox9.Text Cells(erow, 9).Select Selection.NumberFormat = "0.00%" Cells(erow, 9) = Cells(erow, 9) / 100 Cells(erow, 10) = Val(TextBox6.Text) + Val(TextBox7.Text) + Val(TextBox8.Text) + Val(TextBox9.Text) Cells(erow, 10).Select Selection.NumberFormat = "0.00%" Cells(erow, 10) = Cells(erow, 10) / 100 Cells(erow, 11) = TextBox11.Text Cells(erow, 12) = TextBox12.Text Cells(erow, 13) = Val(TextBox11.Text) + Val(TextBox12.Text) End Sub Private Sub CommandButton6_Click() Dim erow As Long Dim ws As Worksheet Set ws = Worksheets("PayrollCalculator") ws.Select ws.Activate erow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row Cells(erow, 1) = TextBox1.Text Cells(erow, 2) = TextBox2.Text Cells(erow, 3) = 40 Cells(erow, 4) = Val(TextBox14.Text) - 40 Cells(erow, 5) = Val(TextBox16.Text)

Cells(erow, 6) = 40 * Val(TextBox3.Text) + Val((TextBox14.Text) - 40) * Val(TextBox16.Text) Cells(erow, 7) = (Val(TextBox6.Text) + Val(TextBox7.Text) + Val(TextBox8.Text) + Val(TextBox9.Text)) * ((40 * Val(TextBox3.Text) + Val((TextBox14.Text) - 40) * Val(TextBox16.Text)) / 100) + Val(TextBox11.Text) + Val(TextBox12.Text) Cells(erow, 8) = TextBox19.Text Cells(erow, 9) = (40 * Val(TextBox3.Text) + Val((TextBox14.Text) - 40) * Val(TextBox16.Text)) - (Val(TextBox6.Text) + Val(TextBox7.Text) + Val(TextBox8.Text) + Val(TextBox9.Text)) * ((40 * Val(TextBox3.Text) + Val((TextBox14.Text) - 40) * Val(TextBox16.Text)) / 100) - Val(TextBox11.Text) Val(TextBox12.Text) TextBox19.Text End Sub

Das könnte Ihnen auch gefallen