2014-11-05 70 views
0

您好我想知道是否有人可以帮助我,我有(在下面)代码模块,但是,如果我目前在不同的打开工作簿中弹出错误消息向上。我猜测它试图在当前选定的工作簿中执行宏而不是所需的工作簿(“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 

任何帮助将非常感激。

+0

什么是错误信息?它在哪一点抛出错误? – kaybee99 2014-11-05 14:39:29

+0

错误是:“运行时错误'1004':选择工作表类失败的方法。”只有当代码运行时我在另一个工作簿中时才会发生。 – 2014-11-05 14:46:45

回答

0

我注意到,可以将代码甩开由于含蓄地提及了一些东西。

我通过CopyPriceOver去了,换成了更明确的一个,useda工作和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。如果您在不同的工作簿中工作,则会在该WorkBook中的任何工作表中处于活动状态时引用D10:CB10。虽然这可能不会导致错误,但我怀疑它是有意的。

此外,当您保存工作簿时引用的ActiveWorkbook,这又是你工作的一个

我有一些问题,一条线,拷贝* .Range(“DateNow:Stock2” ),我不知道什么应该是为什么我没有正确测试代码

0

推测'ScheduleCopyPriceOver'是在工作簿'MKL.xlsm'的模块中定义的?

尝试使用Workbooks("MKL.xlsm").Sheets("Data Quarter Hourly").Activate并将Sub'ScheduleCopyPriceOver'放入'Sub Workbook_Activate()'事件中的“MKL.xlsm”,ThisWorkbook模块中。

不要忘了注释掉现有的呼叫中Sub CopyPriceOver()

+0

感谢您的洞察力巴里我会尽力回复你,关于你之前的问题,我不知道如何回答它,因为我是一个新手,所有的东西都是从网上找到的其他代码拼凑在一起的。 – 2014-11-05 14:57:46

相关问题