8
8oclockbean
i'm no expert at vba and have been having a hard time trying to send a
value from an open access form to a specific cell in excel. I've been writing
a module in Ac2000 that opens excel, names the sheet, and sends some Access
table data over.
Thanks to Bob Larsons code I was able to do most of this. After a week of
trial and error i'm now also able to format the sheet the way it needs to be,
plus some conditional formatting that seems to be working out great- now i'm
very badly stuck on trying to send a value from an open access form to a
particular cell in the sheet. Any attempts with something like
xlWSh.Range("D1").Value = Me.cust.Value
-or- "" = Me!cust.Value
-or- "" = [Forms]![Reporting]![cust].Value
-or- With ApXL.Selection
.Value = Reporting.cust.Value
End With
end up with an error "Improper use of Me" or any number of other errors.
I've tried a number of different ways to reference this combo-box that
resides in the active access form- this is driving me nuts- i've spent 5 full
days trying to figure this out on my own and have gotten nowhere- can anyone
help me with what i'm missing? this seems like it should be so simple but i
am running around in circles- any help would be greatly appreciated.
Public Function SendTQ2Excel(tmpDated As String, Optional DatedReport As
String)
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim sSQL As String
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As Field
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
'On Error GoTo err_handler
Set dbs = CurrentDb
Set rst = CurrentDb.OpenRecordset(tmpDated)
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Add
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("Sheet1")
If Len(DatedReport) > 0 Then
xlWSh.Name = Left(DatedReport, 34)
End If
xlWSh.Range("A4").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A5").CopyFromRecordset rst
xlWSh.Range("1:1000").Select
With ApXL.Selection.Font
.Name = "Calibri"
.Size = 11
End With
xlWSh.Range("1:1000").Select
'ApXL.Selection.Font.Bold = True
With ApXL.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With
ApXL.ActiveSheet.Range("F5:F1000,G5:G1000,H5:H1000,J5:J1000,K5:K1000").
Select
ApXL.Selection.NumberFormat = "$#,##0.00"
ApXL.ActiveSheet.Range("J5:J1000").Select
With ApXL.Selection.Font
.Color = 32768
End With
ApXL.ActiveSheet.Range("J4").Select
With ApXL.Selection.Font
.Color = 32768
End With
ApXL.ActiveSheet.Range("K5:K1000").Select
With ApXL.Selection.Font
.Color = 255
End With
ApXL.ActiveSheet.Range("K4").Select
With ApXL.Selection.Font
.Color = 255
End With
' try to color the action values
Set ColorAction = Range("I5:I1000")
' start checking each cell in the target range for PAY or FIGHT
For Each Cell In ColorAction
If Cell.Value = "PAY" Then ' color it red
Cell.Font.ColorIndex = 3
ElseIf Cell.Value = "FIGHT" Then ' color it green
Cell.Font.ColorIndex = 10
ElseIf Cell.Value = "REDUCED" Then ' color it green
Cell.Font.ColorIndex = 10
Else ' remove all color
End If
Next
xlWSh.Range("A1").Select
' selects all of the cells
ApXL.ActiveSheet.Cells.Select
' does the "autofit" for all columns
ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
' selects the first cell to unselect all cells
xlWSh.Range("A1").Select
rst.Close
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Function
End Function
value from an open access form to a specific cell in excel. I've been writing
a module in Ac2000 that opens excel, names the sheet, and sends some Access
table data over.
Thanks to Bob Larsons code I was able to do most of this. After a week of
trial and error i'm now also able to format the sheet the way it needs to be,
plus some conditional formatting that seems to be working out great- now i'm
very badly stuck on trying to send a value from an open access form to a
particular cell in the sheet. Any attempts with something like
xlWSh.Range("D1").Value = Me.cust.Value
-or- "" = Me!cust.Value
-or- "" = [Forms]![Reporting]![cust].Value
-or- With ApXL.Selection
.Value = Reporting.cust.Value
End With
end up with an error "Improper use of Me" or any number of other errors.
I've tried a number of different ways to reference this combo-box that
resides in the active access form- this is driving me nuts- i've spent 5 full
days trying to figure this out on my own and have gotten nowhere- can anyone
help me with what i'm missing? this seems like it should be so simple but i
am running around in circles- any help would be greatly appreciated.
Public Function SendTQ2Excel(tmpDated As String, Optional DatedReport As
String)
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim sSQL As String
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim fld As Field
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
'On Error GoTo err_handler
Set dbs = CurrentDb
Set rst = CurrentDb.OpenRecordset(tmpDated)
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Add
ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("Sheet1")
If Len(DatedReport) > 0 Then
xlWSh.Name = Left(DatedReport, 34)
End If
xlWSh.Range("A4").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A5").CopyFromRecordset rst
xlWSh.Range("1:1000").Select
With ApXL.Selection.Font
.Name = "Calibri"
.Size = 11
End With
xlWSh.Range("1:1000").Select
'ApXL.Selection.Font.Bold = True
With ApXL.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With
ApXL.ActiveSheet.Range("F5:F1000,G5:G1000,H5:H1000,J5:J1000,K5:K1000").
Select
ApXL.Selection.NumberFormat = "$#,##0.00"
ApXL.ActiveSheet.Range("J5:J1000").Select
With ApXL.Selection.Font
.Color = 32768
End With
ApXL.ActiveSheet.Range("J4").Select
With ApXL.Selection.Font
.Color = 32768
End With
ApXL.ActiveSheet.Range("K5:K1000").Select
With ApXL.Selection.Font
.Color = 255
End With
ApXL.ActiveSheet.Range("K4").Select
With ApXL.Selection.Font
.Color = 255
End With
' try to color the action values
Set ColorAction = Range("I5:I1000")
' start checking each cell in the target range for PAY or FIGHT
For Each Cell In ColorAction
If Cell.Value = "PAY" Then ' color it red
Cell.Font.ColorIndex = 3
ElseIf Cell.Value = "FIGHT" Then ' color it green
Cell.Font.ColorIndex = 10
ElseIf Cell.Value = "REDUCED" Then ' color it green
Cell.Font.ColorIndex = 10
Else ' remove all color
End If
Next
xlWSh.Range("A1").Select
' selects all of the cells
ApXL.ActiveSheet.Cells.Select
' does the "autofit" for all columns
ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
' selects the first cell to unselect all cells
xlWSh.Range("A1").Select
rst.Close
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Function
End Function