+ Reply to Thread
Results 1 to 36 of 36

Calculating grades using a number inside a text string using VBA

  1. #1
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Calculating grades using a number inside a text string using VBA

    Calculating grades using a number inside a text string, using VBA or Formulas.

    I am using Excel 2010.

    On Sheet2 (of the attached file), in Columns G and H, I have a formula using IF-THEN-ELSE to give a Grade level based on the score entered in columns K and L. The formula works perfectly as long as in columns K and L it is entered a number.

    However, my actual scenario is a bit different.

    If you look at Sheet1, in columns K and L, there is a string of text in each column where you can find a three digit number called Scale Score.

    I need to apply the formula from Sheet2 to that three digit number in the text string.

    I tried using MID (to extract the three digit number) and IF in the same formula, but I can’t make it work. The result must go in columns G and H respectively.

    I am using this scenario inside a table (called “Classlog”) where the data starts at row 4 and it has to be applied to the entire column(s) in the table. The solution (formula or VBA) should not work outside the table and it should start in row 4). The table will grow as data is entered so the solution must account for that too.

    I hope it can be done with a formula or VBA. If possible, the solution should not use helper columns (since it would be outside the table and the actual workbook has other data all over the place). Also, if the cells are empty in columns K and L, then the solution should also give a “blank” or empty result.

    I prefer VBA but if both options can be provided much better. If using VBA, can it be done so that it automatically monitors columns K and L and it enters or deletes the result in columns G and H automatically – just as a formula would do?

    NOTE: The actual workbook is already using VBA code for the “Worksheet_Change…” and the “Worksheet_SelectionChange…” Subs, but the code allows for more code to be added as long as it is “self” contained, you know, that it does not end the sub. The attached workbook does not have any code, it’s clean.

    The Scale Score I am using is as follows (which should be able to be edited later):

    Grade Level K - 193 and below
    Grade Level 1 - 194-203
    Grade Level 2 - 204-210
    Grade Level 3 - 211-216
    Grade Level 4 - 217-222
    Grade Level 5 - 223-227
    Grade Level 6 - 228-230
    Grade Level 7 - 231-234
    Grade Level 8 - 235 - 238
    Grade Level 9 - 239 - 243
    Grade Level 10 - 244-248
    Grade Level 11 - 249 - 253
    Grade Level 12 - 254 and above
    Last edited by AliGW; 10-31-2021 at 06:24 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    Where in the workbook have you mocked up what you want?

    To turn text into a real number, you can do this:

    =--MID(K4,13,3)

    Really not sure what you are asking for help with here.

    For grading, you need to set up a lookup table in ascending order and then use INDEX MATCH or VLOOKUP to convert the score to a gradei nstead of nested IFs.
    Last edited by AliGW; 10-31-2021 at 05:30 AM.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    On Sheet1, in cell G4, the solution should render (for example, based on the three digit number inside the text string in cell K4): Grade Level 5

    Also, in cell H4, it should give (based on the three digit number in cell L4): Grade Level K

    And so on down the column.

    Oh, by the way, I used nested IFs because unfortunately I cannot use a reference (lookup) table... it has to be inside the formula or the VBA code - all in one go, self contained.
    Last edited by Luisftv; 10-31-2021 at 05:36 AM.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    Why??? That's completely bonkers!

    Try using the LOOKUP function, then - easier to maintain:

    =IFERROR("Grade Level "&LOOKUP(--MID(K4,13,3),{0,194,204,211,217,223,228,231,235,239,244,249,254},{"K",1,2,3,4,5,6,7,8,9,10,11,12}),"")
    Last edited by AliGW; 10-31-2021 at 05:43 AM.

  5. #5
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    Well,

    Sheet1 is waiting for a solution, what ever that is. Sheet2 is using the nested IFs and they work flawlessly but only if a number is entered.

    Sorry, you are way too fast... I was editing this reply when you had already answered and I only saw the formula you gave after I posted it. I tried your suggestion and I think it work. Give me some time to make sure.

    Thanks.
    Last edited by Luisftv; 10-31-2021 at 05:48 AM.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA or Formulas


  7. #7
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    Yes, Ms. Ali. That's exactly what I needed.

    Now I am wondering if this can be done with VBA - to avoid the formulas being erased, etc., by users...

    Do you mind if I wait and see if anyone will give a VBA solution too, before I mark this as solved?

    Thanks Ms. Ali. I'll start using this right away in the mean time.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    No, but why don't you just protect the cells containing the formulae? It's easy enough to do.

    Just format any cells that need editing as UNLOCKED on the Format ... | Protection tab on the right-click menu, then go to Review | Protect Sheet. You can set a password if you want to. This would prevent your formulae being deleted accidentally - someone would have to deliberately override this setting to do any damage.

    Please don't use Ms - I hate it, and it's not my title. Just use my forum user name. Thanks.

    And thanks for the rep.

  9. #9
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    Sorry about the Ms. In the area where I live it's the only proper and respectful title to use in a neutral way. Although I am not originally from this country either - but I have to adapt to what they use here.

    Yes, I know about locking the sheet and even the code area... the problem with using that option is that then the table sorting will not work properly (the workbook is using INDERCT and other similar formulas that link to other sheets). We have tried but is best not to lock the sheet and whenever possible to use VBA instead since it is out of sight.

    So... in your area, what is the proper way to address with one word in a neutral manner a lady, a single woman, or a married woman when you don't know which one is it? I'm curious... I'm planning to visit England next year and I don't want to end up in a gutter.

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    My advice to you is don't even try. When someone introduces themselves to you, they will either give a first name or a title and surname: wait for that prompt. The country is currently grappling with personal pronouns and all the rest of it: frankly, it's a bit of a minefield.

    Whether a lady is married or not is no more your business than whether a man is married (not you personally, but people in general). The problem with Ms, though, is the other connotations that often go with it. I'm not a feminist, and my title is and always has been Miss, however I don't share that in my profile here because it is none of anyone's business and isn't necessary here. We have user names and that is what you should use to address other members, and nothing else.

    Plesae don't address my as anything other than Ali (in my signature) or AliGW. Anything else is inevitably presumptive. Thanks.

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA or Formulas

    whenever possible to use VBA instead since it is out of sight
    If this is the case, then don't ask for formulae at all - it's a waste of the membership's time given that it will never actually be used. I'm going to update your title.

  12. #12
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Got it.

    Here in Los Angeles the only respectful way is using Ms. Anything else is considered rude or ignorant - and is has nothing to do with being a feminist or not - And yes, it's no bodies business if you are marred or not. Here, the "Ms." is used specifically to be neutral and not to intrude in that private area of someone life, so that no one can tell if you are single, married, widow, etc. I wonder what they use in Australia now... hummm...

    For example, when I address my wife at work (we work at the same place) I have to call her Ms. so and so. When I address my teenage daughter I also use the Ms. and her last name. Here is it the only title to use to show that you are not intruding.

    Anyhow, I meant no disrespect. After all, I have never been to England... but we are planning to go next year. Thanks for the heads up.

    Thanks for your help AliGW. I'll go to sleep now since it's 3:23 am for me.

    Oh, by the way... the title of this post does say "by VBA or Formula" and I did come into the VBA section of the forum.

  13. #13
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    I never waste any code... I use the formulas with the advance users we have. But for those other user who are just learning, that are on training, I rather use the VBA. For example, the workbook I use I rather use the formulas. My wife does not understand formulas and she simply wants to enter data - so her workbook will use the VBA.

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA

    Here in Los Angeles the only respectful way is using Ms.
    Ugh!

    I wouldn't enjoy being in LA, then.

  15. #15
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    I felt the same way when I arrived here... the language it's too... dry, I think is the word. I've been to worst places in the US than LA for sure. The one thing that attracts me about LA is that right or wrong, no one cares, as long as you "live and let live" sort of speak. But yeah, the proper language usage is lacking. If you overlook that for a second then the rest is really great. My wife is from Europe and yes, it took her a long time to "learn" the language used here - she is always correcting me since English is not my first language.

    Good day AliGW. Thanks for your help once more.

  16. #16
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Question: If instead of extracting a number from the cell [using the --MID(K4,13,3)] I simply want to reference to the whole cell (and the cell will only have numbers now), how do you write the formula you provided? Yes, I like that formula. As you said, its much easier to maintain.

    Silly me I tried simply removing the "--MID(K4,13,3)" and leaving the rest but obviously it did not work.

  17. #17
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA

    If I understand you correctly:

    =IFERROR("Grade Level "&LOOKUP(K4,{0,194,204,211,217,223,228,231,235,239,244,249,254},{"K",1,2,3,4,5,6,7,8,9,10,11,12}),"")

  18. #18
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Yes, you got it.

    How do you make this new formula to skip blank cells? Before it was leaving the target cells blanks if the source cells had nothing. Now it is writing "Grade Level K" in all the blank cells. Everything else works perfectly.

  19. #19
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,914

    Re: Calculating grades using a number inside a text string using VBA

    Like this:

    =IF(K4="","",IFERROR("Grade Level "&LOOKUP(K4,{0,194,204,211,217,223,228,231,235,239,244,249,254},{"K",1,2,3,4,5,6,7,8,9,10,11,12}),""))

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, if you have not already done so, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.

  20. #20
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Awesome. Thank you.

    Also, I am working on a VBA solution and when I'm done I'll post it here. It will take me some time since I'm learning as I'm going. For now, I'll mark as SOLVED this post.

  21. #21
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    I'm not sure if I should post a new question or if it belongs here...

    The solution already provided has been working flawlessly. Here it is:

    Please Login or Register  to view this content.

    But now with the passing of time we have found a new necessity:

    Instead of providing whole numbers {"No score","K",1,2,3,4,5,6,7,8,9,10,11,12} now we need it with decimals (the result on screen): 1.2 or 1.5 or 1.9 etc. for each level respectively. How can this be done? Is this possible?


    In other words, as it stands, it works based of this chart:

    Grade Level K - 193 and below
    Grade Level 1 - 194-203
    Grade Level 2 - 204-210
    Grade Level 3 - 211-216
    Grade Level 4 - 217-222
    Grade Level 5 - 223-227
    Grade Level 6 - 228-230
    Grade Level 7 - 231-234
    Grade Level 8 - 235 - 238
    Grade Level 9 - 239 - 243
    Grade Level 10 - 244-248
    Grade Level 11 - 249 - 253
    Grade Level 12 - 254 and above


    But now, we need the result to show "Reading Level: 1.2", or "Reading Level: 1.5", or "Reading Level: 1.7", etc., depending of what score was actually entered between 194 and 203 for Grade Level 1 (and the same for the other levels respectively).

    For example (just an example, not the actual accurate result), if the student got a score of 201 then the grade result will be 1.9 and so on.

    I hope I'm making sense.

    I am hoping that it is a simple tweak to the formula being used (which works flawlessly and we want to keep on using it if possible - we use it within a vba macro). Perhaps the section &LOOKUP(--MID(I6,1,3) has a specific way to add a code here that will allow the grade to be shown with decimals, or maybe at the end of the formula }),"") there is a code like 0.0 or 0,0 or something that could be added to make it show results in decimals. I have experimented with this but no luck. For example with =FIXED(K4) or =FIXED(K4, 1) or ROUND, etc, meaning, K4 is the column where we use the formula, where we need the results in decimals.

    If this is not possible with the current solution already found, can you please point me in the right direction for another formula (another post maybe) that can do what is needed (we will be using that formula with a macro).

    Thank you in advance.
    Last edited by Luisftv; 10-03-2023 at 10:51 PM.

  22. #22
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Calculating grades using a number inside a text string using VBA

    Please remove the "SOLVED" mark if you want to ask something else, and your question will attract more attention that way
    Quang PT

  23. #23
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Oops... thanks for reminding me about that bebo021999. I totally forgot about that.

  24. #24
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Calculating grades using a number inside a text string using VBA

    Which your newest formula, just replace the old results with new one:
    Please Login or Register  to view this content.
    to become:

    Please Login or Register  to view this content.
    with 1.1,2.1,3.3,... is my assumtion of decimal results. Change it to actual value.

    BTW, do you still need a VBA solution?
    If yes, try to attach your current sample file.

  25. #25
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Please see attached file. It is the actual file (minus the real data, I cleaned it).

    Everything works with the file, except that now on columns K and L we need to show the results with decimal places.

    The VBA we are using is a RESET macro which injects all the formulas in the table. Some of our staff sometimes clears the formulas by mistake so there is a button (link to the vba) that they click and it will reset/put back all the formulas, so that part is fine. I also remove that macro and button from the attached file so not to create confusion.

    We just need the formula to show decimals in column K and L.

  26. #26
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Calculating grades using a number inside a text string using VBA

    I test with the first 2 rows: 4 to become 4.6 and 7 become 7.9
    See file attached.

  27. #27
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Yes, that's exactly what we need.

    However, I change the score in column M from 221 to any other number and the result in column K reverts to whole number, it looses the decimal.

    I tried to see what changes you did to the formulas in columns K and L and I can't figure out how you managed the decimals in those two cells highlighted in yellow.
    Last edited by Luisftv; 10-03-2023 at 11:50 PM.

  28. #28
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Calculating grades using a number inside a text string using VBA

    OK. If you understand how LOOKUP works, its easy to fix.
    =LOOKUP(value, lookup_range, result_range)

    If lookup_value = 221
    with lookup_range:
    {0,1,194,204,211,217,223,228,231,235,239,244,249,254}
    lookup_value (221) is found in position #6 (>=217)
    then with result_range:
    {"No score","K",1.1,2.1,3.3,4.5,5.6,6.2,7.1,8.5,9.1,10.1,11.1,12.9}
    the result should be 4.5 (same position #6)

  29. #29
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Ah... I found my mistake. I copy/pasted the solution you provided but it was pointing to the wrong row so I had to modify it to point to it's corresponding row. I will test this more and let you know in a bit.

  30. #30
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    The numbers do not match the expected output score. Yes, now they are decimal, but the result is not accurate, they are a bit off...

    For example, in the file provided, if in cell M4, you change the score to 227 then the calculated score displayed in column K4 should read: "Reading Level 5.9" but instead reads 5.6

    I was able to dig some accurate scale results (these are real results this time) so that you can see what I mean. Enter in cell M4 which ever one of these values and it should give you in cell K4 that exact level:

    In M4: Scale Score=227 --- and K4 should be: Reading Level: 5.9
    In M4: Scale Score=230 --- and K4 should be: Reading Level: 6.9
    In M4: Scale Score=225 --- and K4 should be: Reading Level: 5.5
    In M4: Scale Score=221 --- and K4 should be: Reading Level: 4.7
    In M4: Scale Score=247 --- and K4 should be: Reading Level: 10.7
    In M4: Scale Score=233 --- and K4 should be: Reading Level: 7.7
    In M4: Scale Score=219 --- and K4 should be: Reading Level: 4.4


    Unfortunately, these are the only real scores I managed to dig at the moment, but it should give the correct idea of the decimal output expected (and the calculation of the scale range).
    .
    .
    .
    In other words (according to the scoring table), the formula that we are using now always gives "Grade Level 4" if the Scale Score is between 217 and 222. It is "Level 4" for any score between 217 and 222.

    But what we need now is to show that:

    If the Scale Score is 219 then the Level is 4.4
    If the Scale Score is 221 then the Level is 4.7

    And the formula will display the correct level for all the scores entered under Level 4:

    Scale Score range: 217-222 for LEVEL 4
    If the Scale Score is 217 then the Level is 4.?
    If the Scale Score is 218 then the Level is 4.?
    If the Scale Score is 219 then the Level is 4.4
    If the Scale Score is 220 then the Level is 4.?
    If the Scale Score is 221 then the Level is 4.7
    If the Scale Score is 222 then the Level is 4.?

    Scale Score range: 223-227 for LEVEL 5
    If the Scale Score is 223 then the Level is 5.?
    If the Scale Score is 224 then the Level is 5.?
    If the Scale Score is 225 then the Level is 5.5
    If the Scale Score is 226 then the Level is 5.?
    If the Scale Score is 227 then the Level is 5.9

    And so on, respectively.



    Scoring Table:

    Grade Level K - 193 and below
    Grade Level 1 - 194-203
    Grade Level 2 - 204-210
    Grade Level 3 - 211-216
    Grade Level 4 - 217-222
    Grade Level 5 - 223-227
    Grade Level 6 - 228-230
    Grade Level 7 - 231-234
    Grade Level 8 - 235 - 238
    Grade Level 9 - 239 - 243
    Grade Level 10 - 244-248
    Grade Level 11 - 249 - 253
    Grade Level 12 - 254 and above


    Usually in Excel, you right click on a cell (or a range), then click on "Format Cell", then in the "Number" tab and inside the "Category" you simply click on Number and set it for two decimals, etc., and since the formula is already doing the calculations then is just a matter of adding the decimals. But in this particular case, the cell contains text and number so that usual way does not work here.

  31. #31
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Calculating grades using a number inside a text string using VBA

    Ok, now I understand what you mean.
    Do you want it in , formula, VBA code, or both?

  32. #32
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Can I have it both, the formula and with vba please? The formula is what I need now, but with VBA I can implement it later with more time.

    Thank you.

  33. #33
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Calculating grades using a number inside a text string using VBA

    With formula, it required complex formula combination, which is available in Ex 360, which version both of us do not have.
    For Ex2016 (me) or 2010 (you), it is very complicated formula to do it!
    I tried with VBA first.
    With raw data in column M & N down, results are in K & L
    How to calculate:
    with score: 221. It belongs to 217-223 range, with score = 4
    result = 4 + (221-217)/(223-217) = 4 + 0.67 = 4.67

    try below code:
    PHP Code: 
    Option Explicit
    Sub LevelM
    ()
    Dim lr&, i&, j&, k&, rngres()
    Dim Score&, ScLvLevelLowHigh
    lr 
    Cells(Rows.Count"M").End(xlUp).Row ' last used row
    rng = Range("M4:N" & lr).Value ' 
    data value array
    ReDim res(1 To UBound(rng), 1 To 2' result array
    Sc = Array(194, 204, 211, 217, 223, 228, 231, 235, 239, 244, 249, 254) ' 
    array of score
    Lv 
    = Array(123456789101112' array of Level
    For i = 1 To UBound(rng)
        For j = 1 To 2
            If rng(i, j) <> "" Then
                On Error Resume Next
                Score = CLng(Trim(Split(Split(rng(i, j), "=")(1), "/")(0))) ' 
    number between first "=" and first "/"
                
    Select Case Score
                    
    Case Is 194
                        Level 
    "K" ' lowest level
                    Case Is > 254
                        Level = 12 ' 
    highest level
                    
    Case Else
                        
    = -1
                        
    Do ' loop score within Sc array, until getting higher value
                            k = k + 1
                            If Score > Sc(k) Then ' 
    for examplescore 221
                                Low 
    Sc(k' lower value (for ex =217)
                                Level = Lv(k) ' 
    matching level value (for ex =4)
                            
    End If
                        
    Loop Until Score <= Sc(k)
                        
    High Sc(k'higher value (for ex =223)
                        Level = Format(Level + (Score - Low) / (High - Low), "0.00") ' 
    + (221-217)/(223-217) = 4.67
                End Select
                res
    (ij) = IIf(1"Reading ""Math ") & "Level: " Level
                On Error 
    GoTo 0
            End 
    If
        
    Next
    Next
    Range
    ("K4").Resize(UBound(res), 2).Value res
    End Sub 
    Attached Files Attached Files

  34. #34
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    bebo021999

    Yes, the vba is working fine so far. I will modify our sheet to use it. It's a sheet with a lot of data and many macros already. Thank you. If you can think of a formula do let me know please, since it will be easy to just replace it for the one we have now and the structure of our sheet can remain the same.

  35. #35
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Calculating grades using a number inside a text string using VBA

    How about UDF? Its also formula, but an user-defined-formula?
    Like this:
    K4=LEVEL(M4)
    ?

  36. #36
    Forum Contributor
    Join Date
    11-17-2018
    Location
    Los Angeles
    MS-Off Ver
    Office 2010
    Posts
    208

    Re: Calculating grades using a number inside a text string using VBA

    Yes, I though of that too, but the only UDF formula I came up with requires to know each .x level for each score (instead of a score range). So far, I have not been able to figure out how to make the formula to auto-calculate the .x according to the score entered.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Help - Replace a string in a whole workbook inside formulas
    By Alexander2020 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-02-2020, 04:08 PM
  2. How can I use a formula result inside a text string using IF?
    By trstew in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-21-2017, 12:23 PM
  3. [SOLVED] need to use a text string inside range - errors
    By fredderf81 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2013, 03:29 PM
  4. Increasing a number inside of a URL string
    By Delta223 in forum Excel General
    Replies: 1
    Last Post: 10-23-2009, 03:33 PM
  5. Search text inside a string
    By pimar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2006, 01:01 PM
  6. Renaming files from a number string inside the file.
    By Gordon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-24-2006, 01:10 PM
  7. newbie need help:sum number inside string
    By cuongvt in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-03-2005, 10:56 PM

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