2016-08-22 134 views
1

我正在通過下面的腳本來最終從數據查詢中將一系列數據從基於在inputbox中輸入的值中抽取到單獨的工作表中,但是我一直遇到424錯誤 - 對象需要。424對象VBA查找所需的錯誤

爲了測試是否這工作我想顯示一個消息框,以便然而腳本保持在Vlookup

Private Sub Workbook_Open() 


Dim NextRelease As String 

If MsgBox("Would you like to promote the next release in batch?", vbQuestion + vbYesNo, "Promote the next release?") = vbYes Then 
NextRelease = InputBox("Please enter the date of the next release", "Next Release", "DD/MM/YYYY") 
ReleaseDate = Application.WorksheetFunction.VLookup(NextRelease, TRELINFO.Range("A2:B4"), 1, False) 
If NextRelease = ReleaseDate Then 
MsgBox ("Working") 

請未能有人能提供一個答案,爲什麼這我就可以開始下一階段正在發生並有望解決這個問題。 提前謝謝!

+0

什麼行是錯誤出現在? –

+0

'TRELINFO'這是錯誤。你有任何代碼名稱爲「TRELINFO」的工作表,或者你有一個工作表類型變量設置爲任何工作表? – cyboashu

+0

@cyboashu - 工作表名稱爲TRELINFO,數據查詢也命名爲TRELINFO。爲了避免混淆,我現在已將表格暫時重命名爲TRELINFO2,但同樣的錯誤正在返回。 – Paul

回答

0

在引用工作表之前,請嘗試添加。用您的TRELINFO表替換'Sheet1'。

編輯:如果您的表的第一行中沒有與用戶輸入匹配的值,則VLOOKUP將返回錯誤。在excel中對錶格(使用日期格式)進行測試後,下面的內容適用於我。

Private Sub Workbook_Open() 

Set trelinfo = Sheets("TRELINFO") 
Dim NextRelease As Long 

If MsgBox("Would you like to promote the next release in batch?", vbQuestion + vbYesNo, "Promote the next release?") = vbYes Then _ 
NextRelease = CLng(CDate(InputBox("Please enter the date of the next release", "Next Release", "DD/MM/YYYY"))) 

checkblank = WorksheetFunction.CountIf(trelinfo.Range("A2:A4"), NextRelease) 

If checkblank <> 0 Then 
    ReleaseDate = Application.WorksheetFunction.VLookup(NextRelease, trelinfo.Range("A2:B4"), 1, False) 
    If NextRelease = ReleaseDate Then _ 
    MsgBox "Working" 

Else 
    MsgBox "Release not found" 

End If 

End Sub 
+0

我已經嘗試添加'設置TRELINFO =工作表(「TRELINFO」)'但同樣的錯誤正在返回。 – Paul

+0

我剛剛注意到,在添加上面的錯誤信息後,返回的錯誤信息現在是1004錯誤 - 應用程序定義或對象定義的錯誤 – Paul

+0

1004錯誤可能是由於您的VLOOKUP無法返回值。你有沒有嘗試提交一個肯定位於A2到A4的值? – Rory

0

確保工作表TRELINFO存在或被正確命名。看來該函數找不到該工作表。

+0

我已經三重檢查了表單的名稱,並且可以確認所有信息都是正確的。 – Paul

+0

注意到其他評論中的1004錯誤,請確保該值存在於查找範圍中。如果VLookup在電子表格中使用它時會返回#NA或#Value,則會引發1004錯誤。 – Kevin

1

NextRelease是一個字符串值:你可以不看,最多的日期的表沒有首先將其轉換爲雙。

由於您沒有從VLOOKUP返回值,因此您可以更簡單地使用MATCH。

刪除WorksheetFunction可讓您測試錯誤的返回值,而不會在不匹配時提高運行時錯誤。

Private Sub Workbook_Open() 

Dim NextRelease As String 

If MsgBox("Would you like to promote the next release in batch?", _ 
      vbQuestion + vbYesNo, "Promote the next release?") = vbYes Then 

    NextRelease = InputBox("Please enter the date of the next release", _ 
          "Next Release", "DD/MM/YYYY") 

    If not IsError(Application.Match(CDbl(DateValue(NextRelease), _ 
        TRELINFO.Range("A2:B4"), 0) Then 

     MsgBox ("Working") 
0

只要用這個代替

ReleaseDate = Application.VLookup(NextRelease, Worksheets("trelinfo").Range("A2:B4"), 1, False) 

我遇到使用VLOOKUP函數時,在此之前的錯誤。我只是刪除WorksheetFunction然後它的工作

相關問題