我寫了一個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
是空的,我 改變什麼,我把爲lstCTCT
到IF(ISBLANK(U2),TODAY(),U2)
似乎並沒有幫助。 The image shows what happens in the worksheet 感謝您提供的任何見解。
您應該考慮使用'ThisWorkbook'而不是'ActiveWorkbook'甚至更好,讓您的工作表名稱(在VBA編輯器),並直接引用他們 - 'wksHome。範圍(...)'。 「ActiveWorkbook」存在的問題是,它會根據您上次與哪個工作簿進行交互而發生變化。 – CallumDA