Hi
I have a problem trying to perform the following:
1. add a conditional format at end in excel if Collum 1 > than colum 3 then mark red.
2. make heading bold and centered.
3.do not start excel again if you rerun query just add a new sheet if excel already open.
thanks for your help. Code as follow:
Function SendToExcelDataChecks(strTableUsing As String, strTitle As String)
On Error GoTo SendToExcel_Fail
Dim objWS As Excel.Worksheet
Dim rstData As ADODB.Recordset
Dim rstCount As ADODB.Recordset
Dim fld As ADODB.Field
Dim intColCount As Integer
Dim intRowCount As Integer
Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
Set rstCount = New ADODB.Recordset
rstCount.ActiveConnection = CurrentProject.Connection
'Invoke HourGlass
DoCmd.Hourglass True
'Try to create recordset and create Excel Object
If CreateRecordSet(rstData, rstCount, strTableUsing) Then
If CreateExcelObj() Then
'add a workbooks
With gobjExcel.Workbooks.Add
'Create a reference to the active sheet
With .Sheets(1)
intRowCount = 1
intColCount = 1
'Loop through the fields collection
'make each field name a collumn heading in excel
For Each fld In rstData.Fields
If fld.Type <> adLongVarBinary Then
.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
End If
Next fld
'send record set to excel
'** If you are going to transpose, then you are limited to 16383 rows of data
.Range("A2").CopyFromRecordset rstData, 16383
.Range("A1").CurrentRegion.Copy
End With
'add a sheet in that LAST tab position
With .Worksheets.Add(After:=.Sheets(.Worksheets.Count))
.Range("B5").PasteSpecial Paste:=xlPasteAll, Transpose:=False
.[C2].Value = strTitle
.[C2].EntireRow.Font.Bold = True
.[C2].EntireRow.Font.Size = 12
.[C2].EntireRow.Font.Name = "Arial"
.[C2].HorizontalAlignment = xlCenter
'stretch all the cells to 30 - this maks the auto work better
'.Cells.EntireColumn.ColumnWidth = 30 'This is for everything if we use cells
.Cells.EntireColumn.ColumnWidth = 30
'autofit the columns
.Cells.EntireColumn.AutoFit
End With
End With
Else
MsgBox "Excel not Successfully Launched", vbInformation
End If
End If
Exit_SendToExcel:
DoCmd.Hourglass False
Set objWS = Nothing
Set rstCount = Nothing
Set rstData = Nothing
Set fld = Nothing
Exit Function
SendToExcel_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_SendToExcel
End Function
I have a problem trying to perform the following:
1. add a conditional format at end in excel if Collum 1 > than colum 3 then mark red.
2. make heading bold and centered.
3.do not start excel again if you rerun query just add a new sheet if excel already open.
thanks for your help. Code as follow:
Function SendToExcelDataChecks(strTableUsing As String, strTitle As String)
On Error GoTo SendToExcel_Fail
Dim objWS As Excel.Worksheet
Dim rstData As ADODB.Recordset
Dim rstCount As ADODB.Recordset
Dim fld As ADODB.Field
Dim intColCount As Integer
Dim intRowCount As Integer
Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
Set rstCount = New ADODB.Recordset
rstCount.ActiveConnection = CurrentProject.Connection
'Invoke HourGlass
DoCmd.Hourglass True
'Try to create recordset and create Excel Object
If CreateRecordSet(rstData, rstCount, strTableUsing) Then
If CreateExcelObj() Then
'add a workbooks
With gobjExcel.Workbooks.Add
'Create a reference to the active sheet
With .Sheets(1)
intRowCount = 1
intColCount = 1
'Loop through the fields collection
'make each field name a collumn heading in excel
For Each fld In rstData.Fields
If fld.Type <> adLongVarBinary Then
.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
End If
Next fld
'send record set to excel
'** If you are going to transpose, then you are limited to 16383 rows of data
.Range("A2").CopyFromRecordset rstData, 16383
.Range("A1").CurrentRegion.Copy
End With
'add a sheet in that LAST tab position
With .Worksheets.Add(After:=.Sheets(.Worksheets.Count))
.Range("B5").PasteSpecial Paste:=xlPasteAll, Transpose:=False
.[C2].Value = strTitle
.[C2].EntireRow.Font.Bold = True
.[C2].EntireRow.Font.Size = 12
.[C2].EntireRow.Font.Name = "Arial"
.[C2].HorizontalAlignment = xlCenter
'stretch all the cells to 30 - this maks the auto work better
'.Cells.EntireColumn.ColumnWidth = 30 'This is for everything if we use cells
.Cells.EntireColumn.ColumnWidth = 30
'autofit the columns
.Cells.EntireColumn.AutoFit
End With
End With
Else
MsgBox "Excel not Successfully Launched", vbInformation
End If
End If
Exit_SendToExcel:
DoCmd.Hourglass False
Set objWS = Nothing
Set rstCount = Nothing
Set rstData = Nothing
Set fld = Nothing
Exit Function
SendToExcel_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_SendToExcel
End Function