Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VBA - "End if without Block If Error"
#3
SaptaZapta Wrote:Indentation is your friend.

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

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?
Reply


Messages In This Thread
VBA - "End if without Block If Error" - by Yugidude - 2013-04-16, 04:58 PM
VBA - "End if without Block If Error" - by Locked - 2013-04-16, 05:00 PM
VBA - "End if without Block If Error" - by FenixR - 2013-04-16, 05:04 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)