Back to Browse

A sample Excel VBA Code

2.6K views
Nov 20, 2014
1:36

(see the code at 0:57 sec into the clip) The purpose of the program here is just to show a way to develop a UserForm with basic controls, and also how to take care of data entry issues. This is an Excel VBA program (on a UserForm) that converts temperature units from Fahrenheit to Celsius and vice versa. Here, there was not the need of using the Excel spreadsheets, all calculations were done right into the code. In more complicated programs, spreadsheets are used for both calculations and data manipulation. Please note that there are conversion formulas already included in the Excel formula data bank that can do this, such as "CONVERT(number, from_unit, to_unit)" so a conversion calculation can easily be done on a Spreadsheet. The UserForm contains a Label, two TextBoxes, two CheckBoxes and two CommandButton. All these controls have their own name; in the order shown in the video, they are: Label1 ("ENTER TEMPERATURE"), TextBox1, CheckBox1, CheckBox2, CommandButton1, TextBox2 (Enabled=False; SpecialEffect=0-fmSpecialEffectFlat: set at its properties ) and CommandButton2. There are two variables involved in this program, Fahrenheit (Fr) and Celsius (Cl). Following in the code, there are “If Then” statements for data entry issues that may occur. As you can see in the code, the first “If Then” statement is for the event when the user does not enter anything in TextBox1 or does not check either of the CheckBoxes (OptionButtons could have been used instead of the CheckBoxes). Next in the code is the actual calculation of the conversion from one unit to the other. The formula to convert Fr to Cl is: C = [ (F-32) x 5 / 9 ] and Cl to Fr is: F = (C x 9 / 5) + 32 This is a simple program that shows basic coding and basic controls in a UserForm, it can help one get started in Excel VBA. Note: There are many ways to write a code to accomplish a particular task, many of the tasks here could have been accomplished by writing the code in a different way.

Download

1 formats

Video Formats

360pmp43.1 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

A sample Excel VBA Code | NatokHD