使用Access中的条件格式将表从Access导出到Excel

7uhlpewt  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(148)

如何在Access VBA for Excel中设置条件格式?
代码必须在数据库中,因为最终人们将能够选择自己的文件位置。但这是另一个时间的问题。
我在考虑使用一个函数来查看代码的主要部分(以End Sub结尾)。函数还是在循环中执行呢

Public Sub ModifyExportedExcelFileFormats(sFile As String)
On Error GoTo Err_ModifyExportedExcelFileFormats

Dim xlApp As Object
Dim xlSheet As Object
Dim x1Rng As Object

Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.Workbooks.Open(sFile).Sheets("Full_List")

With xlApp
    .Application.Sheets("Full_List").Select
    .Application.Cells.Select
    .Application.Selection.ClearFormats
    .Application.Rows("1:1").Select
    .Application.Selection.Font.Bold = True
    .Application.Selection.Interior.ColorIndex = 41
    .Application.Selection.RowHeight = 38.25
    .Application.Selection.Font.ColorIndex = 2
    .Application.Selection.VerticalAlignment = xlCenter
    .Application.ActiveWorkbook.Save
    .Application.ActiveWorkbook.Close
    .Quit
End With

Set xlApp = Nothing
Set xlSheet = Nothing

vStatusBar = SysCmd(acSysCmdClearStatus)

Exit_ModifyExportedExcelFileFormats:
    Exit Sub

Err_ModifyExportedExcelFileFormats:
    vStatusBar = SysCmd(acSysCmdClearStatus)
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_ModifyExportedExcelFileFormats

End Sub

Public Function GetCelColor(ByRef CelVal As Long) As Long
Select Case True

Case CelVal = 1: GetCelColor = RGB(222, 0, 0): Exit Function
Case CelVal < 1: GetCelColor = RGB(0, 111, 0): Exit Function
Case CelVal > 1: GetCelColor = RGB(0, 0, 255): Exit Function
End Function
wz3gfoph

wz3gfoph1#

如果您使用的是Excel条件格式红绿灯图标集,则无需设置颜色。
此代码使用后期绑定,因此无需设置对Excel的引用。

**编辑:**在阅读了您的评论后,我添加了一个LastCell函数,这样它将找到工作表中包含数据的最后一个单元格,并将条件格式添加到A:M列直到该行。

需要提供正确的路径名并取消对图纸选择代码的注解。

Public Sub Test()

    Main "S:\Book3.xlsx"

End Sub

Public Sub Main(sFile)

    Dim oXL As Object
    Dim oWrkBk As Object
    Dim owrkSht As Object

    Set oXL = CreateXL
    Set oWrkBk = oXL.workbooks.Open(sFile)
    'Set oWrkSht = oWrkBk.worksheets("Full_List")

    'Testing
    'Set oWrkBk = oXL.workbooks.Add
    Set owrkSht = oWrkBk.worksheets(1)

    With owrkSht
        .cells.clearformats
        With .rows("1:1")
            With .Font
                .Bold = True
                .colorindex = 2
            End With
            .Interior.colorindex = 41
            .RowHeight = 38.25
            .verticalalignment = -4108 'xlCenter
        End With

        With .Range(.cells(2, 2), .cells(LastCell(owrkSht).row, 13))

            'Clear any conditional formatting first.
            'This won't need doing if the workbook is new.
            .FormatConditions.Delete

            .FormatConditions.AddIconSetCondition
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            With .FormatConditions(1)
                .ReverseOrder = False
                .ShowIconOnly = False
                .IconSet = oWrkBk.IconSets(4) 'xl3TrafficLights1
                With .IconCriteria(2)
                    .Type = 0 'xlConditionValueNumber
                    .Value = 2
                    .Operator = 7
                End With
                With .IconCriteria(3)
                    .Type = 0 'xlConditionValueNumber
                    .Value = 4
                    .Operator = 7
                End With
            End With

        End With
    End With

    With oWrkBk
        .Save
        .Close
    End With

End Sub

Public Function LastCell(wrkSht As Object, Optional Col As Long = 0) As Object

    Dim lLastCol As Long, lLastRow As Long

    On Error Resume Next

    With wrkSht
        If Col = 0 Then
            lLastCol = .cells.Find("*", , , , 2, 2).Column
            lLastRow = .cells.Find("*", , , , 1, 2).row
        Else
            lLastCol = .cells.Find("*", , , , 2, 2).Column
            lLastRow = .Columns(Col).Find("*", , , , 2, 2).row
        End If

        If lLastCol = 0 Then lLastCol = 1
        If lLastRow = 0 Then lLastRow = 1

        Set LastCell = wrkSht.cells(lLastRow, lLastCol)
    End With
    On Error GoTo 0

End Function

Public Function CreateXL(Optional bVisible As Boolean = True) As Object

    Dim oTmpXL As Object

    On Error Resume Next
    Set oTmpXL = GetObject(, "Excel.Application")

    If Err.Number <> 0 Then
        Err.Clear
        On Error GoTo ERROR_HANDLER
        Set oTmpXL = CreateObject("Excel.Application")
    End If

    oTmpXL.Visible = bVisible
    Set CreateXL = oTmpXL

    On Error GoTo 0
    Exit Function

ERROR_HANDLER:
    Select Case Err.Number

        Case Else
            MsgBox "Error " & Err.Number & vbCr & _
                " (" & Err.Description & ") in procedure CreateXL."
            Err.Clear
    End Select

End Function

相关问题