2017-04-18 64 views
0

显然我要走出我的阵列边界。每次尝试读取列时,都会出现“下标超出范围错误”。试图调试它,并在另一列运行它,它绝对有效,但不是在这个特定的。任何提示?走出循环的界限?

Private Sub Form_Load() 
    Dim curp, fname, lname1, lname2, gender As String, i, pos As Long, asciinum As String, f As Long, validar As Boolean, fechaNac As Date 

    With Worksheets("sheet1") 
     For f = 2 To Cells(.Rows.Count, "L").End(xlUp).Row 
      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 f 
    End With 
End Sub 

Function CheckFirstLetter(mystring, text, indexCurp, index) As Boolean 
      Dim outStr, asciinum, vocal, vocal2 As String, ary As Variant, i As Long 
      ary = Split(mystring, " ") 
      vocal = LCase(ary(LBound(ary))) Breaks in this line 
      vocal2 = LCase(ary(UBound(ary))) 
      If (vocal = "de" Or vocal = "del") Then 
      vocal = vocal2 
      End If 
      outStr = LCase(Mid(text, indexCurp, 1)) 
      asciinum = LCase(Mid(vocal, 1, 1)) 
      Cells(index, "M") = vocal 
      Cells(index, "O") = vocal2 
      If (asciinum = outStr) Then 
       CheckFirstLetter = True 
       Else: CheckFirstLetter = False 
       End If 
End Function 

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

谢谢!

回答

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 

,这意味着我可以通过任何我想要的,而且不会是一个问题。我怀疑你的子程序中的所有变体都是问题。请参见下面的固定码:

私人小组的Form_Load() 昏暗curp作为字符串 昏暗FNAME作为字符串 昏暗lname1作为字符串 昏暗lname2作为字符串 昏暗性别作为字符串

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 

最后,唐“T尽量可爱的你,如果块:

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

为您节省两行,这使得它更难以阅读和调试代码。您也可以直接将比较的布尔结果分配给布尔变量。这两项工作:

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