2017-03-10 201 views
0

所以我一直在琢磨這個愚蠢的小東西,我寫了更新大型工作表(修剪測試)一段時間,但我沒有看到我在哪裏做了一個錯誤。它似乎打破了第6行,但我不知道爲什麼。VBA宏返回運行時錯誤'5'

只是僅供參考,它似乎工作正常,如果我用特定的日期和DateDiff格式替換表中的標題(它實際上其他地方奇妙地工作)。這一省時的片成了自己的,嘆息的時候匯...

Sub UpdateData() 

Dim myWeek As String 
Dim myOh As String 
myOh = DateDiff(W, "01/01/2017", Date) 
myWeek = "Week " & myOh 
Application.ScreenUpdating = False 

    Range("A6:A8").Select 
    Selection.Copy 

    Range("Table2[[#Headers],[Week 10]:[Week 62]]").Select 
    Selection.Find(What:=myWeek, After:=ActiveCell, LookIn:=xlFormulas, _ 
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False).Activate 


    Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(3, 0)).Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
    :=False, Transpose:=False 

Application.ScreenUpdating = True 

末次

+2

*變量「節省時間片成爲它自己的時間片,嘆息......」 *,是的,因爲你忽略了#1節省時間片,又名'Option Explicit' :) –

+0

我仍然非常糟糕,所有這一切,只是從理由學習:( 我會注意到它的未來雖然!謝謝 – Atlas

回答

0

您需要如下引號括起來的時間間隔參數。你的方式有它一直在尋找一個叫w^

Sub UpdateData() 

Dim myWeek As String 
Dim myOh As String 
myOh = DateDiff("W", "01/01/2017", Date) 
myWeek = "Week " & myOh 
Application.ScreenUpdating = False 

    Range("A6:A8").Select 
    Selection.Copy 

    Range("Table2[[#Headers],[Week 10]:[Week 62]]").Select 
    Selection.Find(What:=myWeek, After:=ActiveCell, LookIn:=xlFormulas, _ 
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False).Activate 


    Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(3, 0)).Select 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
    :=False, Transpose:=False 

Application.ScreenUpdating = True 
End Sub 
+0

哇,我很愚蠢,謝謝!現在就像做夢一樣! – Atlas