Results 1 to 15 of 15

How to remove one additional loop

Threaded View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    How to remove one additional loop

    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
    Attached Files Attached Files
    Last edited by jaryszek; 02-15-2018 at 08:17 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Modfication of Existing Code for additional additional cells
    By thilag in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-29-2016, 03:00 AM
  2. Remove Additional unwanted Rows
    By wysbob in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-25-2015, 11:04 AM
  3. [SOLVED] VBA Need help with additional loop to copy range to different worksheet
    By Old-One in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-14-2014, 05:19 PM
  4. Replies: 1
    Last Post: 02-06-2014, 03:00 PM
  5. Remove characters using Loop?
    By noidea_4 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 11-20-2012, 06:43 PM
  6. How do I delete or remove additional blank pages of a worksheet?
    By Kim in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-27-2005, 04:05 PM
  7. [SOLVED] sort a list/remove additional entries
    By Glowinafuse in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-23-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1