title: Access vba excel Range
body: <p>I am using Access to loop through a data table and generate an Excel Workbook (with three sheets) for each row in the database table.  All works very well until I introduce code using "Range" to hide some columns and rows.
  The code will work successfully on the first row but then fail. It will also fail if we run the code again.  If we quit Access and then rerun the first row is again successful.</p>
  
  <pre><code>NewFileName = "C:\Paul2016Puzzle\TestNewName" + "Project" + Str(iteration)
              'MsgBox NewFileName
  
          Set XL = New Excel.Application
          Set WB = XL.Workbooks.Open(NewFileName)
          WB.Activate
  
          Set wks = WB.Worksheets(2)
          XL.ScreenUpdating = False
          XL.DisplayAlerts = False
  
  
          wks.Select
          WB.Sheets(2).Activate
  
  
          StrExcel = Chr(65 + WorkingColumns + 1)
  
           StrExcel = StrExcel + ":" + StrExcel
           MsgBox StrExcel
  
             WB.Sheets("Sheet 2").Select
             WB.Sheets("Sheet 2").Range(StrExcel).Select
             WB.Sheets("Sheet 2").Activate
  
  
  
                  wks.Range(StrExcel).Activate
                  wks.Columns(StrExcel).Select
                  wks.Range(StrExcel).Select
                  ActiveSheet.Range(Selection, Selection.End(xlToRight)).Select
                  Selection.EntireColumn.Hidden = True
  
                  Rows("12:12").Select
                  ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
                  Selection.EntireRow.Hidden = True
  
  
  
  
          wks.Cells(1, 1).ColumnWidth = 30 '(Set column width)
          For i = 2 To WorkingColumns + 1
          wks.Cells(1, i).ColumnWidth = 15
          Next i
  </code></pre>