title: Spreadsheet not found in the workbook, even if that spreadsheet exists
body: <p>I have this small piece of code:</p>
  <pre><code>NombreLibro = Application.GetOpenFilename()
  Set Libro = Workbooks.Open(NombreLibro, , False)
  
  If Val(Application.Version) &gt; 15 Then  'I tried this but it doesn't solve the problem
      Libro.AutoSaveOn = False
  End If
  
  With Libro
      For i = 1 To .Worksheets.Count
          If .Worksheets(i).CodeName = &quot;Sheet1&quot; Then 'HERE I HAVE THE PROBLEM
              (doing something)
              
          End If
      Next i
  End With
  </code></pre>
  <p>Sometimes the &quot;if&quot; is false even if the workbook actually has the &quot;Sheet1&quot;.
  But if I run this again, adding a stop in the code to check what is going on, then the piece of code works as expected.</p>
  <p>I am downloading the workbooks from a company website and run the code right away.
  I have OneDrive in my computer. So I am wondering if OneDrive is busy uploading the file and then Excel doesn't access it correctly?</p>
  <p>What do you suggest I could try?</p>
  <p>EDIT:</p>
  <p>Following VBasic2008 suggestion (see his/her answer) I changed a little the code, but it kept failing. I then added a debug MsgBox:</p>
  <pre><code>For Each ws In Libro.Worksheets
      MsgBox &quot;CodeName: &quot; &amp; ws.CodeName &amp; vbLf _
          &amp; &quot;Name: &quot; &amp; ws.Name &amp; vbLf &amp; &quot;Libro: &quot; &amp; Libro.Name  'I added this
  
      If ws.CodeName = wsCodeName Then
          wasFound = True
          Exit For ' The worksheet is found, no need to loop anymore.
      End If
  Next ws
  </code></pre>
  <p>Result (shown in the MsgBox):</p>
  <p>CodeName:                     'blank!!!</p>
  <p>Name: SheetName               'correct</p>
  <p>Libro: LibroName              'correct</p>
  <p>Is there a bug in Excel related to CodeNames?</p>
  <p>EDIT2: (WORKAROUND)</p>
  <p>I download the files from a company website, the data on the website is on a table and then a converter downloads it into Excel format.</p>
  <p>I think the downloaded file is not quite Excel format and the CodeName is not filled. When Excel opens it and then the VBA editor is opened, Excel fills out the CodeName with a standard name.</p>
  <p>In my case, as the fresh downloaded files have only one sheet, I can use this workaround:</p>
  <pre><code>For Each ws In Libro.Worksheets
      If ws.CodeName = wsCodeName Or ws.CodeName = &quot;&quot; Then
          wasFound = True
          Exit For 
      End If
  Next ws
  </code></pre>
  <p>This will work the first time the file is processed <code>ws.CodeName = &quot;&quot;</code> and the next times <code>ws.CodeName = wsCodename</code></p>