简体   繁体   English

Excel VBA 尝试为数组编写函数时出现 #Value 错误

[英]Excel VBA Getting a #Value error when trying to write a function for an array

I am trying to find the shortest string in an array from a worksheet.我试图从工作表中找到数组中最短的字符串。 However, when I try to execute the function I get a #Value error.但是,当我尝试执行该函数时,出现 #Value 错误。 What am I doing wrong?我究竟做错了什么? Thanks!谢谢!

Function ShortestString(ByVal array1 As Range)

Dim nRows As Integer
Dim i As Integer
Dim cell As String
Dim cellLength As Integer
Dim string1 As String

nRows = array1.Rows.Count
cellLength = 100
cell = array1.Cells(i, 1).Value

For i = 1 To nRows
    If cellLength > Len(cell) Then
    cellLength = Len(cell)
    string1 = cell
    End If
Next i

ShortestString = string1

End Function

Several issues in your code.您的代码中有几个问题。

  • First, you never assign i before you call cell = array1.Cells(i, 1).Value .首先,在调用cell = array1.Cells(i, 1).Value之前,您永远不会分配i That's going to be a run-time error 1004.这将是一个运行时错误 1004。
  • Second, you should test the input Range to make sure it's an "array" before you do anything else.其次,在做任何其他事情之前,您应该测试输入Range以确保它是一个“数组”。 If it's a single cell, you can skip everything else and just return the text of that cell.如果它是单个单元格,您可以跳过其他所有内容,只返回该单元格的文本。
  • Third, you never update the value of cell anywhere in your function - it will always be the uninitialized value of vbNullString .第三,您永远不会在函数中的任何地方更新cell的值 - 它始终是vbNullString的未初始化值。
  • Fourth, the maximum cell text length is 32767 characters, not 100. You should use that as your initial value.第四,单元格文本的最大长度是 32767 个字符,而不是 100 个。您应该将其用作初始值。

My guess is that you're looking for something more like this:我的猜测是你正在寻找更像这样的东西:

Option Explicit

Public Function ShortestString(ByVal target As Range) As String
    Dim rowCount As Long, columnCount As Long
    rowCount = target.Rows.Count
    columnCount = target.Columns.Count

    If rowCount = 1 And columnCount = 1 Then
        ShortestString = target.Text
        Exit Function
    End If

    Dim rowIdx As Long, colIdx As Long
    Dim shortest As String, current As String
    shortest = String(32767, " ")

    For rowIdx = 1 To rowCount
        For colIdx = 1 To columnCount
            current = target.Cells(rowIdx, colIdx).Text
            If Len(current) <= Len(shortest) Then
                shortest = current
            End If
        Next
    Next

    ShortestString = shortest
End Function

Note that you can probably actually use an array instead of testing individual cells, but that's an optimization you can make if the performance isn't up to snuff.请注意,您实际上可以使用数组而不是测试单个单元格,但如果性能不理想,您可以进行优化。


Edit:编辑:

You can convert the function to use an array of cell values by reading the entire range into a Variant() :您可以通过将整个范围读入Variant()来将该函数转换为使用单元格值数组:

Dim cellValues() As Variant
cellValues = target.Value

For rowIdx = 1 To rowCount
    For colIdx = 1 To columnCount
        current = CStr(cellValues(rowIdx, colIdx))
        If Len(current) <= Len(shortest) Then
            shortest = current
        End If
    Next
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM