+ Reply to Thread
Results 1 to 17 of 17

New to writing macros

  1. #1
    Registered User
    Join Date
    11-01-2006
    Posts
    24

    New to writing macros- Please help.

    Hello,

    I need to write a macro that compares the numerical value of 2 cells and give me a result as described below:

    All the numbers would be either whole numbers or whole numbers + 0.5

    Example 1:

    The two values are both whole numbers then no action is taken.

    Cell A = 5
    Cell B = 5

    or

    Cell A = 7
    Cell B = 5

    Result = No action taken

    Example 2

    The two values are both equal half numbers then 0.5 is subtracted from one number and 0.5 is added to the other number to make them both whole numbers.

    Cell A = 5.5
    Cell B = 5.5

    Result = 0.5 is subtracted from Cell A to make it 5, 0.5 is added to Cell B to make it 6.

    Example 3

    The two values are unequal half numbers then 0.5 is subtracted from the number with the lower value and 0.5 is added to the number with the higher value to make them both whole numbers.

    Cell A = 5.5
    Cell B = 8.5

    Result = 0.5 is added to Cell A to make it 6, 0.5 is subtracted from Cell B to make it 8.

    I hope this makes sense.As I mentioned, I'm just learning to write macros so any help is GREATLY appreciated.

    ~Donny
    Last edited by ynnod; 08-31-2007 at 11:00 PM.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Hi Donny,

    I think this code should do what you're asking:
    Please Login or Register  to view this content.
    Substitute A1 and B1 for your two cells. You can have this code run from a command button or a shortcut key (or by manually running through the Macros screen).

  3. #3
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Thank you so much! I believe this will work.

  4. #4
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Ok, so this worked with just two values. I am now trying to get it to work with 48 adjacent cells in 1 column (24 sets of 2 values to compare), and the macro isn't doing anything when I run it. Below is what I've done so far. I've defined all 48 variables (correctly I think) and put in code to process the first two sets of two numbers.

    Can anybody see what's wrong? Please help if you can.

    ~Donny

    Please Login or Register  to view this content.

  5. #5
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    It's probably not working because your first test (value1 and value2) are both whole numbers - which tells the macro to 'Exit Sub', so no more code runs.

    Had I known you were going to compare 48 cells in a vertical column I'd have written the code differently, using a loop. I'll see what I can put together and get back to you.

  6. #6
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Thanks very much Paul. If you also know a way to define all those variable (I hope I am using the right terminology) that would be great as well. It seems like there should probably a easier way.......

    ~Donny

  7. #7
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Donny, try this code instead. It uses a loop, and you only need to declare the variables once, not for every pair of cells.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Hi Paul,

    I can't get it to work yet, I think I'm make a simple mistake. I keep getting a runtime error 13 - type mismatch. Below is what I'm using:

    Please Login or Register  to view this content.

  9. #9
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    You can't change the 60, 61, 106 and 107 to E60, E61, E106 and E107. That's causing the type mismatch -- you're taking numbers and converting them to text, when the variable is expecting a number.

    Please be sure that when you request assistance, you are specific. Your first example showed that you wanted to compare cells in Column D, so I coded it that way. To change it to column E, use the following code (notice the red 5's, which represent column E, while the original 4's represented column D):
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Sorry Paul. My problem was that I wasn't sure what the numbers represented as this is a little beyond what I've attempted before.

    I appreciate the help alot.

    ~Donny

  11. #11
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Oh and it works perfectly now!

  12. #12
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Hello again. Some more help with this loop if anyone knows what I'm doing wrong. I tried to modify the loop that Paul helped me with before to perform the same function on 2 adjacent columns. The error I'm getting is "invalid next control variable reference". I've tried it several different ways but the latest attempt is below

    Please Login or Register  to view this content.

  13. #13
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Hi donny,

    Are you now trying to compare two adjacent rows in column G and separately compare two adjacent rows in column H? Or are you trying to compare cells in columns G vs. H as well?

  14. #14
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Two adjacent rows in column G and separately compare two adjacent rows in column H.

    So basically the exact same thing the first loop did, but then I want it to do the exact same thing to the same rows in a different column, but no comparison between the 2 columns.

    ~Don

  15. #15
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    For clarity, it may just be easier to keep the two columns separate, like so:
    Please Login or Register  to view this content.
    I also changed your 'For k=61...' line to 'For k=60 to 106', since I assumed cells i and k started at the same row, while cells j and l started the next row down (j=61, l=61).

  16. #16
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Thanks!

    Works great. I had actually tried writing it that way but it didn't work, and now I see the mistake I made.


    I'm not sure if I should start a new thread for this, but I'm looking for help writing a loop that will perform the same action on 8 worksheets but stop if it gets to a sheet with no data in cell A1. the data in A1 could be numeric or text. The code I wrote below below does what I want it to, but I have to run it on each individual sheet. Any help would be greatly appreciated.

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    11-01-2006
    Posts
    24
    Sorry, I should have worded that better. I don't want it to stop if there is no data in A1, I want it to skip that sheet and move on to the next one until it has processed all of the sheets

+ 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