在循环中走出界限?

时间:2017-04-18 17:05:27

标签: vba excel-vba excel

显然我在阵列上越界了。每当我尝试读取一列时,我都会得到“下标超出范围错误”。试图调试它并在另一列中运行它绝对有效,但不是在这个特定的。任何提示?

@total_ordering
class part():
    [...]

    def __lt__(self, other):
        return self.number < other.number.

2 lbound和ubound的原因是因为有时字符串的长度不同而我只想说最后一个字。但它突破了那个特定的空间。我猜这是因为我没有指向正确的细胞?

谢谢!

1 个答案:

答案 0 :(得分:0)

我经历了一些被破坏的东西。每当你声明变量时:

Dim SomeVariable1, SomeVariable2, SomeVariable3 as String

好像在说:

Dim SomeVariable1 as Variant, SomeVariable2 as Variant, SomeVariable3 as String

列出同一行上的所有声明只会阻止您重复范围,而不是类型。

另外,你做了很多:

Cells(f, "K").Value

但是这个引用是不合格的,即使它在With块内。在这些之前加上一段时间来限定他们。

最后,定义应该为您的函数预期的变量类型。

CheckFirstLetter(mystring, text, indexCurp, index) As Boolean

相同
CheckFirstLetter(mystring as Variant, text as Variant, indexCurp as Variant, index as Variant) As Boolean

这意味着我可以通过我想要的任何东西,并且不会出现问题。我怀疑子例程中的所有变体都是问题。请参阅下面的固定代码:

Private Sub Form_Load()     Dim curp As String     Dim fname As String     Dim lname1 As String     Dim lname2 As String     昏暗的性别为字符串

Dim i As Long
Dim pos As Long
Dim asciinum As String
Dim f As Long ' Why jump back to f if you have i declared? It doesnt make a difference, but usually the next logical choice is j
Dim validar As Boolean
Dim fechaNac As Date

With Worksheets("sheet1")
    For f = 2 To Cells(.Rows.Count, "L").End(xlUp).Row
        ' Be sure to qualify ALL references.
        curp = .Cells(f, "L").Value2
        fname = .Cells(f, "F").Value2
        lname1 = .Cells(f, "I").Value2
        lname2 = .Cells(f, "J").Value
        gender = .Cells(f, "k").Value2
        fechaNac = .Cells(f, "P").Value2
' works           validar = CheckFirstLetter(lname1, curp, 1, f)
' works           validar = CheckFirstVowel(lname1, curp, 2, f)
             validar = CheckFirstLetter(lname2, curp, 3, f)
'            validar = CheckFirstLetter(fname, curp, 4)
'            validar = CheckDate(fechaNac, curp, 5)
'            validar = CheckGender(gender, curp, 11)
'            validar = CheckConsonant(lname1, curp, 12, 2)
'            validar = CheckConsonant(lname2, curp, 13, 2)
'            pos = posVowel(fname)
'            validar = CheckConsonant(fname, curp, 14, pos)
            If (validar = True) Then
                .Cells(f, "N") = "Valido"
            Else
                .Cells(f, "N") = "No Valido"
            End If
        Next
    End With
End Sub

Function CheckFirstLetter(mystring As String, text As String, indexCurp As Long, index As Long) As Boolean
            Dim i As Long

            Dim ary As Variant
            ary = Split(mystring, " ")

            Dim vocal As String
            vocal = LCase(ary(LBound(ary)))   'Breaks in this line

            Dim vocal2 As String
            vocal2 = LCase(ary(UBound(ary)))

            If (vocal = "de" Or vocal = "del") Then
                vocal = vocal2
            End If

            Dim outStr As String
            outStr = LCase(Mid(text, indexCurp, 1))

            Dim asciinum As String
            asciinum = LCase(Mid(vocal, 1, 1))

            ' Qualify these cell references
            Cells(index, "M") = vocal
            Cells(index, "O") = vocal2

            ' I dont know if the parentheses are needed here, I just like them
            CheckFirstLetter = (asciinum = outStr)

'            If (asciinum = outStr) Then
'                CheckFirstLetter = True
'            Else
'                CheckFirstLetter = False
'            End If
End Function

最后,不要尝试使用if块:

If True = True Then: Debug.Print True
Else: Debug.Print False
End If

保存两行,这使得读取和调试代码变得更加困难。您还可以直接将比较的布尔结果分配给布尔变量。这两项工作都是:

Result = True = False
Result = (True = False)