title: How to fix 'Run-time error 91 : "Object variable or with block variable not set " using vba Excel?
body: <p>I have a small section of code to automate a logon to a specific website that sometimes works and sometimes gives the runtime error 91, saying object variable or with block variable not set. How do I fix this problem?</p>
  
  <pre><code>Sub AutoLogin()
      Const Url = "https://www.tymetrix360.com/Common/Pages/LoginPage.aspx? 
  ReturnUrl=%2f"
  
      Dim userName As String, password As String, LoginData As Worksheet
      Set LoginData = ThisWorkbook.Worksheets("Sheet1")
      userName = LoginData.Cells(1, "A").Value
      password = LoginData.Cells(2, "A").Value
  
      Dim ie As Object
  
      Set ie = CreateObject("InternetExplorer.Application")
  
  
          ie.navigate Url
          ie.Visible = True
  
          ieBusy ie
  
          ie.Document.all.Item("LoginTextBox").Value = userName
          ie.Document.all.Item("PasswordTextBox").Value = password
  
          ie.Document.all.Item("LoginButton").Click
  
  End Sub
  
  Sub ieBusy(ie As Object)
      Do While ie.Busy
          DoEvents
      Loop
  End Sub
  </code></pre>