+ Reply to Thread
Results 1 to 14 of 14

ElseIf Issues

  1. #1
    Registered User
    Join Date
    02-16-2017
    Location
    Kentucky
    MS-Off Ver
    2013
    Posts
    6

    ElseIf Issues

    I'm trying to setup a macro that will check three designated cells for zero values. There are two subs; one to designate cells to be used and another to give give message boxes depending on cell values. If any zero values are found, a message box will come up stating that one of the cells has not been given a value other than zero ("bill has not been entered"). If all three cells have a value other than zero, a different message box will come up stating that all cells are filled in (all bills have been entered). The problem I'm having is that the message box for "no gas bill has been entered" comes up regardless of whether the cell has a real value or zero.
    Here's what I've got so far:

    Sub SetBills()

    Dim ElectricBill As Integer
    Dim GasBill As Integer
    Dim WaterBill As Integer

    Set ElectricBill = Sheets("LGE Info").Cell(E27).Value
    Set GasBill = Sheets("LGE Info").Cell(E32).Value
    Set WaterBill = Sheets("Water Bill").Cell(D16).Value


    End Sub

    Sub Ready()
    '
    ' Ready Macro
    ' Tells admin if bills are ready to be printed.
    '

    '

    If GasBill = 0 Then
    MsgBox "No, the gas bill has not been entered yet."
    ElseIf ElectricBill = 0 Then
    MsgBox "No, the electric bill has not been entered yet."
    ElseIf WaterBill = 0 Then
    MsgBox "No, the water bill has not been entered yet."
    Else
    MsgBox "Yes, bills are ready to be printed."
    End If

    End Sub


    I'm at a loss as to why I'm not getting the right message box. Help!

  2. #2
    Registered User
    Join Date
    08-10-2012
    Location
    Derby, England
    MS-Off Ver
    2010 (Work) / 2016 (365) (Home)
    Posts
    14

    Re: ElseIf Issues

    Hi,

    I have identified the problem (variables being out of scope), but to give you what I feel would be the optimal solution, I have a few questions, which hopefully will allow me to help further:

    1. Is there a reason for there being two subroutines rather than one? From what I tell of the workflow, it would make more sense to amalgamate them into one, but if there is a functional reason we can work around that.
    2. Are these values for the gas, electricity and water to be used anywhere else in the VBA for this project?
    3. Would it be beneficial for the user to have one message box that displays all of the pertinent information about what is and is not entered correctly, rather than up to three separate message boxes? I know I would quickly get annoyed by that as a user. This is a slight aside from the issue, but may improve the code.

    Thanks,

    ---
    Bernieburnham

  3. #3
    Registered User
    Join Date
    02-16-2017
    Location
    Kentucky
    MS-Off Ver
    2013
    Posts
    6

    Re: ElseIf Issues

    1. The only reason there are two subroutines is that I wrote the second sub first, then went back and wrote the other.
    2. Gas, electricity, and water will not be used anywhere else in this macro. They may be used in another macro, though.
    3. The purpose of the message box is to get the user to stop and verify that the worksheet has been completely updated for the month. If you have a better idea on what the message box should display, I'd be happy to hear it.

  4. #4
    Registered User
    Join Date
    08-10-2012
    Location
    Derby, England
    MS-Off Ver
    2010 (Work) / 2016 (365) (Home)
    Posts
    14

    Post Re: ElseIf Issues

    Hopefully the following should work (I haven't been able to test it unfortunately):

    Please Login or Register  to view this content.
    The problem was that because the variables were declared inside the first subroutine, they were specific to that subroutine and thus cannot be referenced anywhere else. They are also destroyed as soon as the subroutine gets to End Sub. Variables can be declared at Module, Project or even Global level, but having everything in one subroutine negates that problem. For more, please read the fine work of Chip Pearson on the subject: http://www.cpearson.com/excel/scope.aspx.

    Hope that helps,

    ---
    Bernieburnham

  5. #5
    Registered User
    Join Date
    02-16-2017
    Location
    Kentucky
    MS-Off Ver
    2013
    Posts
    6

    Re: ElseIf Issues

    I like what you did with the message box. Unfortunately, I'm getting a Compile Error: Object Required error when I test it.

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: ElseIf Issues

    Integers are standard variables, not objects, I suspect your error is just that "SET" is not required in bernie's code, which aside from that is a nice solution I do see he said untested, and is based on your original which should have given the same issue when you ran setbills?

    Try removing the word Set from:
    Please Login or Register  to view this content.
    so that they just read:
    Please Login or Register  to view this content.
    Last edited by Arkadi; 02-17-2017 at 03:03 PM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  7. #7
    Registered User
    Join Date
    08-10-2012
    Location
    Derby, England
    MS-Off Ver
    2010 (Work) / 2016 (365) (Home)
    Posts
    14

    Lightbulb Re: ElseIf Issues

    Hi again capurs01,

    Arkadi got you half way there in solving the compile error. Those three lines should read:
    Please Login or Register  to view this content.
    That should now compile and work as designed (I've now done a few simple tests and I think it does what it should).

    Thanks to Arkadi too,

    ---
    Bernieburnham

  8. #8
    Registered User
    Join Date
    02-16-2017
    Location
    Kentucky
    MS-Off Ver
    2013
    Posts
    6

    Re: ElseIf Issues

    Bernie & Arkadi,

    I've updated the macro, but now I'm getting caught up on a run-time error 1004: application-defined or object-defined error.

    I appreciate your help so far.

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: ElseIf Issues

    On what line does the error come up? Are you sure the worksheet names have no erros?

    The ThisWorkbook part of the cell references is not really needed, though it is good practice if other workbooks could be open at the same time. The assumption of course is that the workbook with the macro is the one that contains the sheets LGE Info and Water Bill
    Last edited by Arkadi; 02-21-2017 at 10:11 AM.

  10. #10
    Registered User
    Join Date
    02-16-2017
    Location
    Kentucky
    MS-Off Ver
    2013
    Posts
    6

    Re: ElseIf Issues

    It was occurring on this line

    ElectricBill = ThisWorkbook.Worksheets("LGE Info").Range(E27).Value

    But then I realized I had left out the quotation marks around E27 in my range. However, correcting this issue give me a runtime error 6: overflow.

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: ElseIf Issues

    Hi,

    You have declared your variable as an Integer so it will only accept whole number values between -32768 and 32767. I would suggest you use a Double instead.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  12. #12
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: ElseIf Issues

    Yep integer is not the best option unless the numbers will be relatively small. I usually use Long instead, but as was suggested, use Double if decimals will be present.

  13. #13
    Registered User
    Join Date
    02-16-2017
    Location
    Kentucky
    MS-Off Ver
    2013
    Posts
    6

    Re: ElseIf Issues

    Double was the key. I've attached it to a command button and it works just as I had hoped it would. Thank you bernieburnham, Arkadi, and xlnitwit.

  14. #14
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: ElseIf Issues

    I'm glad we could help capurs01,

    If your issue is resolved please remember to mark the thread as solved?

+ 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] When ElseIf condition is met, the statements under elseif are not happening.
    By excelkann in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-07-2016, 12:40 PM
  2. ElseIf Without If
    By HarryGreenwood in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2015, 05:12 AM
  3. If / ElseIf within For Each?
    By pwells in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-26-2014, 10:35 AM
  4. Code stepping in to elseif statement when elseif is definitely not true
    By DFrank231 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-13-2013, 05:31 PM
  5. If Elseif elseif problem
    By tjbillund in forum Excel General
    Replies: 3
    Last Post: 08-10-2011, 07:02 AM
  6. [SOLVED] Re: IF..Then..ELSE.. ELSEIF
    By rjamison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 08:05 PM
  7. [SOLVED] IF..Then..ELSE.. ELSEIF
    By ole_ in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-20-2005, 11:06 AM

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