2014-11-05 73 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

相關問題