+ Reply to Thread
Results 1 to 4 of 4

Alternative Code - Nested Ifs

  1. #1
    Registered User
    Join Date
    02-10-2008
    Posts
    8

    Question Alternative Code - Nested Ifs

    Hello, I've been working on a affine cipher encryptor/decryptor, but am having trouble keeping track of my "if" statements. Here is the part of my code that doesn't work:

    If Modulo(Int((26 / a)), 26) <> 1 Then k = Modulo(a, 26)
    If Modulo(Int((a / k)), a) <> 1 Then l = Modulo(k, a)
    If Modulo(Int((k / l)), k) <> 1 Then m = Modulo(l, k)
    If Modulo(Int((l / m)), l) <> 1 Then MsgBox "a and 26 are not coprime"
    End If
    End If
    End If
    End If

    I know that the problem is in this part somewhere, since I tried running everything before adding this, and it worked. Anyways, the message that appears is "End If without block If", so it has something to do with my "if" statements being ended improperly or missing some additional terms. If anyone can help me with this, it would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    02-10-2008
    Posts
    8

    Alternative Code - Nested Ifs

    I also tried using nested ifs, so if this is easier to fix than the other code, feel free to change this instead.

    IF(Modulo(Int((26 / a)), 26) <> 1, k = Modulo(a, 26), IF(Modulo(Int((a / k)), a) <> 1, l = Modulo(k, a), IF(Modulo(Int((k / l)), k), m = Modulo(l, k), IF(Modulo(Int((l / m)), l), MsgBox "a and 26 are not coprime"))))

    However, after the first condition, Modulo(Int((26 / a)), 26) <> 1, it says there is a compile error, it expects a parenthesis; if I put a parenthesis after <>1, then it says another compile error exists; it "expected Goto or Then". Once again, any help is greatly appreciated!

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello gizoku00,

    In VBA the If Then construct does not require the End If statement when Then is immediately followed by a statement on the same line. For example:
    Please Login or Register  to view this content.
    However, it would if you wrote the code this way...
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Just remove the End If statements.

    Sincerely,
    Leith Ross

  4. #4
    Registered User
    Join Date
    02-10-2008
    Posts
    8

    Smile Thanks!

    Thanks so much Leith! I can't believe that was it! And to think I just remember my teacher saying the exact same thing...It's just so funny because the rest of my code is so much more complex.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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