如果打开另一个工作簿,则运行宏时出错

时间:2014-11-05 14:34:54

标签: excel vba excel-vba

您好我想知道是否有人可以帮助我,我在(下面)代码中有一个模块,但是,如果我当前在另一个打开的工作簿中,则会弹出一条错误消息。我猜它正在尝试在当前选定的工作簿中执行宏而不是所需的工作簿(“MKL”)。 以下是代码。

Dim TimeToRun

Sub auto_open()
    Call ScheduleCopyPriceOver
End Sub

Sub ScheduleCopyPriceOver()
    TimeToRun = Now + TimeValue("00:01:00")
    Application.OnTime TimeToRun, "CopyPriceOver"
End Sub

Sub CopyPriceOver()
    Application.DisplayAlerts = False
    Dim MyPath As String
    Dim MyFileName As String
    Dim celltxt As String
    Calculate
    Workbooks("MKL.xlsm").Sheets("Data Quarter Hourly").Select
    Call ScheduleCopyPriceOver
    Workbooks("MKL.xlsm").Sheets("Data Quarter Hourly").Rows("9:9").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Workbooks("MKL.xlsm").Sheets("Data Quarter Hourly").Range("DateNow:Stock2").Copy
    Workbooks("MKL.xlsm").Sheets("Data Quarter Hourly").Range("A9:C9").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Range("D10:CB10").Copy
    Workbooks("MKL.xlsm").Sheets("Data Quarter Hourly").Range("D9:CB9").PasteSpecial Paste:=xlFormulas, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    celltxt = Workbooks("MKL.xlsm").Sheets("Trades").Range("C2").Text
    If InStr(1, celltxt, "A") Or InStr(1, celltxt, "B") Then
        MyPath = "Z:\capital\Research - internal\Arb Trading Models\Trades"
        MyFileName = "Trades " & Format(Now(), "dd-mmm-yyyy hh-mm-ss")
        If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
        If Not Right(MyFileName, 4) = ".xls" Then MyFileName = MyFileName & ".xls"
        Workbooks("MKL.xlsm").Sheets("Trades").Copy
        With ActiveWorkbook
            .SaveAs Filename:= _
                MyPath & MyFileName, _
                Local:=True, _
                FileFormat:=xlWorkbookNormal, _
                CreateBackup:=False
            .Close False
        End With
    End If
    Application.DisplayAlerts = True
End Sub

Sub auto_close()
    On Error Resume Next
    Application.OnTime TimeToRun, "CopyPriceOver", , False
End Sub

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

大概' ScheduleCopyPriceOver'在Workbook& MKL.xlsm'?

的模块中定义

尝试使用Workbooks("MKL.xlsm").Sheets("Data Quarter Hourly").Activate并将Sub' ScheduleCopyPriceOver'进入" MKL.xlsm",这个工作簿模块在' Sub Workbook_Activate()'事件

请勿忘记在Sub CopyPriceOver()

中评论现有的来电

答案 1 :(得分:0)

我注意到由于隐式引用而导致代码丢失的一些事情。

我浏览了CopyPriceOver并将隐式引用替换为更明确的引用,使用了工作表和workbbok对象并添加了一些注释:

Sub CopyPriceOver()
    Application.DisplayAlerts = False
    Dim MyPath As String
    Dim MyFileName As String
    Dim celltxt As String
    Dim wb As Workbook: Set wb = Workbooks("MKL.xlsm") '<~~ we set a workbook object wb to the workbook "MKL.xlsm", this will save us a lot of writin and improve readability
    Dim wsDataQuarterHourly As Worksheet: Set wsDataQuarterHourly = wb.Worksheets("Data Quarter Hourly") '<~~ set a worksheet object to reference the "Data Quarter Hourly" sheet in the MKL.xlsm workbook, by use of the above wb object
    Dim wsTrades As Worksheet: Set wsTrades = wb.Worksheets("Trades") '<~~ set a worksheet object to reference the "Trades" sheet in in the MKL.xlsm workbook

    Calculate
    wsDataQuarterHourly.Select '<~~ i don't see the need to select it? I may be completely wrong, but if omitted what happens to your execution?
    Call ScheduleCopyPriceOver
    wsDataQuarterHourly.Rows("9:9").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove '<~~ using the worksheet object
    wsDataQuarterHourly.Range("DateNow:Stock2").Copy '<~~ I was not aware you could reference ranges like that? and it not working on my end
    wsDataQuarterHourly.Range("A9:C9").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    'Next line should be updated
    Range("D10:CB10").Copy '<~~ what do you want to copy? if from the "Data Quarter Hourly" then wsDataQuarterHourly.Range("D10:CB10")

    wsDataQuarterHourly.Range("D9:CB9").PasteSpecial Paste:=xlFormulas, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    celltxt = wsTrades.Range("C2").Text
    If InStr(1, celltxt, "A") Or InStr(1, celltxt, "B") Then
        MyPath = "Z:\capital\Research - internal\Arb Trading Models\Trades"
        MyFileName = "Trades " & Format(Now(), "dd-mmm-yyyy hh-mm-ss")
        If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
        If Not Right(MyFileName, 4) = ".xls" Then MyFileName = MyFileName & ".xls"
        wsTrades.Copy '<~~ why copy? I don't see it used?
        With wb '<~~ explicit reference to the "MKL.xlsm" workbook
            .SaveAs Filename:= _
                MyPath & MyFileName, _
                Local:=True, _
                FileFormat:=xlWorkbookNormal, _
                CreateBackup:=False
            .Close False
        End With
    End If
    Application.DisplayAlerts = True
End Sub

在上面的代码中,在你自己的代码中,你应该特别注意这一行:

    Range("D10:CB10").Copy

它隐含地暗示了ActiveWorkbook的ActiveSheet中的范围D10:CB10。如果您在不同的工作簿中工作,它将引用D10:CB10在该工作簿中的活动表中。虽然这可能不会导致错误,但我怀疑它是否有意。

此外,当您保存工作簿时,您引用了ActiveWorkbook,这也是您正在使用的工作簿。

我遇到了一些复制* .Range(&#34; DateNow:Stock2&#34;)的问题,我不知道为什么我没有正确测试代码

相关问题