错误代码1004

时间:2014-05-07 21:24:14

标签: excel vba range

我尝试从Access 2010导出查询到Excel 2010,然后插入条件格式。此代码导出信息正常,但抛出错误1004" object_Global的范围失败"。代码是使用excel宏构建的,然后复制并粘贴到我的Access模块​​中。对此的任何帮助将不胜感激。

Sub Macro2()
Dim objXls As Excel.Application
    Dim MyBook As Excel.Workbook
    Dim MySheet As Excel.Worksheet
    Dim MyFile As String


    DoCmd.TransferSpreadsheet acExport, 8, "VASL/OCA Report", _
    "G:\shared documents\FSFN OCA Adoption Reconciliations\FY14\VASL-OCA Reconciliation", True

    Set objXls = CreateObject("Excel.Application")
    MyFile = "G:\shared documents\FSFN OCA Adoption Reconciliations\FY14\VASL-OCA Reconciliation.xls"
    objXls.Workbooks.Open ("" & MyFile)
    objXls.Visible = True

    Set MyBook = objXls.Workbooks("VASL-OCA Reconciliation.xls")
    Set MySheet = MyBook.Worksheets("VASL_OCA_Report")

    MySheet.Activate

    Range("G2").Select

    'deleted "scrollRow" lines

    Range("G2:G808").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
        Formula1:="=H2"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = True
    Range("H808").Select

    'deleted "scrollRow" lines

    Range("H2:H808").Select
    Range("H808").Activate
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
        Formula1:="=G2"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = True



    End Sub

1 个答案:

答案 0 :(得分:1)

未测试:

Sub Macro2()

    Const FLDR As String = "G:\shared documents\FSFN OCA Adoption Reconciliations\FY14\"
    Dim objXls As Excel.Application
    Dim MyBook As Excel.Workbook
    Dim MySheet As Excel.Worksheet

    DoCmd.TransferSpreadsheet acExport, 8, "VASL/OCA Report", _
                              FLDR & "VASL-OCA Reconciliation", True

    Set objXls = CreateObject("Excel.Application")
    objXls.Visible = True

    Set MyBook = objXls.Workbooks.Open(FLDR & "VASL-OCA Reconciliation.xls")
    Set MySheet = MyBook.Worksheets("VASL_OCA_Report") '<<EDIT

    MySheet.Activate

    With MySheet.Range("G2:G808")
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
                              Formula1:="=H2"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = True
    End With

    With MySheet.Range("H2:H808")
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
                            Formula1:="=G2"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = True
    End With

End Sub