+ Reply to Thread
Results 1 to 17 of 17

Putting multiplication formula in cells with vba

  1. #1
    Registered User
    Join Date
    03-06-2010
    Location
    Bordeaux, France
    MS-Off Ver
    Excel 2007
    Posts
    12

    Putting multiplication formula in cells with vba

    Hi everyone, I am sure this is very simple, but I cant figure out how to do it I hope someone can help.

    I want to put a simple multiplication formula into a cell....for example:

    Please Login or Register  to view this content.
    .......that is straightforward and works fine, however I need to use variables for the row number of the cells being multiplied so what I really want to do (but of course this syntax won't work) is:

    Please Login or Register  to view this content.
    ...where 'start' and 'finish' are variables (integers) reperesenting row numbers......

    Please can anyone tell me how I make the statement between the quotes work so that the column letter and variables can be combined into cell references to be multiplied and will work between quotes to become the literal contents of the cell?

    thanks

    bj
    Last edited by bj33790; 05-17-2010 at 06:35 AM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Putting multiplication formula in cells with vba

    Would you please edit your post to add code tags?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Putting multiplication formula in cells with vba

    Is this what you're looking for?
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    03-06-2010
    Location
    Bordeaux, France
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Putting multiplication formula in cells with vba

    Quote Originally Posted by foxguy View Post
    Is this what you're looking for?
    Please Login or Register  to view this content.
    Hi, Thanks for your response, I dont think it is quite there, I need to put a formula into the destination cell that multiplies the contents of the two cell references just as though I had typed it for example if variable 'start' was 238 and variable 'finish' was 239 then in the destination cell if I clicked on it, the formula bar would show:

    Please Login or Register  to view this content.
    thanks

  5. #5
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Putting multiplication formula in cells with vba

    well, then something like this

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Putting multiplication formula in cells with vba

    How would you trigger the sub()? If it's triggered from a button or menu, where would it get Start and Finish? If it's called from another sub(), where does that sub() get Start and Finish? Maybe input boxes?

    Also, how would it know what the destination cell is? If it's called from WorksheetChange(), than I'm back to - where does it get Start and Finish?
    Last edited by foxguy; 05-16-2010 at 11:31 PM.

  7. #7
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Putting multiplication formula in cells with vba

    As far as I'm concerned this is about the syntax of this particular statement and not about the whole macro.

  8. #8
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Putting multiplication formula in cells with vba

    So what was wrong with this code?
    Please Login or Register  to view this content.
    I see that I used "lStart" instead of "Start" and "lEnd" instead of "Finish", but that can't be what you think is wrong with it.

  9. #9
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Putting multiplication formula in cells with vba

    Nothing wrong with it. Maybe the OP didn't know how to call the Sub() and tried to run it, which failed.

  10. #10
    Registered User
    Join Date
    03-06-2010
    Location
    Bordeaux, France
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Putting multiplication formula in cells with vba

    Quote Originally Posted by foxguy View Post
    So what was wrong with this code?
    Please Login or Register  to view this content.
    I see that I used "lStart" instead of "Start" and "lEnd" instead of "Finish", but that can't be what you think is wrong with it.
    Many thanks to both you and Teylyn, it was more about the syntax, getting those pesky quote marks in the right place, it works fine now with Teylyn's syntax.

    I understood that you used different variables np, however as a newbie I would really like to know how to use your solution which I am sure is fine, maybe you could explain. Always willing to learn how to do things another way!

    So having got a row number into each of the variables earlier in my procedure, I would simply want to 'call' the sub passing the variables to it and on the return, put the result into a new cell reference on the same sheet. Can you give me an example of how that might look in code please and do I have to declare the variables as a specific type ?

    many thanks

    bj

  11. #11
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Putting multiplication formula in cells with vba

    bj;

    First you have to tell us where the values for Start and Finish come from. Are they in cells on a worksheet, or maybe you want Excel to ask you when you run the sub()?
    Also how do you want the sub() to know what cell to enter the result into? Do you want to put the result into whatever cell is selected? Or maybe have Excel ask you when the sub() is run?

    Also, why do you want a sub() to enter the formula into a cell? Why can't you just manually enter it?

    Answer those questions and we can get started.

    Foxguy

  12. #12
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Putting multiplication formula in cells with vba

    why make it so complicated?

    bj, using foxguy's code in the way presented earlier, you need to set three variables in your code:
    - the range where you want to put the formula, for example dim MyCell as range
    - the first row number (dim start as Long)
    - the second row number (dim finish as Long)

    In your code, you assign values to these variables, e.g.
    Please Login or Register  to view this content.
    Then you call the Sub Foxguy provided, using this syntax

    Please Login or Register  to view this content.
    The Sub will then take these variable's values and assign them to the variables in the Sub.

    Please Login or Register  to view this content.
    rCell will be = MyRange
    lStart will be = start
    lEnd will be = finish

    Then the values will be plugged into the respective places.

    Does that help you understand this a bit better?

    cheers

  13. #13
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Putting multiplication formula in cells with vba

    teylyn;

    I get the feeling that bj is real new to VBA. I can't imagine that (s)he wants to have MyRange, Start, & Finish hard coded into a sub(). Obviously that would only run once, and never be used again without re-coding the sub(). (S)he must have more than one cell to enter a formula into, and is looking for a way to have many formulas entered into many cells, otherwise (s)he would just manually enter the formula.

    I gathered that (s)he was asking how to enter this info
    Please Login or Register  to view this content.
    without hard coding it into a sub()

    Or maybe I misunderstood what (s)he is looking for.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  14. #14
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Putting multiplication formula in cells with vba

    Foxguy,

    my interpretation is that bj was having trouble running the sub with the parameters and did not know how to call a sub that requires parameters.

    Of course, hard-coding the variables in my sample code was only a means of giving them a value. I'm sure bj will know how to assign different values to those variables, and if not, we'll probably see another thread here soon.

    Since bj has marked the thread as solved after my last post, I assume s/he has an answer that satisfies the question, whatever it may have been.

  15. #15
    Registered User
    Join Date
    03-06-2010
    Location
    Bordeaux, France
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Putting multiplication formula in cells with vba

    Quote Originally Posted by teylyn View Post
    why make it so complicated?

    bj, using foxguy's code in the way presented earlier, you need to set three variables in your code:
    - the range where you want to put the formula, for example dim MyCell as range
    - the first row number (dim start as Long)
    - the second row number (dim finish as Long)

    In your code, you assign values to these variables, e.g.
    Please Login or Register  to view this content.
    Many thanks for your kind explanation and patience, I understand how the sub works now and what I was doing wrong. At 60 + years of age and just learning VB with Excel, (I did some programming in BASIC many years ago but VB is much more complex). I have already written quite a lot of code that works fine (although it may not be written in the best way!), but sometimes the simpler things can cause a problem.

    Although the sub works fine if I use the code for example

    Please Login or Register  to view this content.
    if I try to specify the range using a known column letter and a variable for the row and concaternate them, it fails as 'MyRange' ends up containing 'nothing'
    My code is

    Please Login or Register  to view this content.

    Is this another syntax problem to join the column letter with a row number to get a range? I cant seem to find an explanation searching on the internet.

    As I previously posted the problem SOLVED, I am not sure if this should officially be another thread.

    Thanks anyway

    Brian j

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Putting multiplication formula in cells with vba

    Please Login or Register  to view this content.
    That syntax is fine if my_variable is an integer (meaning whole number; it should be declared as a Long) variable between 1 and the number of rows. There's an implicit type conversion, but omitting it isn't a cardinal sin:

    Please Login or Register  to view this content.
    The catenation is unnecessary, though:
    Please Login or Register  to view this content.
    Last edited by shg; 05-17-2010 at 02:05 PM. Reason: Typo

  17. #17
    Registered User
    Join Date
    03-06-2010
    Location
    Bordeaux, France
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Putting multiplication formula in cells with vba

    Many thanks for your patience, I have it all working now
    It's only simple when you know the answer

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1