Excel UDF - 引用其他工作簿表时出现#Value错误

时间:2016-08-27 07:53:49

标签: excel vba excel-vba

我是新来的。请原谅我,如果我犯了错误,如果有人给我指示,如果我做错了,我会感激不尽。

我的问题与创建一个UDF有关,该UDF通过匹配YEAR& amp;来提供另一个工作簿表中的提议号。来自当前工作簿的NAME。当前代码给出了#VALUE错误。

Function ProposalNo(BorrowerName As String, Year As String) As String
Dim PropNo As String
Dim BorrowerName As String
Dim Year As String
Dim CMISPath As String
Dim wb As Workbook
Dim sht As Worksheet
Dim Match As Double

CMISPath = "C:\Users\15169\Desktop\Data Dump\CMIS\SME- CMIS.xlsb"
Set wb = Workbooks.Open(CMISPath)
Set sht = wb.Worksheets("Sheet1")

LastRow = wb.Worksheets(1).Cells(sht.Rows.Count, "A").End(xlUp).Row

        For i = 2 To LastRow
            If (JW(Trim(UCase(CStr(BorrowerName))), Trim(UCase(sht.Cells(i, 11).Value))) >= 0.85 And CStr(Year) = CStr(sht.Cells(i, 10).Value)) Then
                PropNo = sht.Cells(i, 1)
            End If
        Next
    ProposalNo = PropNo
End Function

JW是另一个基本上尝试使用百分比匹配函数匹配借款人名称的功能。由于Set sht = wb.Worksheets("Sheet1")而发生#Value错误。我尝试创建另一个函数来分配工作表详细信息,但这不起作用。如果有人能给我一些指示,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

这不是完整的答案(因为我不知道功能 JW 的功能)。 对您的代码进行一些更正:

1.删除以下2个变量声明(因为它们在调用此函数时传递):

 Dim BorrowerName As String
 Dim Year As String

以下一行:

Set sht = wb.Worksheets("Sheet1") - 对我有用,你确定你有一张名为" Sheet1 "在您的wb被叫" SME- CMIS.xlsb "

3.一旦您能够正确设置工作表变量sht,就可以在以下行中使用:

LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

常规消息:在开头使用Option Explicit,它会解决这些错误中的一半。

(在运行Dim i As Long循环之前,不要忘记定义For。)

4. 最后,您定义此功能以接收BorrowerName As String, Year As String,因此,在以下行中您不需要使用Cstr:< / p>

If (JW(Trim(UCase(CStr(BorrowerName))), Trim(UCase(sht.Cells(i, 11).Value))) >= 0.85 And CStr(Year) = CStr(sht.Cells(i, 10).Value)) Then

只需使用:

If (JW(Trim(UCase(BorrowerName)), Trim(UCase(sht.Cells(i, 11).Value))) >= 0.85 And Year = CStr(sht.Cells(i, 10).Value)) Then

整个代码(无法测试 JW 功能):

Option Explicit

Function ProposalNo(BorrowerName As String, Year As String) As String

Dim PropNo As String
Dim CMISPath As String
Dim wb As Workbook
Dim sht As Worksheet
Dim Match As Double
Dim i As Long

CMISPath = "C:\Users\15169\Desktop\Data Dump\CMIS\SME- CMIS.xlsb"
Set wb = Workbooks.Open(CMISPath)
Set sht = wb.Worksheets("Sheet1")

LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
    If ((JW(Trim(UCase(BorrowerName)), Trim(UCase(sht.Cells(i, 11).Value))) >= 0.85) And (Year = CStr(sht.Cells(i, 10).Value))) Then
        PropNo = sht.Cells(i, 1)
    End If
Next

ProposalNo = PropNo

End Function

答案 1 :(得分:0)

用户定义函数(UDF)"must not modify the contents or formatting of any cell and must not modify the operating environment of Excel"

因此,在您要使用该函数时,所需的工作簿必须已经打开,并且只将其设置为给定(或传递)的工作簿名称

这是你的UDF的相应重构:

Function ProposalNo(BorrowerName As String, Year As String) As String
    Dim PropNo As String
    Dim sht As Worksheet
    Dim i As Long

    Const WBNAME As String = "SME- CMIS.xlsb"
    Const SHEETNAME As String = "Sheet1"

    Set sht = GetSheet(WBNAME, SHEETNAME)
    If sht Is Nothing Then
        ProposalNo = "couldn't fine sheet '" & SHEETNAME & "' in workbook '" & WBNAME & "'"
    Else
        With sht
            For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
                If (JW(Trim(UCase(CStr(BorrowerName))), Trim(UCase(sht.Cells(i, 11).value))) >= 0.85 And CStr(Year) = CStr(sht.Cells(i, 10).value)) Then
                    ProposalNo = sht.Cells(i, 1)
                    Exit For
                End If
            Next
        End With
    End If
End Function

Function GetSheet(WBNAME As String, shtName As String) As Worksheet
    On Error Resume Next
    Set GetSheet = Workbooks(WBNAME).Worksheets(shtName)
    On Error GoTo 0
End Function