复制工作表时出现#VALUE错误

我使用的UDF基本上是一个简化的查找。 这里是代码:

Function SUELDOBASICO(Columna As Integer) As Double SUELDOBASICO = Application.WorksheetFunction.VLookup(Application.Caller.Parent.Cells(Application.Caller.Row, 3), Application.Caller.Parent.Parent.Sheets("Escalas Salariales").Range("A3:DJ23"), Columna, False) End Function 

我注意到,有时当复制工作表(在同一个工作簿)时,我得到一个#VALUE错误。 如果我在Excel中“编辑”单元格,只要使用F2Enter就不会改变,错误消失。 它曾经发生,当简单地改变窗口(例如对Firefox,并且回到Excel)。 这就是为什么我在代码中使用CallerParent原因。 它几乎是完全固定的,除非有时复印纸张。 我似乎无法find错误的来源。 请帮助。

我知道这不是你确切的问题,但是,如果可能的话,我会build议完全避免VBA,如果这是一个选项,并写下你的公式如下:

 =VLOOKUP(INDIRECT("C"&ROW()),'Escalas Salariales'!$A$3:$DJ$23,XXXXX,false) 

XXXXX可以和你目前的Columnavariables相同。

这将保证你的代码按照需要工作。


鉴于在评论中讨论了什么,并尽我所能确保这个工作正常,我其实并没有看到你的代码有什么问题,只是猜测它可能与Application.Caller

当这种事情发生在我身上时,我尽我所能只用debugging器来弄清楚为什么 – 这通常包括Stop语句能够进入代码并查看发生了什么,或Debug.Print Err.Descriptiontypes的消息。

无论哪种方式,我试图打破每一个部分,所以,至less你可以看到问题的来源。 要做到这一点,我重新工作你的function(与一些主要的矫枉过正)….

 Function SUELDOBASICO(Columna As Integer) As Double On Error GoTo ErrorCheck Dim CellRef As Range Dim LookupRef As Range Set CellRef = Cells(Application.Caller.Range("A1").Row, 3) Set LookupRef = Application.Caller.Worksheet.Parent.Sheets("Escalas Salariales").Range("A3:DJ23") SUELDOBASICO = Application.VLookup(CellRef, LookupRef, Columna, False) Exit Function ErrorCheck: Stop Resume End Function 

(还请注意,我将Application.WorksheetFunction.VLookup更改为Application.VLookup – 查看此链接以获取解释)

一旦你弄清楚了,我会,但是,从函数中删除错误代码,因为这不是一个好主意的生产代码 – 只是为了debugging。

希望能给你你正在寻找的答案。

希望可以帮助….


更新#2:

考虑到复制工作表的可能性导致这个错误,这里是一个testing,看看过程是否得到解决:

 Function SUELDOBASICO(Columna As Integer) As Double On Error GoTo ErrorCheck Dim NumTimesErrored As Integer Dim StartTime As Double Dim WaitSeconds As Integer NumTimesErrored = 0 Dim CellRef As Range Dim LookupRef As Range Set CellRef = Cells(Application.Caller.Range("A1").Row, 3) Set LookupRef = Application.Caller.Worksheet.Parent.Sheets("Escalas Salariales").Range("A3:DJ23") SUELDOBASICO = Application.VLookup(CellRef, LookupRef, Columna, False) Exit Function ErrorCheck: ' This will make it tries this "hack" up to 3 times: If NumTimesErrored < 3 Then StartTime = Now WaitSeconds = 1 ' Wait one second Loop While Now - TimeStart < TimeSerial(0, 0, WaitSeconds) DoEvents ' Allows all the other processes to complete Loop ' Increment the number of times you've tried this: NumTimesErrored = NumTimesErrored + 1 ' Go back to the calculation step that errored Resume End If Stop Resume End Function 

不需要使用来电者和家长。

 Function SUELDOBASICO(Cell as Range, LookupRange as range, Columna As Integer) As Double ' When you call the function : ' set Cell to be the cell in column C in the same row ' Set LookupRange to Sheets("Escalas Salariales").Range("$A$3:$DJ$23") SUELDOBASICO = Application.WorksheetFunction.VLookup(Cell, LookupRange, Columna, False) End Function 

单元格中的公式示例…

= SUEL​​DOBASICO(C10,'Escalas Salariales'!$ A $ 3:$ DJ $ 23)