+ Reply to Thread
Results 1 to 45 of 45

Macro / Msgbox VBA help!

  1. #1
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Macro / Msgbox VBA help!

    Hi there,
    I have an issue come up with one of the macro in my spreadsheet,

    I would like it to search through the dates in column Y (the first date starts at row 20), and if every single one is larger than LDays (which is set to 31), to display a message box (but only once - currently it displays it for every single date that is larger than LDays.) - so basically if all the dates are more than 31 days in the future.

    I have tried searching online and trying different things, but can't seem to solve it.

    I think it may have something to do with the specific IF statement lying within the
    Please Login or Register  to view this content.
    part of the code. I have tried it outside of this part, but then the message box constantly pops up.

    Everything else works fine, other than this part:
    Please Login or Register  to view this content.
    This is the complete macro code so far:

    Please Login or Register  to view this content.
    Many thanks!

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,176

    Re: Macro / Msgbox VBA help!

    put a global flag to show/not show the message anymore

    public gbHideMsg as boolean


    then once it shows, turn it on and it never shows again:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    I'd take the last if statement out of the loop all together and check it like below (before loop).

    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  4. #4
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    This works properly when there are no expired or any within 31 days, but even if there are any that are expired or within 31 days it still pops up.

    Any idea how to solve that??
    Have I put the bit of code in the wrong place? I have put the old statement in as a comment for my own use.

    Please Login or Register  to view this content.

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Can you upload a sample sheet? Where added code fires when it shouldn't?
    When I tested on my end, it behaved as expected.

  6. #6
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Spreadsheet attached
    Attached Files Attached Files

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Ah, so your cell has some formats etc and rCount comes back as 56. That's what's throwing off calc. Use Column "Y" instead of "Z" for rCount.

    Also, your insert location is off. The check should happen before While LRow < 200 line (completely outside of loop).

    Following should work.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Even with all the dates far in the future, the rCount section does no longer even display a msgbox at all - whereas before it showed it no matter whether the date had expired or whether it is far in the future.
    Last edited by NatWally; 11-20-2017 at 11:46 AM.

  9. #9
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Huh? I'm not sure I get you. It only shows msg box "There are either no expired safeguarding certificates, or no certificate expiring within the next 31 days."...
    When there are no item that falls within 31 days or less. I.E. When all expiry date is over 31 days away.

    Works fine on my end... see image below.
    0.JPG

  10. #10
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    I've tried using those dates, plus the British way of those dates, and I am still not getting the msgbox.
    Are you able to attach the spreadsheet back to me to see what seems to be different between mine and yours?

  11. #11
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Here you go.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    I have downloaded the spreadsheet and tried using the button, but nothing happens.

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    You used the file I uploaded right? Works fine on my end. Did you forget to enable macros?

    If you click as is, it will show the message for no certificate expiring within next 31 days.

  14. #14
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Macros all enabled. I click the button, and nothing happens.

    No idea why it is working on yours and not mine.

  15. #15
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    It's not likely the code itself. Try checking your Personal.xlsb and if you have add-ins turn them off and see if any is interfering with the code.

    Other than that, I'm can't say what may be causing this. It's fairly simple and straight forward code.

  16. #16
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    I think I have managed to sort it with:
    Please Login or Register  to view this content.
    Try that and see if that works on yours.

  17. #17
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Hm? Just use it if it works for you.

  18. #18
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Does that not work on yours?

    If you try it so there is an expired date, expiring date, and one over 31 days away, it shouldn't appear - or at least it doesn't on mine.

  19. #19
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Looking at code I see no reason it should not.

    I just prefer not to check something in every iteration of the loop when I can check in one shot outside of loop

  20. #20
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Okay forget that, I have tested it several times in different scenarios, and it appears that if you have two dates that are over 31 days it appears - even if there are some that have expired.

  21. #21
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    So pasting code from post #7 doesn't work? Is there formula in Column Y?

  22. #22
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    No since it was in Y, I have added another column for another detail.

    It seems to now work if I use AND NOT when listing the formulae previously used.
    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    And that now doesn't work if all the dates in column Z are more than 31 days.

    I just don't know what alternatives or other ways there are now.

  24. #24
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    If your sample accurately represent your actual workbook, my code should work. Tested on Excel 2013 & 2016. On Win 7 & 10.

    Here's alternate approach using different logic. See if this at least gives you correct result.
    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    This is the result I cam currently getting - the first name and surname of the person are not appearing. The wording for those that have already expired, and those that are expiring should be slightly different. Those that have expired should be like it was before with the wording and then just the date. vba.jpg

  26. #26
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Then your workbook structure changed from sample.
    Upload sample that accurately reflect your actual structure.

  27. #27
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    I'm using the same one as you sent me back.
    Attached Files Attached Files

  28. #28
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    From what I have just tested (with my old code), I think I have sussed out what the problem is.

    The 'no expired' message is determined by what the last date in the column is.
    * the last date in column Z is expired, it doesn't come up - how it should be.
    * the last date in column Z is not expired or is not expiring, and there are no expired elsewhere, it comes up - how it should be
    * the last date in column Z is not expired or is not expiring, and there ARE expired dates somewhere else, then the message comes up - should not be like this

    It must be down to one of these two bits
    Please Login or Register  to view this content.
    There must be one part of it that it is making it execute by determining whatever the value in the last cell is?

  29. #29
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    My code doesn't use cells/range other than to put it into array. tempArr(i, 26) is equivalent of checking column Z for the date in array.

    Well, no wonder you don't get first and last names for items that expired, or is going to expire within 31 days... you don't have it in your data.

    Only row 20 to 22, which all are more than 31 days away have names in column A & B. Fill First & Last appropriately and you will see correct result.

    FYI - Anything above "====" separator is items that's going to expire within 31 days, anything below is those that already expired.

  30. #30
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Sorry, my testing was on my old code.

    I completely forgot there weren't names in the cells for your code!
    That seems to work perfectly now.
    Thank you!

    Also, is there any way of making it so the length of the names don't matter? Currently there are massive gaps between names and the rest of the text as the length of the string is predetermined in the code.
    I would like it so there is just one space between the names and text, rather than having the predetermined space (I hope that makes sense).

  31. #31
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Sure.
    Please Login or Register  to view this content.
    See attached as well.
    Attached Files Attached Files

  32. #32
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    This is brilliant, thanks for all your help!

    I have one more request:

    So I also need the array to identify where there might the expiry date may have a gap (as long as there is a name in column A and B).
    I have tried using the following code in different parts of the vba, but cannot get the array to display it correctly - currently the array displays it among the 'Expiring' section'.
    Please Login or Register  to view this content.

  33. #33
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Not sure I understand your requirement. Can you demonstrate it in a sample workbook?

  34. #34
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    So what I need is:

    If there is no date found in a cell in "Expiry Date", but there is a name (or characters) located in First Name and Surname (so column A and B), then a message will appear - the message similar to that listed in the code above.

    Does that make sense?

  35. #35
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Then I'd do it like below.
    Please Login or Register  to view this content.

  36. #36
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    This has no effect on anything - nothing happens!

  37. #37
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Sure it does. Did you add data with just the first and last name without the expiration date?

  38. #38
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Yep!
    I no longer even get the array for if one has expired / is expiring.

  39. #39
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Upload your sample that this code produces nothing on.

    Works fine on my end.
    0.JPG

  40. #40
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    It seems to work okay on this dummy copy, but on my main spreadsheet (which I can't show), it doesn't work. Is the array currently limited to a certain number of rows?
    My main spreadsheet is down to row 117, and will continue to go down the page further.
    Attached Files Attached Files

  41. #41
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    No, array will expand based on what is in Column A, and as far as I know there isn't set limit (I routinely work with array with row size over 400k).

    And will span from "A20:Z" & last row with data in column A. Ex. A20:Z28 in your sample file.

    Likely culprit is some structure difference from sample to your actual workbook.

  42. #42
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    All the columns in my main workbook are the same as the one in the sample one I sent, so absolutely no idea as to why it isn't working.
    Tomorrow I will send over my main workbook (but I will remove the confidential data), so you can see exactly how that workbook is set up.

    I will try and workout why it isn't working in the mean time.

  43. #43
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Sorry it has been so long.

    Please see the attached spreadsheet.

    The array doesn't seem to come back with anything.

    Thanks!
    Attached Files Attached Files

  44. #44
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Macro / Msgbox VBA help!

    Just change the last If statement like below. Only item that meet criteria is one No Safe Guard in your sample.
    Please Login or Register  to view this content.

  45. #45
    Registered User
    Join Date
    10-20-2017
    Location
    Oxford, England
    MS-Off Ver
    2016
    Posts
    52

    Re: Macro / Msgbox VBA help!

    Brilliant, thank you!

+ 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. Replies: 1
    Last Post: 07-13-2016, 12:14 PM
  2. [SOLVED] Macro then msgbox
    By daveyc18 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-22-2015, 10:40 AM
  3. [SOLVED] MsgBox AFTER macro has run
    By realrookie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-16-2015, 07:59 AM
  4. Macro to capture MsgBox prompt to a string variable but ignore the MsgBox
    By BuglerDobbs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-07-2015, 10:56 AM
  5. Macro for MsgBox if...
    By shattered_z in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-17-2014, 03:51 PM
  6. [SOLVED] Problem with VBA Editor uses lower case on some lines (ex. msgbox instead of MsgBox)
    By stubbsj in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-14-2013, 06:59 PM
  7. [SOLVED] Call another Macro or Stop Macro Based on MsgBox Yes or No
    By GStone in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-03-2012, 05:59 PM

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