Hi Guys,
i have code:
Option Explicit
Sub test()
Dim ArrayLoop, ArrSource, ArrLoopNum As Variant
Dim DicLoop, DicSource As Object
Dim i, NumberLoop As Long
Dim VarLoop, LandscapeVar As String
Dim varkeytop As Variant
Dim SystemPos, SystemPos2 As Long
Set DicLoop = CreateObject("Scripting.dictionary")
Set DicSource = CreateObject("Scripting.dictionary")
'' Range for t_loop
With Worksheets("t_loop")
ArrayLoop = .Range("t_loop[#Data]")
For i = 1 To UBound(ArrayLoop)
If Not DicLoop.exists(ArrayLoop(i, 1)) Then
VarLoop = checkArrayLength(ArrayLoop, i)
DicLoop.Add ArrayLoop(i, 1), VarLoop
Else
MsgBox "You cannot have duplicates"
Exit Sub
End If
Next i
End With
'' Range for t_source
With Worksheets("t_source")
ArrSource = .Range("t_source[#Data]")
For i = 1 To UBound(ArrSource)
If Not DicSource.exists(ArrSource(i, 1)) Then
DicSource.Add ArrSource(i, 1), Join(Array(ArrSource(i, 2), ArrSource(i, 3)), Chr(2))
Else
MsgBox "You cannot have duplicates"
Exit Sub
End If
Next i
End With
''We have 3 cases here: System, Object or Method.
For Each varkeytop In DicLoop
ArrLoopNum = Split(DicLoop(varkeytop), Chr(2))
NumberLoop = UBound(ArrLoopNum)
' I am wondering how to avoid loop here, to loop only through row variables within t_loop table and bases on that - retrive data from table t_source
For i = 1 To UBound(ArrSource)
LandscapeVar = Split(DicSource(ArrSource(i, 1)), Chr(2))(0)
If InStr(DicLoop(varkeytop), LandscapeVar) > 0 Then
y = y + 1
SystemPos = InStr(DicLoop(varkeytop), LandscapeVar)
SystemPos2 = InStr(SystemPos, DicLoop(varkeytop), Chr(2))
SystemVar = Mid(DicLoop(varkeytop), SystemPos, SystemPos2 - SystemPos)
''do things for SystemVar
Else
'do nothing
End If
If y = NumberLoop Then
Exit For
End If
Next i
y = 0
Next varkeytop
End Sub
and function:
Option Explicit
Function checkArrayLength(ArrayLoop, number)
Dim y As Long
Dim r, c, i As Long, MyString As Variant
Dim MyDelimiter As String
MyDelimiter = Chr(2)
For c = 2 To UBound(ArrayLoop, 2)
If Len(ArrayLoop(number, c)) > 1 Then
MyString = MyString & MyDelimiter & Trim(ArrayLoop(number, c))
End If
Next c
MyString = MyString & Chr(2)
checkArrayLength = Mid(MyString, Len(MyDelimiter) + 1)
End Function
what i am doing here is creating dictionary for t_loop table.
For each row in this table i will be creating new workbook with data.
Next i am creating dictionary for t_cource - this is my dictionary data.
And problem is within looping within my dictionary (From for each vakeytop in DicLoop).
For each variable in t_source Atributes column i am searching for matching string within each row in t_loop table.
(If InStr(DicLoop(varkeytop), LandscapeVar) > 0 Then)
example:
For system in second column in t_source table i will check this if:
If InStr(DicLoop(varkeytop), LandscapeVar) > 0 Then, --> here LandscapeVar is "System" so if statement will go further and find which system number should be taken (SystemVar)
For object i will check this if also, and for Method i will check this if also.
It is ok for ID1 in t_loop because i have 3 variables there inputed by user.
Problem is with ID2 where there are only 2 variables: Object1 and System1.
Here i want only run my if statement for System and Object from table t_source.
I do not want to loop unnecessarily also for Method value here...
How can i do it?
Maybe it is better way then looping always for each value in t_source table?
Please help,
Jacek
Bookmarks