+ Reply to Thread
Results 1 to 2 of 2

for-loop too slow

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-26-2018
    Location
    Vienna, Austria
    MS-Off Ver
    2016 professional
    Posts
    148

    for-loop too slow

    Dear Pros,


    I have a for loop for a task that a VLOOKUP would do. I just needed all the matches so the for loop. It served its task but it got too slow.
    Can somebody help me turning it into an Array-Macro?

    Sub suchenMitVBA()
    
    
    Dim lastrow As Long
    Dim LastRow2 As Long
    Dim i As Long
    Dim M As Long
    Dim Article As String
    
        With ActiveSheet
            '
            lastrow = ActiveWorkbook.Worksheets("Übertrag vom Suchfile").Cells(Rows.Count, 1).End(xlUp).Row
    
            LastRow2 = .Cells(Rows.Count, 1).End(xlUp).Row
            
            .Range("c2").EntireColumn.ClearContents
    
            'Outer loop to go through column E values
            For M = 2 To LastRow2
                Article = Range("a" & M).Value
                'Inner loop to run through column A values
                For i = 2 To lastrow
                    If VBA.StrComp(ActiveWorkbook.Worksheets("Übertrag vom Suchfile").Range("a" & i).Value, Article, vbTextCompare) = 0 Then
                        .Range("c" & M).Value = .Range("c" & M).Value & ActiveWorkbook.Worksheets("Übertrag vom Suchfile").Range("B" & i).Value
                    End If
    
                Next i
            Next M
        End With
    End Sub

    I also attached the file with a short description.


    Thank you indeed for your help!!


    Best regards

    Peter
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,147

    Re: for-loop too slow

    How about
    Sub peterschien()
       Dim Ary As Variant, Nary As Variant
       Dim i As Long
       Dim Dic As Object
       
       Set Dic = CreateObject("scripting.dictionary")
       Dic.comparemode = 1
       With Sheets("Übertrag vom Suchfile")
          Ary = .Range("A2:B" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
       End With
       For i = 1 To UBound(Ary)
          Dic(Ary(i, 1)) = Dic(Ary(i, 1)) & Ary(i, 2)
       Next i
       Ary = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value2
       ReDim Nary(1 To UBound(Ary), 1 To 1)
       For i = 1 To UBound(Ary)
          If Dic.exists(Ary(i, 1)) Then Nary(i, 1) = Dic(Ary(i, 1))
       Next i
       Range("C2").Resize(UBound(Nary)).Value = Nary
       
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Loop too slow
    By ngenova in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-28-2020, 01:46 PM
  2. Do while loop very slow?
    By Bundi999 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2018, 05:24 AM
  3. Loop running very slow
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-16-2017, 04:42 PM
  4. [SOLVED] VERY SLOW Do While Loop Macro
    By cocacrave in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-17-2015, 04:26 AM
  5. For each loop very slow
    By CassioGodinho in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2012, 04:50 PM
  6. Slow Do While Loop
    By bd528 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-10-2010, 02:22 PM
  7. How do I slow down my loop ?
    By svanni in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2006, 06:48 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