VBA returning different results than expected IF GOTO ELSE

Sethomas5

Board Regular
Joined
Oct 5, 2015
Messages
204
Hello and thanks in advance!
Running Excel 2013 on Windows 7.

I have a formula where I'm trying to fill bookmarks in a Word Document. I'm trying to get it to check if I1 = certain text, then goto Line1 else goto Line2 and so on. But, even though I1 in my sheet DOES = "Humanities", my code is skipping to Line4 and giving me the msgbox

Here is my code
Code:
Sub RunThisOne()
Dim objWord As Object
Dim ws As Worksheet
Dim headerstring As String

    Set ws = ThisWorkbook.Sheets("EnglishCreativeWr")

    Set objWord = CreateObject("Word.Application")

    objWord.Visible = True

    objWord.Documents.Open "C:\Users\thomassa\Desktop\thistemplate.dotm" ' change as required

    With objWord.ActiveDocument

    
     .Bookmarks("EKU_Major").Range.Text = ws.Name

    If ("I1") = "Heritage" Then GoTo Line1 Else GoTo Line2
    
Line1:
    .Bookmarks("Heritage_Class1").Range.Text = ws.Range("I6").Value
    .Bookmarks("Heritage_Class2").Range.Text = ws.Range("I7").Value
    .Bookmarks("Heritage_Class3").Range.Text = ws.Range("I8").Value
    
Line2:
    If ("I1") = "Humanities" Then GoTo Line3 Else GoTo Line4
    
Line3:
    .Bookmarks("Humanities_Class1").Range.Text = ws.Range("I6").Value
   
Line4:
    MsgBox "Doesn't exist, sorry"
    End With
 
    Set objWord = Nothing

End Sub

If anybody could tell me WHY this is happening that would be great.

Is it because I used a macro to get Humanities to appear in I1, if so, how can I get around this?
 
Did you mean

Code:
If [COLOR="#FF0000"]ws.Range[/COLOR]("I1").Value = "Heritage" Then ...

The literal string "I1" is never equal to the string "Heritage"
 
Upvote 0
You're welcome.

Be aware that VBA string comparisons are case sensitive by default.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top