ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

A Tutorial: Sales Tax Calculator Program (code) done in Visual Basic (VB)

Updated on June 23, 2012

This hub will show us how to build a total cost calculator in Visual Basic for Applications (VBA). VBA is included in Excel, so all you will need is Excel 2010. It may also work in previous versions of Excel, not sure about that.

In order to use VBA, you will need the Developer tab found within Excel. Here is how to display it. Open up Excel and do as follows:

1. File --> Options (click)

2. Customize Ribbon (click)

3. Check the “Developer” box and click “OK” at bottom.

4. You will now see “Developer” tab. Click it.

5. Section dealing with macros and VBA (Visual Basic) is now visible.

Now we will save the open workbook as an “.xlsm” file, which allows us to work in VB. File --> Save As, and a window will open up. Make file name = “tax” and Save As Type = “Excel Macro Enabled Workbook”.

Now we can write the program which gives us quick and easy cost calculator including sales tax.

Click on “Developer” Tab. Over on far left, see “Visual Basic” tab. The Visual Basic workspace comes up. Two workspaces are open, the VB space and the spreadsheet. We will not do anything with the spreadsheet; it only serves as a place where our GUI, or graphical user interface, will show up. The GUI will be part of our program, and will allow us to enter price and tax rate, then display total cost.

Refer to the diagram/ picture, area #1. In the VB workspace, you will see on left three spreadsheets listed, along with “ThisWorkbook”. Right mouse click on ThisWorkBook and select (left click) on “view code”. Then, in the drop down menu at right, select “workbook” (see picture). This code will be visible:

Private Sub Workbook_Open()

End Sub


All that means is the workbook opens. In the diagram, I show one line of code you need to type in. Put that one line in. What that line does is activate a visual user form, which will be our interface. We will build that form next.

Do as follows. Still working with VB interface, at top of page, Insert --> UserForm. Your form will show up, most likely with a toolbox. If the toolbox does not show we will fix that in a minute. In the VBA Project window, see we now have UserForm1 under ThisWorkbook. Right click on Useform1 and see you can toggle between View Code and View Object. Try that and get a feel for it. When you toggle back to View Object, the toolbox may now be there. If not, there will be a toolbox icon available, see picture area #2. If your toolbox did not come up, click on the toolbox icon and it will appear. See that the icon will toggle the toolbox on and off. Keep it on and drag it to the right of or below the form.

Now we will build the form, our interface. In the picture, area #3, see I have three toolbox icons circled, from left to right: Label, TextBox and Command Button. Hover over them with the mouse in your workspace and you will see the names. In area #4 of the picture, see that I have left clicked (with hold down click) and dragged all three from the toolbox onto the form: three text boxes, three labels, and one command button. If you make a mistake you can left click on any of them, then right click and delete it. Then try again. Now you can slow double click on each and change the text as I show in area #5. If you fast double click, the code view will come up. No problem, just go back to VBA project window, left click on UserForm1 to select it, right click and choose View Object. So we now have our form built as shown in area #5.

Now try this. Close the VB window and the workbook will remain. Save the workbook and close it. Re-open and your form will show up. We have not placed our final code, so it will not work further. Close the form by clicking on the “x” at upper right corner. Let’s get back into the VB interface. Hit the Developer Tab, then Visual basic tab. In VBA project window, left click select UserForm1, right click and select View Code. You will see picture area #6, which is blank code for the form and command button you placed in it. Copy and paste this one line of code between the two lines in the UserForm code:

CommandButton1


In the CommandButton code, paste these lines in between:

Dim bpstr As String, trstr As String, tstr As String

Dim bp As Single, tr As Single, t As Single

bpstr = TextBox1.Text

bp = C(bpstr)

trstr = TextBox2.Text

tr = Val(trstr) / 100

t = bp + tr * bp

t = Math.Round(t, 2)

tstr = Str(t)

tstr = Trim(tstr)

tstr = "$ " & tstr

tstr = Trim(tstr)

TextBox3.Text = tstr

What does the code do? The first one line code we put into the Workbook at the beginning just brings up our form when the Workbook is opened. The other one liner we put into the Userform code simply passes us on to the command button on our form. The big block in the CommandButton code will, once you type in the price and tax, calculate the total price including tax.

Close the VB interface. Save the workbook. Open it up, the form will show. I have an example in picture area #7, where I type in price and tax rate, then hit “calculate” (the command button, remember?). The calculated cost then appears in the third window. We are done. Good job.

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)