title: Why am I getting 'subscript out of range' error for my macro?
body: <p>Previously, I was getting a win32 error 'open method of excel workbooks failed'. But now, I'm getting an error in the excel macro I'm trying to run via python, 'subscript out of range'.</p>
  <p><a href="https://i.stack.imgur.com/ro5tU.png" rel="nofollow noreferrer">Screenshot of error</a></p>
  <p>When I debug it, it outlines this line of code:</p>
  <p>Sheets(&quot;2 RawData&quot;).Select</p>
  <p>I know that the error is supposed to mean that there isn't a sheet with that name, but when I checked, it was there:</p>
  <p><a href="https://i.stack.imgur.com/z3RfG.png" rel="nofollow noreferrer">All sheets in excel file</a></p>
  <p>And this is a screenshot of the errors from python:</p>
  <p><a href="https://i.stack.imgur.com/cdfbx.png" rel="nofollow noreferrer">Python errors</a></p>
  <p>This is the code I'm trying to run to parse an excel document via macro:</p>
  <pre><code>    if os.path.exists(self.excel_parser_location):
          # print &quot;Opening Telematics_Messages_Parser.xlsm in Excel&quot;
          xl = client.Dispatch(&quot;Excel.Application&quot;)
          xl.Application.visible = False
          wb = xl.Workbooks.Open(os.path.abspath(self.excel_parser_location), ReadOnly=True)
          xl.Application.Run('DoThisFirst')
          xl.DisplayAlerts = False
          wb.DoNotPromptForConvert = True
          wb.CheckCompatibility = False
          # xl.Application.Run('SheetKiller')
          xl.Application.Run('CleanUp')
          xl.DisplayAlerts = False
          wb.DoNotPromptForConvert = True
          wb.CheckCompatibility = False
          # If the file already exists
          if os.path.exists(save_path):
              # Remove the older save
              os.remove(save_path)
          wb.SaveAs(save_path)
          # print &quot;Saving Parsed_Messages.xlsm&quot;
          wb.Close(True)
          del xl
  </code></pre>
  <p>Edit: I called this method before the method to run the macros:</p>
  <pre><code>if os.path.exists(csv_path):
      #data = pd.read_csv(csv_path, error_bad_lines=False)
      data = pd.read_csv(csv_path, on_bad_lines='skip')
      book = openpyxl.load_workbook(self.excel_parser_location,keep_vba=True)
      writer = pd.ExcelWriter(self.excel_parser_location)
      writer.book = book
      data.to_excel(writer, sheet_name='2 RawData', index=False)
      # print 'Writing new data'
      book.remove(book['2 RawData'])
      # print 'Removing blank sheet'
      book_sheet = book['2 RawData1']
      book_sheet.title = '2 RawData'
      # print 'Renaming sheet'
      writer.save()
      writer.close()
      # print 'Saving Telematics_Messages_Parser.xlsm'
      self.run_parsing_macro(sn)
      os.remove(csv_path)
      return True
  </code></pre>
  <p>It's supposed to copy data to the excel sheet from a downloaded csv file.</p>