Results 1 to 8 of 8
  1. Neutron Male
    IGN: Yugidude3536
    Server: Bera
    Level: 177
    Job: Hero
    Guild: Prospect
    Alliance: ComeAtUsBro
    Michigan

    Default VBA - "End if without Block If Error"


    Hi,

    So I am in the middle of writing a fairly simply VBA program, but I keep getting an error that I cant get rid of.

    Code:
     Sub CheckWords()
    Selection.HomeKey Unit:=wdStory
    Dim FirstRound, InQuotes As Boolean
    Dim WrongWordCount As Integer
    Dim WordCount As Integer
    Dim TestingWord As String
    WrongWordCount = 0
    UnusableWords = Array("i", "me", "you", "us", "we", "my", "mine", "our", "your", "can't", "won't", "should'nt", "you're")
    FirstWord = True
    InQuotes = False
    Do Until Selection.Bookmarks.Exists("\EndOfDoc") = True
      If (FirstWord = False) Then Selection.MoveRight (2)
      Selection.MoveEndUntil Cset:=" ", Count:=10
      TestingWord = NoPunct(Selection)
      InQuotes = QuoteState(TestingWord, InQuotes)
      If InQuotes = False Then
      For WordCount = 0 To UBound(UnusableWords)
      If TestingWord = UnusableWords(WordCount) Then
      Selection.Range.HighlightColorIndex = wdYellow
      WrongWordCount = WrongWordCount + 1
      End If
      End If
      Next WordCount
      FirstWord = False
      Loop
    MsgBox ("The Program has finished and found " & WrongWordCount & " unusable words.")
    Returns a "Compile Error: End If without Block If" (It highlights the 2nd "End If" when the error comes up)

    this started happening after I added the quote checker. When i comment out those two lines:

    Code:
     Sub CheckWords()
    Selection.HomeKey Unit:=wdStory
    Dim FirstRound, InQuotes As Boolean
    Dim WrongWordCount As Integer
    Dim WordCount As Integer
    Dim TestingWord As String
    WrongWordCount = 0
    UnusableWords = Array("i", "me", "you", "us", "we", "my", "mine", "our", "your", "can't", "won't", "should'nt", "you're")
    FirstWord = True
    InQuotes = False
    Do Until Selection.Bookmarks.Exists("\EndOfDoc") = True
      If (FirstWord = False) Then Selection.MoveRight (2)
      Selection.MoveEndUntil Cset:=" ", Count:=10
      TestingWord = NoPunct(Selection)
      InQuotes = QuoteState(TestingWord, InQuotes)
     ' If InQuotes = False Then
      For WordCount = 0 To UBound(UnusableWords)
      If TestingWord = UnusableWords(WordCount) Then
      Selection.Range.HighlightColorIndex = wdYellow
      WrongWordCount = WrongWordCount + 1
     ' End If
      End If
      Next WordCount
      FirstWord = False
      Loop
    MsgBox ("The Program has finished and found " & WrongWordCount & " unusable words.")
    It Works fine.

    Basically, my program is supposed to check an essay to make sure no 1st person/2nd person/conjunctions exist. if they do, highlight them. However, if a word is in quotes, dont check it. When i try "boxing" the middle of the program, it wont run. As soon as I take it out, it's fine. I get the feeling its some kind of stupid newb mistake.

    Any help is appreciated,
    Dan

  2. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: VBA - "End if without Block If Error"


    Indentation is your friend.

    Code:
     Sub CheckWords()
    Selection.HomeKey Unit:=wdStory
    Dim FirstRound, InQuotes As Boolean
    Dim WrongWordCount As Integer
    Dim WordCount As Integer
    Dim TestingWord As String
    WrongWordCount = 0
    UnusableWords = Array("i", "me", "you", "us", "we", "my", "mine", "our", "your", "can't", "won't", "should'nt", "you're")
    FirstWord = True
    InQuotes = False
    Do Until Selection.Bookmarks.Exists("\EndOfDoc") = True
      If (FirstWord = False) Then Selection.MoveRight (2)
        Selection.MoveEndUntil Cset:=" ", Count:=10
        TestingWord = NoPunct(Selection)
        InQuotes = QuoteState(TestingWord, InQuotes)
        If InQuotes = False Then
          For WordCount = 0 To UBound(UnusableWords)
            If TestingWord = UnusableWords(WordCount) Then
              Selection.Range.HighlightColorIndex = wdYellow
              WrongWordCount = WrongWordCount + 1
            End If    
        End If    This belongs to an If started before the For.  Bad nesting.
          Next WordCount
        FirstWord = False
    Loop
    MsgBox ("The Program has finished and found " & WrongWordCount & " unusable words.")

    Specifically, your problem is that inside the loop between "For Wordcount" and "Next Wordcount" there is one If but two End If.

  3. Neutron Male
    IGN: Yugidude3536
    Server: Bera
    Level: 177
    Job: Hero
    Guild: Prospect
    Alliance: ComeAtUsBro
    Michigan

    Default Re: VBA - "End if without Block If Error"


    I'm sorry, i dont understand.

    Code:
     Sub CheckWords()
    Selection.HomeKey Unit:=wdStory
    Dim FirstRound, InQuotes As Boolean
    Dim WrongWordCount As Integer
    Dim WordCount As Integer
    Dim TestingWord As String
    WrongWordCount = 0
    UnusableWords = Array("i", "me", "you", "us", "we", "my", "mine", "our", "your", "can't", "won't", "should'nt", "you're")
    FirstWord = True
    InQuotes = False
    Do Until Selection.Bookmarks.Exists("\EndOfDoc") = True
      If (FirstWord = False) Then Selection.MoveRight (2)
      Selection.MoveEndUntil Cset:=" ", Count:=10
      TestingWord = NoPunct(Selection)
      InQuotes = QuoteState(TestingWord, InQuotes)
      If InQuotes = False Then                                    [IF #1]
      For WordCount = 0 To UBound(UnusableWords)
      If TestingWord = UnusableWords(WordCount) Then           [IF #2]
      Selection.Range.HighlightColorIndex = wdYellow
      WrongWordCount = WrongWordCount + 1
      End If                                                 [END IF #1]
      End If                                                 [END IF #2]
      Next WordCount
      FirstWord = False
      Loop
    MsgBox ("The Program has finished and found " & WrongWordCount & " unusable words.")
    According to my count, I have two of each, excluding the " If (FirstWord = False) Then Selection.MoveRight (2)" But I dont beleive that needs an End If (Correct me if i'm wrong). Did I miss an "If" or an "End If" in my count?

    Also, I was unaware indentation mattered. Where should I indent that I have not?

    EDIT: just saw your Indentation mock-up of my code. You said that the 2nd "End If" is refering to code before the "For" Loop and that that was bad.. That is what I want. I only want the "For" Loop to execute if "InQuotes = False". Is thier a better way to do this?

  4. Default Re: VBA - "End if without Block If Error"


    Unindented code is a mess.

    "If Then" does need an "End If" by the way.

  5. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: VBA - "End if without Block If Error"


    The count of IFs and END IFs is correct, but their nesting inside the inner loop is wrong.

    Indentation doesn't matter to the compiler, but it makes it a lot easier for humans to see what's going on. I have added the indented code to my post above. You can easily see that you're opening the IF before the FOR loop, but closing it before the NEXT. Not allowed to do that.

  6. Certified Pimento Bi Male
    IGN: xxxxFenixR
    Server: Bera
    Level: Mix
    Job: Severals
    Guild: None
    Alliance: Nada
    Farm: Wut?
    venezuela

    Default Re: VBA - "End if without Block If Error"


    Code:
     Sub CheckWords()
    Selection.HomeKey Unit:=wdStory
    Dim FirstRound, InQuotes As Boolean
    Dim WrongWordCount As Integer
    Dim WordCount As Integer
    Dim TestingWord As String
    WrongWordCount = 0
    UnusableWords = Array("i", "me", "you", "us", "we", "my", "mine", "our", "your", "can't", "won't", "should'nt", "you're")
    FirstWord = True
    InQuotes = False
    Do Until Selection.Bookmarks.Exists("\EndOfDoc") = True
      If (FirstWord = False) Then Selection.MoveRight (2) [IF #3]
      Selection.MoveEndUntil Cset:=" ", Count:=10
      TestingWord = NoPunct(Selection)
      InQuotes = QuoteState(TestingWord, InQuotes)
      If InQuotes = False Then                                    [IF #1]
      For WordCount = 0 To UBound(UnusableWords)
      If TestingWord = UnusableWords(WordCount) Then           [IF #2]
      Selection.Range.HighlightColorIndex = wdYellow
      WrongWordCount = WrongWordCount + 1
      End If                                                 [END IF #1]
      End If                                                 [END IF #2]
      Next WordCount
      FirstWord = False
      Loop
    MsgBox ("The Program has finished and found " & WrongWordCount & " unusable words.")
    Also you should really add an indentation level after every decision statement (If, For, etc) and reduce it after that Decision is done with.

  7. Neutron Male
    IGN: Yugidude3536
    Server: Bera
    Level: 177
    Job: Hero
    Guild: Prospect
    Alliance: ComeAtUsBro
    Michigan

    Default Re: VBA - "End if without Block If Error"


    Ah. That makes sence. Thanks so much for your help.

    Will Do. I learned my lesson this time...

  8. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: VBA - "End if without Block If Error"


    If you only want the FOR to execute if the condition is true, you code it thus:
    Code:
    IF (condition) THEN
      FOR counter ...
        ....
      NEXT counter
    END IF
    You need to have the entire loop inside the IF - END IF block.

  9.  

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
  •