2015-02-09 111 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解决;我不小心改变了工作表的名称,但不是在代码中。