+ Reply to Thread
Results 1 to 10 of 10

Variant defined value gets erased after elseif statement

  1. #1
    Registered User
    Join Date
    02-14-2019
    Location
    Argentina
    MS-Off Ver
    2018
    Posts
    5

    Variant defined value gets erased after elseif statement

    Hello, im trying to make a macro that will show a row resume on another worksheet vertically wich is the part that works. But i tried to expand it to be able to change te original row from the resume and thats where it gets sticky. The msgbox displaying iRow shows the right value the first time, but when trying to use the second part of the macro after elseif the variable is set to empty!
    I appreciate any help!
    Please Login or Register  to view this content.
    Last edited by alenieto97; 02-14-2019 at 09:05 AM.

  2. #2
    Forum Expert
    Join Date
    08-13-2008
    Location
    UK
    MS-Off Ver
    2010 primarily
    Posts
    2,001

    Re: Variant defined value gets erased after elseif statement

    Please use code tags when posting code.

    If you need the variable to maintain its value between each trigger of the event, you should declare it as Static, not just Dim.
    Rory
    If a job's worth doing, it's worth doing right
    over your shoulder by your boss

  3. #3
    Registered User
    Join Date
    02-14-2019
    Location
    Argentina
    MS-Off Ver
    2018
    Posts
    5

    Re: Variant defined value gets erased after elseif statement

    I had to change my code because of a small mistake and i keep having the same problem, Now it looks like this

    Please Login or Register  to view this content.
    Again, The variable iRow is empty when i need it. Appreciate help a lot.
    Also i dont know how to label the thread as solved.

  4. #4
    Forum Expert
    Join Date
    08-13-2008
    Location
    UK
    MS-Off Ver
    2010 primarily
    Posts
    2,001

    Re: Variant defined value gets erased after elseif statement

    You're now using two separate routines, so you'll have to declare the variable at module level, like this:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-14-2019
    Location
    Argentina
    MS-Off Ver
    2018
    Posts
    5

    Re: Variant defined value gets erased after elseif statement

    I still get the same problem, tried with dim and static at module level but it gets erased

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    08-13-2008
    Location
    UK
    MS-Off Ver
    2010 primarily
    Posts
    2,001

    Re: Variant defined value gets erased after elseif statement

    That's not possible with that code. If you see iRow as 0, it wasn't set in the first place, or you have some other code interfering.

  7. #7
    Registered User
    Join Date
    02-14-2019
    Location
    Argentina
    MS-Off Ver
    2018
    Posts
    5

    Re: Variant defined value gets erased after elseif statement

    I know it was set in the first place because of the first msgbox, i dont see it as 0 but as empty when i place my mouse over the word irow on Activesheet.cells(irow, orow) and when the second msgbox pops. All other code i have is

    Please Login or Register  to view this content.
    and on other module

    Please Login or Register  to view this content.
    Isnt it weird?

  8. #8
    Forum Expert
    Join Date
    08-13-2008
    Location
    UK
    MS-Off Ver
    2010 primarily
    Posts
    2,001

    Re: Variant defined value gets erased after elseif statement

    It can't be Empty because it's declared as a Long, not a Variant. It can only be a number.

    Also, there is no point enabling events in the Workbook_Open. If they aren't enabled, that code won't run anyway.

  9. #9
    Registered User
    Join Date
    02-14-2019
    Location
    Argentina
    MS-Off Ver
    2018
    Posts
    5

    Re: Variant defined value gets erased after elseif statement

    Thanks rory i solved it, only had to add Options Explicit to declare the variable! now how i declare the thread as solved? very grateful for your help!

  10. #10
    Forum Expert
    Join Date
    08-13-2008
    Location
    UK
    MS-Off Ver
    2010 primarily
    Posts
    2,001

    Re: Variant defined value gets erased after elseif statement

    Click the Thread Tools dropdown at the top of the page, then mark Solved.

+ 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