Arithmetic in Visual Basic

B

Ben K. Bullock

I know next to nothing about Visual Basic, but due to the extreme tedium and
frequent mistakes in typing in very repetitive sequences of
invoices/estimates/delivery notes involving the same numbers in several
places, I made the following function to make the documents automatically
using fields in a Word template. Basically I grabbed some code off the web
somewhere and messed around with it until it did more or less what I wanted.
There is also a pop up box where I can type in the numbers, and it is set to
open automatically when the template is clicked for a new document.

This works very well as far as it goes, and saves me several minutes of
fiddling and possible typing errors or forgetting to change numbers when
typing the complicated multipage repetitive invoices. However, in the
following, NoTaxCost should be UnitCost multiplied by NoOfPages, and
TotalCost should be that multiplied by 1.05 for the sales tax. So to be even
more lazy I would like this to calculate automatically for me and only have
to input two numbers, the unit cost and the number of pages I'm invoicing
for. It sounds simple enough, and I tried some things, as commented out
below, but they didn't work. What should I be doing?

Thanks for any assistance.

B. Bullock.

Private Sub CommandButton1_Click()

Dim UC As Integer

UC = Val(UnitCost.Text)
'NoTaxCost As Integer
'TaxCost As Integer
'TotalCost As Integer

'UnitCost := 2500

'NoTaxCost = UnitCost * NoOfPages
'TaxCost = NoTaxCost * 0.05
'TotalCost = NoTaxCost + TaxCost

With ActiveDocument
.Bookmarks("TaxCost").Range _
.InsertBefore TaxCost
.Bookmarks("CompanyName").Range _
.InsertBefore CompanyName
.Bookmarks("NoOfPages").Range _
.InsertBefore NoOfPages
.Bookmarks("NoTaxCost").Range _
.InsertBefore NoTaxCost
.Bookmarks("TotalCost").Range _
.InsertBefore TotalCost
.Bookmarks("UnitCost").Range _
.InsertBefore UnitCost
End With
'select all text

Selection.WholeStory

'update fields

Selection.Fields.Update

'move cursor to deselect

Selection.MoveUp Unit:=wdLine, Count:=1
UserForm1.Hide
End Sub

Private Sub TotalCost_Change()

End Sub

Private Sub UserForm_Click()

End Sub
 
J

Jezebel

This line leaps out --

'UnitCost := 2500

You use colons in assignments only in function arguments, not in ordinary
code.

UnitCode = 2500

Also, the one declaration you have is Integer, which you certainly don't
want for currencies. (Integers have no decimal places.) Use

Dim UC as currency

and declare all your other variables also. Make it a rule always to include
Option Explicit at the top of each module. This is general good practice,
and it's CRITICAL in code that deals with money.
 
B

Ben K. Bullock

Jezebel said:
This line leaps out --

'UnitCost := 2500

You use colons in assignments only in function arguments, not in ordinary
code.

UnitCode = 2500

Thank you. I changed the things as you suggest and also put "Dim" there, and
now the thing is doing what I wanted it to. Problem solved!
Also, the one declaration you have is Integer, which you certainly don't
want for currencies. (Integers have no decimal places.) Use

Dim UC as currency

and declare all your other variables also. Make it a rule always to
include Option Explicit at the top of each module. This is general good
practice, and it's CRITICAL in code that deals with money.

Thanks, I'll bear that in mind for future projects.

Thanks again!

B. Bullock.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top