Cambiar la estructura de la variable para evitar la estructura variable()()

When I run this code if I get a match instead of putting it into jackal(1) or jackal(2) it puts it in Jackal(1)(0) or Jackal(2)(0). How can I rewrite this code so that the values are placed into jackal(1) directly? Maybe its the syntax of my filter function?

Sub cmov2()
'This macro is designed to sniff out multiple selection incompatibilities; specifically     if you choose a L/R Monitor Arm with L/R Swing Light it will Warn.
'Code Section#1: Find if any of the following are on the order EDS-3090, BDS-2530, or     BDS-2589

Dim valid() As String
ReDim valid(1 To 3)
valid(1) = "EDS-3090"
valid(2) = "BDS-2530"
valid(3) = "BDS-2589"
Sheets("Config").Columns("B:B").Select
Selection.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Activate
 ActiveCell.Select
 rowq = ActiveCell.row
 Selection.End(xlDown).Select
 rowp = ActiveCell.row
 Range("F" & rowq).Select
 Dim Stern() As String
 ReDim Stern(1 To rowp - rowq)
 zea = 1
 Do
 Stern(zea) = Selection.Value
 Selection.Offset(1, 0).Select
 zea = zea + 1
 Loop Until zea = (rowp - rowq)
 Dim quack As Integer
 quack = 1
  Dim jackal() As Variant
 ReDim jackal(1 To 3)
 Do
 Stop
 zee = Filter(Stern(), valid(quack))
 jackal(quack) = z
 quack = quack + 1
 Loop Until quack = 3
' This code creates the wrong structure for this variable i get jackal(1)(0) and things        'like that. Would prefer to check jackal( 1 to end) for <> nullstring
 If jackal(1)(0) = vbNullString Then
    'change to y=1 do if jackal(y)<>vbnullstring then
    'msgbox "warning"

 Exit Sub
 Else
 MsgBox "Warning: You have a selection with two swingarms that are on the same radius and cannot swing past one another " & Chr$(13) & " Choose Okay if you still wish to proceed otherwise choose Cancel to revise your order", vbOKCancel
 End If
End Sub

preguntado el 09 de septiembre de 13 a las 21:09

A good start would be to add Option Explicit and then declare all your variables. Also, it's much easier to follow code if you use variable names which are descriptive, and which aren't too similar to each other (z, zee and zea?) Basically though, it seems like you're creating an array of arrays (since Filter() returns an array), so I'm not sure what other syntax you'd expect: if jackal(1) is an array then how else would you access its contents? -

Debería considerar usar un Dictionary object to store your order list. You can then use .exists to test for validation trigger items. -

What is an order list? -

2 Respuestas

Así es como lo haría:

Sub cmov2()


    Dim valid, i
    Dim rng As Range, f As Range, rngProb As Range

    valid = Array("EDS-3090", "BDS-2530", "BDS-2589")

    'what is the purpose of Find() here?
    Set f = Sheets("Config").Columns("B:B").Find(what:="1", After:=ActiveCell, _
                     LookIn:=xlFormulas, lookat:=xlPart, MatchCase:=False)

    If f Is Nothing Then Exit Sub 'not found - could this happen?

    Set rng = f.Parent.Range(f, f.End(xlDown))

    For i = LBound(valid) To UBound(valid)

        Set f = rng.Find(what:=valid(i), LookIn:=xlValues, lookat:=xlWhole)
        If Not f Is Nothing Then
            If rngProb Is Nothing Then
                Set rngProb = f
            Else
                Set rngProb = Application.Union(rngProb, f)
            End If
        End If

    Next i

    If Not rngProb Is Nothing Then
        'Msgbox "..."                  'warn about problem
        rngProb.Parent.Activate        'show the sheet
        rngProb.Interior.Color = vbRed 'highlight problem values
    End If

End Sub

Respondido el 10 de Septiembre de 13 a las 15:09

I have started using the syntax of valid=array(element 1, element 2, element 3)... much easier to organize. - Préstamo de bolsillo

I took your Advice Tim here's what I got :
I am stuck at the filter function with a type mismatch error. I don't understand why.

Option Explicit
Sub cmov2()
'This macro is designed to sniff out multiple selection incompatibilities; specifically    if you choose a L/R Monitor Arm with L/R Swing Light it will Warn.
'Code Section#1: Find if any of the following are on the order EDS-3090, BDS-2530, or BDS-2589

Dim valid() As String
ReDim valid(1 To 3)
Dim rowq As Integer
Dim rowp As Integer
Dim counter As Integer
Dim compare As String
 Dim quack As Integer
valid(1) = "EDS-3090"
valid(2) = "BDS-2530"
valid(3) = "BDS-2589"
 Sheets("Config").Columns("B:B").Select 
 Selection.Find(What:="1", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Activate
 ActiveCell.Select
 rowq = ActiveCell.row
 Selection.End(xlDown).Select
 rowp = ActiveCell.row
 Range("F" & rowq).Select
 Dim Stern() As String
 ReDim Stern(1 To rowp - rowq)
 counter = 1
 Do
 Stern(counter) = Selection.Value
 Selection.Offset(1, 0).Select
 counter = counter + 1
 Loop Until counter = (rowp - rowq)
 quack = 1
  Dim jackal As String

    Do
 Stop
 compare = Filter(Stern(), valid(quack), True)
 quack = quack + 1
 Loop Until quack = 3
 If jackal = vbNullString Then
 Exit Sub
 Else
 MsgBox "Warning: You have a selection with two swingarms that are on the same radius and cannot swing past one another " & Chr$(13) & " Choose Okay if you still wish to proceed otherwise choose Cancel to revise your order", vbOKCancel
 End If
End Sub

contestado el 10 de mayo de 15 a las 10:05

Filter() returns an array, so you can't assign its return value to a string variable: using Application.Index() or Application.Match() might be easier here. You repeatedly declare jackal in your Do loop, but never asign it a value. Maybe it would be best if you just describe exactly what you want to acheive with your code: it's not easy to figure out just from reading it. - tim williams

Warn users if there are elements in stern() that match elements on valid(). For instance, if Stern (1)= "BDS-2589" Stern(2) through Stern(4) are equal to "Q-5000" and Stern(5)="EDS-3090" then I would like an array to populate as such : arraymatches(1)="BDS-2589" and arraymatches(2)="EDS-3090" - Préstamo de bolsillo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.