MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2004, 01:23 AM   #1
jcvoth
 
Join Date: Feb 2004
Posts: 198
Default Calculation within VB

Greetings all!

I would like to find a way to solve simple equations through VB and print the result in something like a msgbox.

Here is an example:

******** ******************** ************************************************************************>
Microsoft Excel - Book4___Running: 11.0 : OS = Windows XP
(F)ile (E)dit (V)iew (I)nsert (O)ptions (T)ools (D)ata (W)indow (H)elp (A)bout
=

A
B
C
D
1
NameSales**
2
Bob4**
3
Joe3**
4
Jim2**
5
Jane5**
6
****
Sheet1*

[HtmlMaker 2.42] To see the formula in the cells just click on the cells hyperlink or click the Name box
PLEASE DO NOT QUOTE THIS TABLE IMAGE ON SAME PAGE! OTHEWISE, ERROR OF JavaScript OCCUR.


What I would like is to click a command button, which will trigger a new sub that will sum the range B1:B5 and print the result [14] in a msgbox.

I know how to push an equation into a particular cell, but I would like to run the whole calculation through VB.

Thank you all for your help!
-Jarrod
jcvoth is offline   Reply With Quote
Old Mar 30th, 2004, 01:41 AM   #2
erik.van.geit
MrExcel MVP
 
erik.van.geit's Avatar
 
Join Date: Mar 2004
Location: Belgium 3272 Testelt
Posts: 16,775
Default Re: Calculation within VB

jcvoth,
is this what you are searching for ?

Code:
MsgBox Application.WorksheetFunction.Sum(Range("B1:B5"))
(why did you include B1 in your question? there is no number)

regards,
Erik
__________________
I love Jesus
calm down piano improvisation

email Erik

founder of DRAFT

my free Addins
Table-It download & info
Formula Translator 03
erik.van.geit is offline   Reply With Quote
Old Mar 30th, 2004, 01:45 AM   #3
jcvoth
 
Join Date: Feb 2004
Posts: 198
Default Re: Calculation within VB

Hi Erik,

Yes, this is exactly what I was looking for.

(The B1 was a typo on my part. Should have been B2:B5.)

The sum is an overly simplistic example of what I would like to do. Eventually, I want to push most of my time-intensive calculations into VB such as vlookups, etc.

Thanks for your help!
-Jarrod
jcvoth is offline   Reply With Quote
Old Mar 30th, 2004, 01:45 AM   #4
HalfAce
MrExcel MVP
 
Join Date: Apr 2003
Location: Alaska
Posts: 7,332
Default Re: Calculation within VB

Or to add a little text to the message....
Use Erik's idea like so:
Code:
MsgBox "Your total is " & Application.WorksheetFunction.Sum(Range("B1:B5"))
__________________
XP & '03
Vista & '07
HalfAce is offline   Reply With Quote
Old Mar 30th, 2004, 01:47 AM   #5
erik.van.geit
MrExcel MVP
 
erik.van.geit's Avatar
 
Join Date: Mar 2004
Location: Belgium 3272 Testelt
Posts: 16,775
Default Re: Calculation within VB

you're welcome !
Erik
__________________
I love Jesus
calm down piano improvisation

email Erik

founder of DRAFT

my free Addins
Table-It download & info
Formula Translator 03
erik.van.geit is offline   Reply With Quote
Old Mar 30th, 2004, 01:59 AM   #6
jcvoth
 
Join Date: Feb 2004
Posts: 198
Default Re: Calculation within VB

Okay, so I am getting there, but the vlookup function is failing.

Here is my code thus far:

Private Sub CommandButton1_Click()
lastrow = Range("A65536").End(xlUp).Row
For i = 1 To lastrow
Range("b" & i) = Application.WorksheetFunction.VLookup(Range("a" & i), Range("h2:i27"), 2, False)
i = i + 1
Next i
End Sub


Essentially I am looking up every value in column A and finding its corresponding value in the lookup series H2:I27. I am then printing the results in the B column.

The error I receive is Unable to get the vlookup property of the worksheet class.

Help!
-Jarrod
jcvoth is offline   Reply With Quote
Old Mar 30th, 2004, 02:20 AM   #7
erik.van.geit
MrExcel MVP
 
erik.van.geit's Avatar
 
Join Date: Mar 2004
Location: Belgium 3272 Testelt
Posts: 16,775
Default Re: Calculation within VB

did you have a good reason to put "i = i + 1" in it ?
that's just what "next" does, so you don't need to add 1 each time, Excel does it for you !

"unable to get ..." : I'm not 100% sure of my English here but probably Excel doesn't find the value Range("a" & i) in Range("h2:i27")

go through your code with the functionkey F8 to find the reason of your bug

it's time to zzzzzzzzzzzzzz for me (03:15 AM here)

bye,
Erik
__________________
I love Jesus
calm down piano improvisation

email Erik

founder of DRAFT

my free Addins
Table-It download & info
Formula Translator 03
erik.van.geit is offline   Reply With Quote
Old Mar 30th, 2004, 02:43 AM   #8
jcvoth
 
Join Date: Feb 2004
Posts: 198
Default Re: Calculation within VB

You were right about the i+1... I noticed that shortly after posting. And I was able to fix the vlookup code itself after some continued testing. Thanks for your help!

The final sub was:

Private Sub CommandButton1_Click()
lastrow = Range("A65536").End(xlUp).Row
Set lookuprange = Worksheets("Sheet1").Range("h2:i27")
For i = 2 To lastrow
Range("b" & i) = Application.WorksheetFunction.VLookup(Range("A" & i), lookuprange, 2, False)
Next i
End Sub


-Jarrod
jcvoth is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 11:33 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.