Rx_
Nothing In Moderation
- Local time
 - Today, 08:40
 
- Joined
 - Oct 22, 2009
 
- Messages
 - 2,803
 
Failure at line 510 - the commented line just before it works fine!
So does the commented line below BUT only the first time, trying to run a second time generates an object variable error.
The intMaxHeaderColCount is Good.
While I can hard code the offset value (rather than keep it dynamic for a different record set) and it never fails on the 2nd, 3rd, 4th... time
 
Is there any other way to dynamically select the range based on the number of columns returned from a recordset?
 
" 91 Object variable in block not set"
	
	
	
		
 So does the commented line below BUT only the first time, trying to run a second time generates an object variable error.
The intMaxHeaderColCount is Good.
While I can hard code the offset value (rather than keep it dynamic for a different record set) and it never fails on the 2nd, 3rd, 4th... time
Is there any other way to dynamically select the range based on the number of columns returned from a recordset?
" 91 Object variable in block not set"
		Code:
	
	
	260     If ObjXL Is Nothing Then            ' just in case Excel was already running in memory
270       Set ObjXL = New excel.Application
280       ObjXL.EnableEvents = False
290     Else
300       excel.Application.Quit
310       Set ObjXL = New excel.Application
320       objExcel.EnableEvents = False
330     End If
340       Set rsData = frm.RecordsetClone
350     ObjXL.Workbooks.Add
360       intWorksheetNum = 1                                   ' Refer to sheet by its number in code
370       ObjXL.Visible = True                                  ' *********** Excel visible True for testing *******
 
380       intRowPos = 6                                       ' Row data starts in Excel (keep everything relative from here)
                                                  ' Always start at 2 or higher - the title will appear in the row above this
                                                      ' *******   Recordset Count ****************
390       rsData.MoveLast                                         ' force to last for accruate record count
400         intMaxRecordCount = rsData.RecordCount              ' how many records
410       rsData.MoveFirst                                        ' reset pointer for poste
420       ObjXL.Workbooks(1).Worksheets(1).Range("A" & intRowPos).Select                     ' adjusts to Row data starts
430       ObjXL.Application.DisplayAlerts = False                 ' Turn off Are You Shure? questions - turn back on at end
 
440         intMaxheaderColCount = rsData.Fields.count - 1
450     For intHeaderColCount = 0 To intMaxheaderColCount
460     If Left(rsData.Fields(intMaxheaderColCount).Name, 3) <> "xxx" Then  ' Future use - adding xxx title in queries for fields to exclude
470           ObjXL.Worksheets(intWorksheetNum).Cells(intRowPos - 1, intHeaderColCount + 1) = rsData.Fields(intHeaderColCount).Name    ' Relative to intRowPos
480     End If
490       Next intHeaderColCount
          'Debug.Print "Columns created count is " & intHeaderColCount
500       ObjXL.Workbooks(1).Worksheets(1).Rows((intRowPos - 1) & ":" & (intRowPos - 1)).Select         ' Selection for Bold header column (can make 2 Rows if needed)
        'ObjXL.Range("A" & intRowPos - 1).Select        ' Select the first column at our starting Row for the header
        'ObjXL.Range("A" & intRowPos - 1 & ":AE" & intRowPos - 1).Select  ' Put Title at one row less than where data starts
            ' ****************************** this next line will error the Second Time Through even if all forms are closed back to switchboard ******
510     ObjXL.Workbooks(1).Worksheets(1).Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(0, intMaxheaderColCount)).Select
        'ObjXL.Range(ActiveCell.Offset(0, intMaxheaderColCount)).Select
        '   in debug window   ? objxl.Activecell does return correct value in the active cell
520     Call Send2ExcelRowHeaderFormat(ObjXL)               ' Format the data row heading  bold, outline, as a Select
 
530      With ObjXL.Workbooks(1).Worksheets(1)                                             ' Complete the data row heading Format as Rows & Cells
540        .Rows((intRowPos - 1) & ":" & (intRowPos - 1)).RowHeight = 25.5
550        .Rows((intRowPos - 1) & ":" & (intRowPos - 1)).Select                                    ' Selection for Bold header column (can make 2 if needed)
560        .Cells.EntireColumn.AutoFit
570      End With