2014-07-15 2 views
0

J'essaie de vérifier si la chaîne dans la cellule actuelle correspond à une chaîne entière trouvée dans le tableau, mais pour quelque raison la fonction jette toujours une erreur (im devinant) parce que le "If Not IsError (Application.Match (v, arr, 0)) Alors ... "Toujours sauté.VBA - Créer une fonction personnalisée pour vérifier si la chaîne est contenue dans un tableau?

Voici le code que j'ai pour le moment pour la fonction et la méthode qui l'appelle.

Function IsInArray(v As String, arr As Variant) As Boolean 

    If Not IsError(Application.Match(v, arr, 0)) Then 
     IsInArray = True 

    Else 
     IsInArray = False 

    End If 

End Function 


'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------' 
'--- LABOR WORKSHEET -- LABOR WORKSHEET -- LABOR WORKSHEET -- LABOR WORKSHEET -- LABOR WORKSHEET -- LABOR WORKSHEET -- LABOR WORKSHEET -- LABOR WORKSHEET -- LABOR WORKSHEET -- LABOR WORKSHEET --' 
'-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------' 
Private Sub LaborTab_Setup(ByVal SheetNameString As String, ByVal JPNum As String, _ 
          ByVal OID As String, ByVal SID As String) 

    Dim PasteRow As Variant 
    Dim LaborListString As String 
    Dim MainRange As Range, Cell As Excel.Range 
    Dim MainSheet As String, LaborCraftArray() As String 
    Dim JobPlan_Number As String, OrgID As String, SiteID As String 

    JobPlan_Number = JPNum 
    OrgID = OID 
    SiteID = SID 
    MainSheet = SheetNameString 

    'Lists all of the possible LABOR Craft Codes in a single string. 
    'Seperated by only "," and no spaces! 
    LaborListString = "OPER,INST,SERV,PAINT,SANDB,ELEC,BOILM," & _ 
      "SCAFF,WELD,RIGGER,INSUL,CATLYS,REFRA,ENG,QAQC,WATCHF,INSP,PIPEF," & _ 
      "MECH,XRAY,RVTECH,HYPRO,MACH,PIPEF" 

    'Splits the string of Labor Crafts assigning each Craft Code to a spot within an array. 
    LaborCraftArray = Split(LaborListString, ",") 

    Sheets(MainSheet).Activate 
    Range("D2").Select 

    'Sets Range for Worksheet containing ALL information 
    Set MainRange = ActiveSheet.Range(Selection, Selection.End(xlDown)) 

    'Keeps Track of what line to paste info in 
    'Does not increase if the Craft was not found in the array 
    PasteRow = 2 

    For Each Cell In MainRange 
     If IsInArray(Cell.Value, LaborCraftArray) Then 


      'JOBLABOR.JPTASK 
       Sheets(MainSheet).Select 
       Range("A" & Cell.Row).Select 
       Selection.Copy 


      'Updates the PasteRow variables value in order to paste the copied information to the correct line 
       PasteRow = PasteRow + 1 

     End If 

    Next Cell 

End Sub 

Répondre

0

tout d'abord considérer s'il vous plaît supprimer tous les si elles ne sont pas nécessaires .select et .activate, car ils pourraient toujours conduire à des problèmes. Aussi pourquoi ne pas simplifier la fonction de recherche:

Function IsInArray(v As String, arr As Variant) As Boolean 

Dim Check As Boolean 

For i = LBound(arr) to UBound(arr) 
    If v = arr(i) Then 
     Check = True 
     Exit For 
    End If 
Next 

IsInArray = Bool 

End Function 

erreurs de manipulation est le plus souvent frustrant et pourrait conduire à des problèmes

Questions connexes