title: Excel 2013 VBA: Error -2147417848 Method 'Select' of object 'Range' failed
body: <p>I am getting the above error in my Excel code below.  This code is for re-arranging the row height of rows with merged cells.  The code was copied directly from microsoft's support site and works fine if used only once.</p>
  
  <p>In my loop below, it infact works perfectly fine on the 1st six merged cells.</p>
  
  <p>The error only occurs on the line:</p>
  
  <pre><code>NewWorksheet.Range(NewWorksheet.Cells(RowCounter, 5), NewWorksheet.Cells(RowCounter, 6)).Select
  </code></pre>
  
  <p>while executing 24th iteration of the 'For' loop.  Value of ErrorFile_LastRow is 43.  The 1st merged cell is at row 18.  Row 24 has un-merged cells.  I had found a somewhat relevant article from microsoft at  <a href="https://support.microsoft.com/en-us/kb/319832" rel="nofollow">https://support.microsoft.com/en-us/kb/319832</a> and as such added the oXL references in the code below.  Based that same article, I added the NewWorksheet in the offending lines, but none of that has helped.  </p>
  
  <p>The same error occurs at exact same line on the exact same iteration regradless of any of the above changes or what and how many applications are running.  I have even tried to restart my laptop and make sure that only the macro is running an no other office application, but even that does not seem to help.</p>
  
  <p>Infact, if someone can tell me how to do adjust row height of the merged cells without using 'Select', 'ActiveCell', etc. that would be the best as I am trying not to use these kinds of commands so as to both increase the robustness &amp; speed of the code and also to make sure that macro does not stop processing just becuase I am working on another application.</p>
  
  <p>Relevent code snippet (the macro itself is highly complex):-</p>
  
  <pre><code>Dim oXL As Excel.Application
  Dim NewWorkbook As Workbook
  Dim NewWorksheet As Worksheet
  Dim ErrorFile_LastRow As Long
  Dim MergedHeight As Single
  Dim MergedWidth As Single
  Dim PossNewRowHeight As Single
  Dim lngRowCount As Long
  Dim lngColCount As Long
  Dim i As Long
  Dim RowCounter As Long
  Dim ActiveCellWidth As Single
  
  Set oXL = Excel.Application
  
      oXL.Workbooks.Add
      '-------------------------------------------------------------------------------------------------
      '   Create a workbook handle for the new workbook
      '-------------------------------------------------------------------------------------------------
      Set NewWorkbook = oXL.ActiveWorkbook
      '----------------------------------------------------------------------------------------------------------
      '   Use the new workbook handle.
      '----------------------------------------------------------------------------------------------------------
      With NewWorkbook
          '-------------------------------------------------------------------------------------------------
          '   Create a new worksheet handle for the new workbook.
          '-------------------------------------------------------------------------------------------------
          Set NewWorksheet = .Sheets(1)
      End With
  
      '----------------------------------------------------------------------------------------------------------
      '   Use the new worksheet handle.
      '----------------------------------------------------------------------------------------------------------
      With NewWorksheet
          '-------------------------------------------------------------------------------------------------
          '   Capture the last row of data to process.
          '-------------------------------------------------------------------------------------------------
          ErrorFile_LastRow = .Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
          .Range(Cells(1, 1), Cells(ErrorFile_LastRow, 6)).Select
      End With
  
      NewWorksheet.Activate
      Application.PrintCommunication = True
      NewWorksheet.PageSetup.PrintArea = Selection.Address
      '-------------------------------------------------------------------------------------------------
      '   Adjust the row height to fit the data.
      '-------------------------------------------------------------------------------------------------
      For RowCounter = 2 To ErrorFile_LastRow
          If RowCounter &lt;&gt; ErrorFile_LastRow Then
              NewWorksheet.Range(NewWorksheet.Cells(RowCounter, 5), NewWorksheet.Cells(RowCounter, 6)).Select
          Else
              NewWorksheet.Range(Cells(RowCounter, 1), Cells(RowCounter, 6)).Select
          End If
          If ActiveCell.MergeCells Then
              With ActiveCell.MergeArea
                  If .WrapText = True Then
                      lngRowCount = .Rows.Count
                      lngColCount = .Columns.Count
                      MergedHeight = Selection.Height
                      For i = 1 To lngColCount
                          MergedWidth = .Cells(1, i).ColumnWidth + 1 + MergedWidth
                      Next i
                      If MergedHeight &gt; 409.5 Then
                          MergedHeight = 409.5
                      End If
                      If MergedWidth &gt; 409.5 Then
                          MergedHeight = 409.5
                      End If
                      ActiveCellWidth = ActiveCell.ColumnWidth
                      .MergeCells = False
                      .Cells(1).RowHeight = MergedHeight
                      .Cells(1).ColumnWidth = MergedWidth
                      .EntireRow.AutoFit
                      PossNewRowHeight = .Cells(1).RowHeight
                      .MergeCells = True
                      .Cells(1).ColumnWidth = ActiveCellWidth
                      For i = 1 To lngRowCount
                          .Cells(i, 1).RowHeight = PossNewRowHeight / lngRowCount
                      Next i
                  End If
              End With
          End If
      Next RowCounter
  </code></pre>