+ Reply to Thread
Results 1 to 28 of 28

Macro needed to compare multiple cells

  1. #1
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Macro needed to compare multiple cells

    I am new to Excel macros and am currently right now trying to write a macro that would highlight a row if a condition existed. The best way that I can I explain is it as follows:

    If
    cell M >= (90% of Cell N) and cell O >= (90% of Cell N)
    then
    Highlight the row

    I have over 3400 rows and I have concluded that writing a macro would be the most efficient way. I am open to any suggestions. Thanks in advance
    Last edited by spwolfe; 06-10-2011 at 10:07 AM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    I think this will do, let me know:
    Please Login or Register  to view this content.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    I really appreciate the response.

    I seem to be pulling a "Run-time error '1004': Method 'Range' of object '_Global failed on this line:

    Set TtlRng = Range(LRng, HRng)

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    Hi spwolfe, one thing you need to change and I'm sorry I didn't state it, is the sheets reference here:
    Please Login or Register  to view this content.
    You need to change that to whatever sheet it is that you want this code to work on. I had it as 6 because that is where I created the mock data in my own workbook.

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    In fact, you should probably declare the worksheet, which I forgot to do also.

    Code Change:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    So for example if my sheet's name was Convert, I would do as follows:

    Set ws = Worksheets("Convert")

    Correct? When I do that I still pull the previous error that I stated.

  7. #7
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    Perhaps if you can upload a mock workbook with non-personal data, this problem may be rectified.

  8. #8
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    I really appreciate your time. Here is the link for an example

    http://www.megaupload.com/?d=HSUFBXOL

    Hopefully that will suffice. Let me know if I should provide anything else.

  9. #9
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    Can you upload in advanced settings? I cannot access that page because it is blocked by my employer. Or perhaps someone else can get that file and try to fix it!

  10. #10
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    I apologize about that. I am new to this forum. Maybe this will work.
    Attached Files Attached Files

  11. #11
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    Hi spwolfe, that was much better for me. I made a slight ammendment to the code.
    Please Login or Register  to view this content.
    I think the calculation is right but you may want to double or even triple check that the the proper rows are being highlighted. Good luck
    Last edited by Mordred; 06-08-2011 at 03:47 PM.

  12. #12
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    Ok. I think progress been made with the macro on my part. Now I am getting another error. I am now getting the following:

    Run-time error '13':
    Type mismatch

    on this line:

    If cl.Value >= (cl.Offset(0, 1).Value - (cl.Offset(0, 1).Value * 0.1)) And _
    cl.Offset(0, 2).Value >= (cl.Offset(0, 1).Value = (cl.Offset(0, 1).Value * 0.1)) Then

    I appreciate your patience with this issue.

  13. #13
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    My apologies, I had a typo. I guess i tapped a key funny. I really really appreciate your help! Thanks a ton!

  14. #14
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    Sorry to bother again. As I approached the bottom of the worksheet I noticed what you said with the validity. It is not comparing the values of O to the N value.

  15. #15
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    As you approach the bottom, are there values in Columns M, N, & O that are not numerical?

  16. #16
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    Attached is the full list of number from M, N, and O
    Attached Files Attached Files

  17. #17
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    Sorry spwolfe but I have to clarify, in your new upload you have data from Range("M1:037") and then data from range ("A40:C3409"). Should it all be in columns M, N, & O?

  18. #18
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    This works for me without error (and there are no spelling mistakes this time-sorry)
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    My apologies about the other spreadsheet. I think it may be just comparing M with N rather than comparing M to N and O to N as well. If that is true then that row I would like to highlight. I tried that last bit of code that you posted and it seems to be pulling the same results. I am going to attach the correct worksheet. Again I really appreciate your time.
    Attached Files Attached Files

  20. #20
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    Hi spwolf, I wonder if it is a compatability issue. I am at work and I am using Office 2003 while you are using 2010 according to your info. In my version, there are no errors and the code moves fast through the workbook you provided in post #16. I'll ask for more assistance and hopefully there is something minor that can be worked out to get you up and running.

  21. #21
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    I just ran the same code from post #18 in the new upload from you and received 0 errors so I am at a loss for words right now. Sorry and thanks for YOUR patience on this matter.

  22. #22
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    It is running without errors, but for instance in column O on the first one it highlights on my spreadsheet, the row should not be highlighted because O is more than 10% of the value of N. I apologize if I haven't made myself clear.

  23. #23
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    No worries spwolfe, you have been very clear, perhaps my mind is not firing properly today.

  24. #24
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Macro needed to compare multiple cells

    spwolfe,

    For something like this, no macro necessary, just use conditional formatting.
    Attached is a modified version of your sample workbook. The cell range M1:O3409 has had the following conditional formatting rule applied:
    =IF(AND($M1>=0.9*$N1,$O1>=0.9*$N1,$M1<>""),TRUE,FALSE)

    Hope this helps,
    ~tigeravatar
    Attached Files Attached Files

  25. #25
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,510

    Re: Macro needed to compare multiple cells

    You could test which rows should be highlighted with the following formula:

    =IF(AND(M1="",N1="",O1=""),"",IF(AND( M1 >=N1*(90%), O1 >=N1*(90%)),"highlight","")) in row 1 and copy down.

    The sample workbook and Mordred's code worked for me in Excel 2007 on Windows 7.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  26. #26
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Macro needed to compare multiple cells

    I just compared highlighted cells from my code to tiger's and they highlight the same so if the results aren't as you want them then you have given us the wrong criteria for what your output should be.

    Regards:

  27. #27
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,620

    Re: Macro needed to compare multiple cells

    Slightly modified version:
    Please Login or Register  to view this content.
    select the entire columns A - O and apply at one time
    Attached Files Attached Files
    Ben Van Johnson

  28. #28
    Registered User
    Join Date
    06-07-2011
    Location
    Mississippi
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro needed to compare multiple cells

    Ok guys I think I have figured it out. The formula I actually used after reviewing the last few posts was the following:

    =IF(AND($M1>=0.9*$N1,$O1<=1.1*$N1,$M1<>""),TRUE,FALSE)

    Attached is the file

    I greatly appreciate the help that I have had with this.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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