2016-05-12 53 views
1

我寫了一個VBA函數,它檢查幾件事情並返回一個觸發條件格式的代碼 。除 之外,一切運行良好,該公式通常會觸發#value錯誤。它爲包含公式(幾千)的所有 單元執行此操作。每當我打開另一個工作簿時,出現錯誤 。有時候我不打 打開另一個工作簿。#用戶定義函數中的值錯誤

的功能是在這裏:

Function jjcheck(STDTRow As Integer, cuCOL As Integer, cuMax As Integer, trmEnd As Integer, trmEMax As Integer, worksheetSRC As String, lstCTCT As Date) As Variant 
    'use in spreadsheet =jjcheck(B2,Variables!$G$4,Variables!$G$2,Variables!$F$2,"SRM",U2) 
    '=jjcheck(B2,Variables!$G$4,Variables!$F$4,Variables!$G$2,Variables!$F$2,"SRM",IF(ISBLANK(U2),TODAY(),U2)) 
    Dim V() As String, dayMax As Integer, lookup As Date, theDiff As Integer, lstContact As String 
    V = Split(ActiveWorkbook.ActiveSheet.Cells(1, 2).Value, "-"): dayMax = V(1): theDiff = 256 
    lookup = lstCTCT 
    theDiff = DateDiff("d", lookup, Date): lstContact = "" 
    If theDiff > dayMax Then lstContact = "Alert" 
    Dim STDcu As Integer, STtrmEnd As Date, daysTOtrmend As Integer 
    STDcu = ActiveWorkbook.Worksheets(worksheetSRC).Cells(STDTRow, cuCOL).Value 
    STtrmEnd = ActiveWorkbook.Worksheets(worksheetSRC).Cells(STDTRow, trmEnd).Value 
    daysTOtrmend = DateDiff("d", Date, STtrmEnd) 
    If STDcu < cuMax And daysTOtrmend < trmEMax Then 
     jjcheck = "CHECK" & lstContact 
    ElseIf daysTOtrmend < trmEMax/2 Then 
     jjcheck = "ETerm" & lstContact 
    Else 
     jjcheck = "" & lstContact 
    End If 
End Function 

懷疑該錯誤可能是由於時代細胞U2是空的,我 改變什麼,我把爲lstCTCTIF(ISBLANK(U2),TODAY(),U2) 似乎並沒有幫助。 The image shows what happens in the worksheet 感謝您提供的任何見解。

+1

您應該考慮使用'ThisWorkbook'而不是'ActiveWorkbook'甚至更好,讓您的工作表名稱(在VBA編輯器),並直接引用他們 - 'wksHome。範圍(...)'。 「ActiveWorkbook」存在的問題是,它會根據您上次與哪個工作簿進行交互而發生變化。 – CallumDA

回答

0

讓我知道這是否有什麼區別?主要變化是從 ActiveWorkbookThisWorkbook。 (其他幾個人剛整理)

Function jjcheck(STDTRow As Integer, cuCOL As Integer, cuMax As Integer, trmEnd  As Integer, trmEMax As Integer, worksheetSRC As String, lstCTCT As Date) as Variant 

'use in spreadsheet =jjcheck(B2,Variables!$G$4,Variables!$G$2,Variables!$F$2,"SRM",U2) 
'=jjcheck(B2,Variables!$G$4,Variables!$F$4,Variables!$G$2,Variables!$F$2,"SRM",IF(ISBLANK(U2),TODAY(),U2)) 

Dim V() As String, lstContact As String 
Dim dayMax As Integer, theDiff As Integer, STDcu As Integer, daysTOtrmend As Integer 
Dim lookup As Date, STtrmEnd As Date 

STDcu = ThisWorkbook.Worksheets(worksheetSRC).Cells(STDTRow, cuCOL).Value 
STtrmEnd = ThisWorkbook.Worksheets(worksheetSRC).Cells(STDTRow, trmEnd).Value 
daysTOtrmend = DateDiff("d", Date, STtrmEnd) 
V = Split(ThisWorkbook.ActiveSheet.Cells(1, 2).Value, "-"): dayMax = V(1): theDiff = 256 
lookup = lstCTCT 
theDiff = DateDiff("d", lookup, Date): lstContact = vbNulltring 

If theDiff > dayMax Then lstContact = "Alert" 

If STDcu < cuMax And daysTOtrmend < trmEMax Then 
    jjcheck = "CHECK" & lstContact 
ElseIf daysTOtrmend < trmEMax/2 Then 
    jjcheck = "ETerm" & lstContact 
Else 
    jjcheck = vbNullString & lstContact 
End If 
End Function 
+0

非常感謝。這似乎做到了。很好的解釋。謝謝。 – TMZ

+0

沒問題@TMZ如果您很高興這回答了您的問題,請選擇此答案旁邊的勾號以將其標記爲已接受。 – CallumDA

+0

謝謝,我不知道我能做到。我試圖投票,但因爲我是新成員,所以無法投票。無論如何,答案的絕大部分工作非常好。當我打開新的電子表格時,該錯誤不再出現。但是,它有時會出現(不頻繁)。我懷疑它在同一張表中的某些「無關」宏運行時出現。我懷疑其他宏具有與該函數相同的問題,但是發現它很好奇錯誤出現在這個「無關」函數中。 – TMZ

相關問題