循环遍历范围中的每个单元格以确保它们的长度相同

时间:2015-07-06 19:38:18

标签: excel vba

我正在尝试运行测试以确保一系列单元格中的每个单元格都具有相同的长度字符串。基本上,我想检查定义范围内的每个单元格的字符串长度是否为1.如果每个单元格都是1,那么我将对代码执行某些操作,否则我将执行其他操作。我正在使用一个评估(sumproduct(length())构造,但我得到一个类型不匹配。任何帮助将不胜感激。

Sub Name_Length()

Dim aCell As Range
Dim ws As Worksheet
Dim LastRow As Long
Dim NameStr As String
Dim Namer As Range
Dim NameResult As Integer
Dim i As Integer
Dim cell As Range

Set ws = Sheets("Data Input")

LastRow = ws.Cells(Rows.Count, "E").End(xlUp).Row

For Each cell In ws.Range("E4:E" & LastRow)
NameResult = Evaluate("=SUMPRODUCT(LEN(cell))")
Next cell

If NameResult >= 1 Then
 'Do something'
Else
 MsgBox NameResult
End If

End Sub

4 个答案:

答案 0 :(得分:2)

Sub Name_Length()

Dim ws As Worksheet
Dim LastRow As Long
Dim addr As String

    Set ws = Sheets("Data Input")
    LastRow = ws.Cells(Rows.Count, "E").End(xlUp).Row
    addr = "E4:E" & LastRow

    'Note `ws.Evaluate` - otherwise the context used will be the
    '   ActiveSheet, which may not be what you want...
    If ws.Evaluate("SUM(1*(LEN(" & addr & ")=1))=rows(" & addr & ")") Then
        Debug.Print "all length=1"
    Else
        Debug.Print "Not all length=1"
    End If

End Sub

答案 1 :(得分:0)

两个问题......

  • 您已传递引用范围(cell)的变量名作为地址参考。它不是。 cell.address,但是地址参考。
  • 您已将变量名称作为字符串的一部分传递。它需要被分解并将其值连接到字符串中。

NameResult = Evaluate("=SUMPRODUCT(LEN(" & cell.address & "))")

答案 2 :(得分:0)

你需要将cell.address连接到你的evaluate函数中,这样看起来就像nwhaught所说:

NameResult = Evaluate("=SUMPRODUCT(LEN(" & cell.address & "))")

但是,你所拥有的东西可能无法返回你想要的东西,因为如果最后一个单元格的长度大于或等于1个字符,它将只会执行你想要的东西。要纠正这个问题,可以尝试这样的事情:

Sub Name_Length()

Dim aCell As Range
Dim ws As Worksheet
Dim LastRow As Long
Dim NameStr As String
Dim Namer As Range
Dim NameResult As Integer
Dim i As Integer
Dim cell As Range

Set ws = Sheets("Data Input")

LastRow = ws.Cells(Rows.Count, "E").End(xlUp).Row

Set cell = ws.Range("E4:E" & LastRow)
NameResult = Evaluate("=SUMPRODUCT(LEN(" & cell.address & "))")

If NameResult >= 1 Then
 'Do something'
Else
 MsgBox NameResult
End If

End Sub

答案 3 :(得分:0)

在我看来,你正在寻找一个关于单一权威性问题的正确/错误; '范围内的所有单元格是否包含长度相同的值?'

如果是这种情况,那么只有在存储长度值并将其与所有后续长度值进行比较时,才能遍历这些值。此外,遍历所有单元格将比批量操作慢。

Sub Name_Length()
    Dim aCells As Range
    Dim LastRow As Long
    Dim NameLengthResult As Double
    Dim bAllTheSameLength As Boolean

    With Sheets("Data Input")
        LastRow = .Cells(Rows.Count, "E").End(xlUp).Row
        Set aCells = .Range("E4:E" & LastRow)
        NameLengthResult = CDbl(Evaluate("SUMPRODUCT(LEN(" & aCells.Address(external:=True) & "))") / aCells.Count)
        bAllTheSameLength = CBool(Round(NameLengthResult, 14) = Len(aCells.Cells(1, 1).Value2))

        If bAllTheSameLength Then
            MsgBox "They are all the same." & Chr(10) & NameLengthResult & " characters wide"
        Else
            MsgBox "They are NOT all the same."
        End If
    End With
End Sub

这将确定所有单元格是否包含相同数量的字符。请特别注意日期,因为工作表LEN function将返回 5 作为当前日期的长度(今天 42191 )。

如果范围内的所有单元格都包含具有相同字符数的值,则可以使用 NameLengthResult 或该范围内任何单元格的长度来确定实际长度。