+ Reply to Thread
Results 1 to 18 of 18

Simple Macro - Sum of values that are related to a sprecific word chosen by you.

  1. #1
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Hi,

    I want to do a simple macro but I'm having some troubles and I think you may help me.

    I have a table with just two columns (the first column has numbers and the second words) and some rows. So I want to create a macro that consists on a loop with a condiction inside. This macro would sum all the values of the first column that meet a specific word on the second column like the example bellow where the result would be 2+5= "7". The specific word that I want to test should be specified and written by me at the beginning of the macro in a popup box that in the example bellow would be "food".

    Can anyone help me?


    2 food
    1 football
    5 food
    4 cinema

    Best regards,
    Last edited by Adam Brave; 03-01-2012 at 10:10 AM.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Simple Macro

    Hi Adam, welcome to the forum. Unfortunately you need to read the forum rules and give your thread a proper title, as per rule #1.

    In the meantime - is there a reason you want a macro to do this? It can be done very simply with a formula.

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Simple Macro

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    PLEASE PM WHEN YOU HAVE DONE THIS AND I WILL DELETE THIS POST
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Simple Macro

    Quote Originally Posted by Andrew-R View Post
    Hi Adam, welcome to the forum. Unfortunately you need to read the forum rules and give your thread a proper title, as per rule #1.

    In the meantime - is there a reason you want a macro to do this? It can be done very simply with a formula.

    Hi Andrew-R, for two reasons; with the macro I guess it would make easier for me to choose the word I want to use to do the sum but the main reason is for make myself to start using and understanding how to make macros =P

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    If your scores are in cells A1:A4 and the words are in B1:B4 then you could have another cell, let's say C1, where the user could enter the word and then you could use:

    =SUMIF(A1:A4,B1:B4,C1)

    I.e. SUM the cells in column A where the corresponding values in column B equals the value in C1.

    However, if you're keen to teach yourself macros then it's perfectly understandable that you don't want to use a formula. Is it going to be helpful if somebody here just writes the macro for you, or would you rather show us how far you've got yourself and ask for help/advice on specific topics?

  6. #6
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Well maybe it would be better if someone just writes the macro. The popup box to write the word is easy but I have some difficulties with the main part =\

    Popup box:

    Please Login or Register  to view this content.
    Last edited by Adam Brave; 03-01-2012 at 10:44 AM.

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Sorry to slap you with the forum rules again, but you need to use code tags around any macro code you post. Edit your last post to put some in and then we'll have a look at how to do this.

  8. #8
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Sorry lolol

    Done

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    No problems.

    I'd do this one like this*...

    Please Login or Register  to view this content.
    However a lot of new coders would go through their worksheet row by row, checking each value. This can be very slow indeed for larger sheets.

    * This is a lie, I'd use WorksheetFunction.SumIf to implement the formula I wrote up above, but if I'd forgotten how to do that then I'd do things as above. Probably

  10. #10
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    WOW!! While I was waiting I actually got to write one macro by myself and it works but it takes to long to present the result. Unlike my code your is way faster comparing with mine and it works Oo I still dont understand him at full but I will analyse him

    Thank you very much

    Regards,


    (I send the code bellow)

    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Yes, it's always going to be slower going through the rows 1 by 1, rather than using .Find to find the values you're looking for.

    Aside from that, you don't need:

    Please Login or Register  to view this content.
    Taking those 2 lines out won't change how your code works at all.

  12. #12
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Can you please explain me this piece of code :
    Please Login or Register  to view this content.
    I really dont get it, what is the xlValues and the xlWhole? What do you put in the rngMatchWord variable? What do the "If" condition in your case?

    Thanks

  13. #13
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Using the Find method is the code version of pressing Ctrl-F in Excel and then typing in a word to search for - LookIn:=xlValues tells Excel to search in the values, rather than in the formula, and Lookat:=xlWhole says that the entire cell has to match the sWordToMatch, not just part of it. So searching for "food" wouldn't match "foodstuff", for example.

    What Find returns is a range object, not a variable in the normal sense, but something we can treat as a range. So if we wanted we could put in a line of code saying:

    Please Login or Register  to view this content.
    And the macro would highlight in red each match found.

    The second line of code you quoted checks that a range has been returned. If there is no match for the search term then rngMatchWord will be recorded as Nothing, and if there is no match then we do not need to continue.

    My code calls the Find method in a loop to get every match, but as with Ctrl-F it will loop back around to the beginning once it has found all of the matches. To stop it doing that we record the cell address of the first match, e.g. $B$4 and stop looping when we get back round to that address.

    So, what's in the rngMatchWord variable? Lots of things, we could examine rngMatchWord.Value if we wanted to see what value it has, but that value will be the one we just searched for, so what's important is not what's in rngMatchWord, but where that range is, because we know it's one cell to the right of a value we want to count, so rngMatchWord.Offset(0,-1).Value gets the count for us to add to our running total.

    If you're new to coding, or used to more old-fashioned languages, it can take a while to get your head around these object variables, but they are incredibly powerful and can make coding a lot easier.

  14. #14
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Thanks for the explanation, now I get it

  15. #15
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Just one more question Andrew, when you do:

    Please Login or Register  to view this content.
    I guess I understand the lCOL_OFFSET, you want to check the cell on the left side of the cell you found but I dont understand the lROW_OFFSET = 0. Will you not get stuck in the row zero that way?

  16. #16
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    No, because the row offset is relative to the range rngMatchWord, so a row offset of 0 is just saying to stay on the same row.

    If, say, rngMatchWord is set to the range A1 then rngMatchWord.Offset(0,1) refers to cell B1 and rngMatchWord.Offset(1,0) to cell A2.

  17. #17
    Registered User
    Join Date
    03-01-2012
    Location
    Lisbon, Portugal
    MS-Off Ver
    Excel 2010
    Posts
    60

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Andrew, how do I make result be presented with decimal places?

  18. #18
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Simple Macro - Sum of values that are related to a sprecific word chosen by you.

    Is the result going in a cell or just being displayed in a msgbox?

    If in a cell just format that cell, if a box use the Format function.

+ 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