+ Reply to Thread
Results 1 to 11 of 11

If ElseIf Else Statement not working

  1. #1
    Registered User
    Join Date
    04-30-2014
    Location
    england
    MS-Off Ver
    Microsoft Excel 365
    Posts
    67

    If ElseIf Else Statement not working

    Hi Gurus,

    I have a userform whereby there is a drop down list for user to chose the data to be save at which outlet.

    Currently I have 3 outlet on hand therefore the data will be store according to the option selected.

    However I notice my statement doesn't work correctly for example I chose outlet "ABC" the data should store in ABC outlet but the data end up in GHI outlet
    On top of that if I chose DEF outlet the data end up go to incorrect row.

    I'm getting very confused with the if else statement code. You may refer the code here.

    Please Login or Register  to view this content.
    To make you easy understand what problem I'm Facing, I attached the duplicate file together.

    Thank you.

    Report-Test.xlsm

  2. #2
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: If ElseIf Else Statement not working

    u have wrongly define the Last row

    Please Login or Register  to view this content.
    that is what its going in GHI
    Thanks - Naveed
    -----------------------------
    If the suggestion helps you, then Click * to Add Reputation
    To Attach File: Go Advanced>>Manage Attachments>>Add Files (In Top Right Corner)>>SelectFiles>>.........Locate Your File(s)>>Upload Files>>Done (In Bottom Right)
    1. Use [code] code tags [\code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

  3. #3
    Valued Forum Contributor Naveed Raza's Avatar
    Join Date
    11-04-2012
    Location
    India, Hyderabad
    MS-Off Ver
    Excel, Access 2007/2010
    Posts
    1,338

    Re: If ElseIf Else Statement not working

    Just replace your Row finding code with below one

    In ABC
    Please Login or Register  to view this content.
    In DEF
    Please Login or Register  to view this content.
    In GHI

    Please Login or Register  to view this content.

  4. #4
    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
    48,440

    Re: If ElseIf Else Statement not working

    The problem is this:
    Please Login or Register  to view this content.
    That is the equivalent of saying go to the bottom of column E and press the End key and the Up Arrow key.

    Unfortunately, that takes you to the area related to outlet GHI, regardless of what outlet you select in the drop down combo box.

    You need to modify the way that you locate the relevant row, something like the code below:

    Please Login or Register  to view this content.

    See the attached example.

    Regards, TMS
    Attached Files Attached Files
    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


  5. #5
    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
    48,440

    Re: If ElseIf Else Statement not working

    The form specifies a Week Number so, presumably, it's not just a case of going up to the next available blank row for an outlet, otherwise there would be little point specifying a week number and listing the weeks down column A

    Hence the reason that I have used:
    Please Login or Register  to view this content.
    and, later:

    Please Login or Register  to view this content.

    Using separate row numbers for each outlet means the code is, of necessity, repetitive and redundant. I haven't changed it, but, instead of hard coding the store in the MATCH, you could use the input store. You could then replace the nr3, nr4, nr5 and nr6 and remove two thirds of the code.


    Regards, TMS

  6. #6
    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
    48,440

    Re: If ElseIf Else Statement not working

    Money where mouth is ...

    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    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
    48,440

    Re: If ElseIf Else Statement not working

    Thank you for your kind comments in your Private Messages.

    If these suggestions have resolved your issue, please mark this thread as solved.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  8. #8
    Registered User
    Join Date
    04-30-2014
    Location
    england
    MS-Off Ver
    Microsoft Excel 365
    Posts
    67

    Re: If ElseIf Else Statement not working

    Hi TMS,

    Yes your solution do solve partly of my problem, however due to the form have interrelated problem, I hope you don't mind to take a look at this thread

    http://www.excelforum.com/excel-prog...-userform.html

    this code work great with specific of week enter in the column
    Please Login or Register  to view this content.
    However, my current setup required the week column value that Enter by the User therefore, the code you provide couldn't work.

    Please advise if there is another round of code that able to solve this problem?

    Thank you so much!

    Quote Originally Posted by TMS View Post
    Thank you for your kind comments in your Private Messages.

    If these suggestions have resolved your issue, please mark this thread as solved.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  9. #9
    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
    48,440

    Re: If ElseIf Else Statement not working

    Please don't quote entire posts. It is unnecessary and just clutters the thread.

    However, my current setup required the week column value that Enter by the User therefore, the code you provide couldn't work.
    The code, as provided (by me) in both versions does work. It uses the Week Number entered into the box in the User Form to determine which rows to update. Other responses just addressed the fact that using End(xlUp) would not necessarily get you to where you wanted to be, and provided workarounds.

    How else would the user enter a Week Number? And what is the purpose of having the box on the User Form if you do not use it to match the week number in the worksheet?

    Have you checked and tested the updated workbooks that I uploaded? Note that the second one is about 3kb smaller from removing the redundant code.

    Personally, I probably wouldn't have separate areas for each Outlet. I'd just have an extra column and write the Outlet Code there. Similarly, use the Week Number entered in the User Form in a separate column instead of pre-coding the entries. Then you can just sort and filter the data by Outlet and Week Number.

    But, in practical terms, this isn't really the place to design your workbook/worksheet/application on the fly.

    Regards, TMS

  10. #10
    Registered User
    Join Date
    04-30-2014
    Location
    england
    MS-Off Ver
    Microsoft Excel 365
    Posts
    67

    Re: If ElseIf Else Statement not working

    I fixed the date problem and amend the whole file according to your suggestion, therefore I have this error code in debug mode

    Please Login or Register  to view this content.
    You may refer attached file to see the changes.

    Thank you so much.

    Edit: I notice if my Week column empty which causing this problem, may I know how to solve it?

    The code:

    Please Login or Register  to view this content.
    Report-Test-v1.xlsm
    Last edited by lazyserv; 05-27-2015 at 01:26 PM. Reason: added info

  11. #11
    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
    48,440

    Re: If ElseIf Else Statement not working

    OK, that wasn't quite what I had in mind My intention/expectation is that there are NO ENTRIES at all for Outlet and Week Number, and they are completed from the User Form.

    Please see the attached updated example. I have cleared the "raw data" input sheet. Just complete the form for each Outlet/Week and Save the data.

    Regards, TMS
    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)

Similar Threads

  1. Adding CountIf Statement within If/ElseIf Statement
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-17-2014, 05:09 AM
  2. [SOLVED] If...Then...ElseIf statement
    By BrownBoy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-21-2014, 05:19 PM
  3. 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
  4. If then elseif statement not working.........
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-11-2011, 02:27 PM
  5. If then elseif statement
    By Mac10 in forum Excel General
    Replies: 3
    Last Post: 10-02-2010, 09:29 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