Excel 上的错误代码1004

Excel 上的错误代码1004,excel,vba,range,Excel,Vba,Range,我正在尝试将查询从Access 2010导出到Excel 2010,然后插入条件格式。这段代码很好地导出了信息,但抛出了一个错误1004“对象范围\全局失败”。代码是使用excel宏生成的,然后复制并粘贴到我的Access模块中。对此,我们将不胜感激 Sub Macro2() Dim objXls As Excel.Application Dim MyBook As Excel.Workbook Dim MySheet As Excel.Worksheet Dim MyF

我正在尝试将查询从Access 2010导出到Excel 2010,然后插入条件格式。这段代码很好地导出了信息,但抛出了一个错误1004“对象范围\全局失败”。代码是使用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
未经测试:

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
Sub-Macro2()
Const FLDR As String=“G:\shared documents\FSFN OCA采用对账\FY14\”
Dim objXls作为Excel.Application
将MyBook设置为Excel.工作簿
将我的工作表设置为Excel.Worksheet
DoCmd.TransferSpreadsheet acExport,8,“VASL/OCA报告”_
FLDR和“VASL-OCA对账”,正确
设置objXls=CreateObject(“Excel.Application”)
objXls.Visible=True
设置MyBook=objXls.Workbooks.Open(FLDR和“VASL-OCA对账.xls”)

设置MySheet=MyBook.Worksheets(“VASL\u OCA\u Report”)“我从代码中删除了几百行“ScrollRow”行-它们在这里不相关。
Range(“G2”)。选择
应该是
MySheet.Range(“G2”)。选择
同样,所有后续使用的
Range()
或任何其他特定于Excel的内容需要使用您拥有的其他引用(例如
MySheet
)的Excel应用程序对象进行限定,哪一行被指示为引发错误的行?你有这些信息就在你面前,而我们没有;如果您在请求我们帮助时提供所有可用信息,这会很有帮助。Ken,它将错误抛出行范围(“G2”)。选择感谢Tim提供的所有帮助。您编写的代码在MySheet.Activate上抛出错误91