+ Reply to Thread
Results 1 to 10 of 10

My IF statement does not recognise a counter

  1. #1
    Registered User
    Join Date
    06-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    21

    Angry My IF statement does not recognise a counter

    0 down vote favorite


    I have a simple if statement, that should test the value of a counter called ROW_NO. If ROW_NO > 0, ROW_NO must increase by 1, Else it must be set to 7. At the end of the routing, I display the ROW_NO in a cell, and the value is 7. Therefor, if the IF statement is run, ROW_NO must increase to 8!?! but it does not do it, and ROW_NO is not reset anywhere in the macro. Any ideas?
    Please Login or Register  to view this content.

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

    Re: My IF statement does not recognise a counter

    Macro1 and DoWork are independent macros. Variables defined in one are not accessible to the other. Thus, ROW_NO and FIL_NM1 referenced in DoWork are not associated with the variables ROW_NO and FIL_NM1 defined in Macro1. Thus, for each call to DoWork in the Macro1 loop, those DoWork variables are re-created and initialized to zero then destroyed when DoWork is exited.

    If you want to pass data between procedures you need something like:
    Please Login or Register  to view this content.
    If you had put Option Explicit at the top of your module, before any procedures, you would have gotten an error in the DoWork because you tried to use variables that were undefined in that procedure.
    Last edited by protonLeah; 06-07-2013 at 02:17 AM.

  3. #3
    Registered User
    Join Date
    06-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    21

    Re: My IF statement does not recognise a counter

    hi ProtonLEAH

    Thanx for the help. I am fairly new at this, and although I did Cobol programming 20 Years ago, my logic is going for a loop. It will take some time for me to get to know VBA.

    I have made the changes as suggested, but get an error Compile Error: Syntax Error on the line that contains the DoWork(START_ROW,STRT_NM1) instruction. It looks OK to me, but then again I am a novice.

    Any suggestions?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: My IF statement does not recognise a counter

    Remove the brackets around START_ROW,STRT_NM1, or use Call.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    06-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    21

    Re: My IF statement does not recognise a counter

    Thanx again. It works. Another few little probs came up, but I want to see if I can solve it before asking again.

    Have a great day!:

  6. #6
    Registered User
    Join Date
    06-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    21

    Re: My IF statement does not recognise a counter

    Hi ProtonLEAH

    Sorry to bug YOU. My 3 counters (START_ROW, STRT_NM1, COUNTER) is incremented at the completion of each loop. I display them right at the end of the macro, and can therefor verify the increments. Yet, in the third line after the IF statement, on the line that reads Range("D" & STRT_NM1).Select it keeps selecting the same cell, and not incrementing it from D2 to D3 to D4. Is there another way I can select a cell using a variable like STRT_NM1?

    André

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

    Re: My IF statement does not recognise a counter

    Try this:
    Please Login or Register  to view this content.
    Ben Van Johnson

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: My IF statement does not recognise a counter

    please see forum rules on cross-posting and provide links
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  9. #9
    Registered User
    Join Date
    06-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    21

    Re: My IF statement does not recognise a counter

    Hi
    My apologies. I'll make sure it does not happen again. I just get so excited learning all this stuff at my age (53) that I want answers a.s.a.p. But I will respect your rules from now on! Thanx for leading me on the right track

    Have a great day

    André

  10. #10
    Registered User
    Join Date
    06-06-2013
    Location
    South Africa
    MS-Off Ver
    Excel 2010 and Excel 2013
    Posts
    21

    Red face Re: My IF statement does not recognise a counter

    Hi Everyone

    A Sincere THANK YOU to all who have assisted in getting my problem out of the way. It is really appreciated. You guys are so cool!

    Regards

    André Roux

+ 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