簡體   English   中英

在Excel中用於條件格式的VBA訪問代碼

[英]VBA Access code for Conditional formatting in Excel

我有一個將表導出到Excel的數據庫,我需要Z列中的單元格格式基於Y列中的值是Currency或Percentage。我知道我可以在Excel中使用條件格式來做到這一點,但是我很困惑如何在Access中做到這一點。

我試過了

Sub GetExcel_INV_Hist(File_Path_Name)

Dim MyXL As Object

Set MyXL = CreateObject("Excel.Application")
    MyXL.Workbooks.Open (File_Path_Name)

    MyXL.Visible = True

' IHFormat Macro

MyXL.Application.Columns("Z:AC").Select
MyXL.Application.Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"

MyXL.Application.Range("Z:Z").Select
MyXL.Application.Selection.NumberFormat = "0.00%"
MyXL.Application.Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=$Y3 = ""GM"""
MyXL.Application.Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
MyXL.Application.ExecuteExcel4Macro "File_Path_Name!(2,1,""0.00%"")"
MyXL.Application.Selection.FormatConditions(1).StopIfTrue = False
MyXL.Application.Selection.Copy
MyXL.Application.Range("Z4").Select
MyXL.Application.Range(Selection, Selection.End(xlDown)).Select
MyXL.Application.Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

首先將Z的格式更改為貨幣,然后選擇第一個單元格Z3,檢查是否Y3 = GM,如果是,則將格式更改為百分比。 然后選擇Z的其余部分並進行選擇性粘貼/格式化。 它在ExecuteExcel4Macro行上引發錯誤。 我假設因為條件格式化未在流程創建的工作簿上完成。

在此先感謝您的幫助。

編輯:我在excel中記錄了該宏,因此該行被創建為excel宏的一部分。 我擴展了代碼,這是錯誤的:

Errorsnip

其余的格式化工作完美。

再次感謝

我想您很難跟蹤選擇的內容,而Access卻不了解xlExpression (2)xlPasteFormats (-4122)xlNone (-4142)的含義。 您將需要使用等效的數值。

MyXL.Application.ExecuteExcel4Macro "File_Path_Name!(2,1,""0.00%"")"我感到驚訝,但這就是我錄制宏時所得到的。 我還是把那行扔掉,而改用NumberFormat = "£#,##0.00"

試試這個代碼:

Sub Test()

    GetExcel_INV_Hist "C:\Path_To_My_Workbook\Book1.xlsx"

End Sub

Public Sub GetExcel_INV_Hist(File_Path_Name As String)

    Dim MyXL As Object
    Dim MyWB As Object

    Set MyXL = CreateXL

    Set MyWB = MyXL.Workbooks.Open(File_Path_Name)

    With MyWB.worksheets("Sheet1")
        .Columns("AA:AC").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
        .Columns("Z:Z").NumberFormat = "0.00%"

        With .Columns("Z:Z")
            .FormatConditions.Add Type:=2, Formula1:="=$Y1=""GM""" '2 = xlExpression
            .FormatConditions(.FormatConditions.Count).SetFirstPriority
            With .FormatConditions(1)
                .NumberFormat = "$#,##0.00"
            End With
        End With

    End With

End Sub

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:
        MsgBox "Error " & Err.Number & vbCr & _
            " (" & Err.Description & ") in procedure CreateXL."
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM