2017-04-18 67 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)