2015-02-09 86 views
0

我完全不知所措......此宏查看Range,用Rnd繪製一個數字,然後創建一個vlookup,每次打開我的工作簿時都會返回一個報價和作者(如果適用)。運行時錯誤424:新版本和舊版本

這個錯誤今天晚上纔開始,但只在今天的版本。我可以按照預期打開舊版本並運行代碼。

下面是「今天的」最新副本併產生運行時錯誤,用break發生就行定義字符串quote

Private Sub Workbook_Open() 

Dim sht As Object 
Dim RandNumb As Integer 
Dim quote As String 
Dim author As String 
Dim ws As Worksheet 

Set ws = Worksheets("Home") 

    'Make "Home" Sheet visible and select 
ws.Visible = True 

    'Search for all sheets not named "Home" and hide them 
For Each sht In Worksheets 
    If sht.Name <> "Home" Then 
    sht.Visible = xlSheetHidden 
    End If 
Next sht 

    'Create random number, then vlookup based off number 

RandNumb = Int((56 - 1 + 1) * Rnd + 1) 
quote = Application.WorksheetFunction.VLookup(RandNumb, Sheet3.Range("ba101:bc465"), 2, False) 
author = Application.WorksheetFunction.VLookup(RandNumb, Sheet3.Range("ba101:bc465"), 3, False) 

If quote <> Empty Then 
    MsgBox quote & vbNewLine & vbNewLine & " - " & author, vbOKOnly, "Quote of the day" 
End If 

End Sub 

雖然從2/6的作品就好了版本

Private Sub Workbook_Open() 

Dim sht As Object 
Dim RandNumb As Integer 
Dim quote As String 
Dim author As String 
Dim ws As Worksheet 

Set ws = Worksheets("Home") 

    'Make "Home" Sheet visible and select 
ws.Visible = True 
ws.Select 
Range("A1").Select 

    'Search for all sheets not named "Home" and hide them 
For Each sht In Worksheets 
    If sht.Name <> "Home" Then 
    sht.Visible = xlSheetHidden 
    End If 
Next sht 

    'Create random number, then vlookup based off number 

RandNumb = Int((56 - 1 + 1) * Rnd + 1) 
quote = Application.WorksheetFunction.VLookup(RandNumb, Sheet3.Range("ba101:bc465"), 2, False) 
author = Application.WorksheetFunction.VLookup(RandNumb, Sheet3.Range("ba101:bc465"), 3, False) 

If quote <> Empty Then 
    MsgBox quote & vbNewLine & vbNewLine & " - " & author, vbOKOnly, "Quote of the day" 
End If 

End Sub 

這些代碼對我來說沒有什麼不同。即使我從2/6複製版本並將其放入「今日」,我仍然會收到錯誤。請幫助。

+0

您在第二個示例('「A1」')中選擇的'範圍'似乎並不存在於該表中。 – meatspace 2015-02-09 02:43:50

+1

你確定你沒有更改Sheet3工作表的代號嗎?或者聲明一個名爲'Application'的變量? – Rory 2015-02-09 09:56:15

+0

Thanks @Rory,它是'Sheet3',爲了組織,我將它改爲'Sheet03'。這是我遇到的主要問題之一,做了太多改變,並不總是記住我所做的。有什麼建議麼? – PlainsWind 2015-02-09 14:20:59

回答

0

這是@Rory解決;我不小心改變了工作表的名稱,但不是在代碼中。

相關問題