need help because vba code is running slow

roykana

Active Member
Joined
Mar 8, 2018
Messages
311
Office Version
  1. 2010
Platform
  1. Windows
Dear all Master,
vba code runs slow for 100000 row records even though I'm using vba code with dictionaries and arrays. Please solve.
and also why the code run time result is different and I attached a screenshot of the time result.


thanks
roykana

VBA Code:
Sub Demo03()
Dim Rng As Range, Ds As Range, n As Long, Dic As Object, Source As Variant
Dim startTime As Double
Dim endTime As Double
Dim t
t = Timer
endTime = Timer
'Dim startTime As Double
'Dim endTime As Double
Application.ScreenUpdating = False
With Sheets("data")
    Source = .Range("h1").CurrentRegion.Resize(, 2)
End With
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
For n = 2 To UBound(Source, 1)
    Dic(Source(n, 1)) = n
Next
With Sheets("data")
    Set Rng = .Range(.Range("c2"), .Range("c" & Rows.Count).End(xlUp))
    For Each Ds In Rng
        If Dic.Exists(Ds.Value) Then
            Ds.Offset(, 1) = Source(Dic(Ds.Value), 1)
'            Ds.Offset(, 3) = source(Dic(Ds.Value), 3)
        End If
    Next Ds
End With


Application.ScreenUpdating = True

Debug.Print "Demo-03????:" & amp; amp; endTime - startTime
Debug.Print "It's done in: " & Timer - t & " seconds"
End Sub
 

Attachments

  • result time.JPG
    result time.JPG
    21.3 KB · Views: 19

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
VBA Code:
With Sheets("data")
    Set Rng = .Range(.Range("c2"), .Range("c" & Rows.Count).End(xlUp))
    For Each Ds In Rng
        If Dic.Exists(Ds.Value) Then
            Ds.Offset(, 1) = Source(Dic(Ds.Value), 1)
'            Ds.Offset(, 3) = source(Dic(Ds.Value), 3)
        End If
    Next Ds
This loop is going to be slow because is accesses the worksheet multiple times in a loop. change this to using an array.
 
Upvote 0
VBA Code:
With Sheets("data")
    Set Rng = .Range(.Range("c2"), .Range("c" & Rows.Count).End(xlUp))
    For Each Ds In Rng
        If Dic.Exists(Ds.Value) Then
            Ds.Offset(, 1) = Source(Dic(Ds.Value), 1)
'            Ds.Offset(, 3) = source(Dic(Ds.Value), 3)
        End If
    Next Ds
This loop is going to be slow because is accesses the worksheet multiple times in a loop. change this to using an array.
dear mr. offthelip
what is the code solution? Please help me.
Thanks
roykana
 
Upvote 0
Are you just trying to return one value from Source, or multiple values?
 
Upvote 0
I mean how many values per row are you trying to return?
 
Upvote 0
How many columns of data are you trying to return per row, not how many rows.
 
Upvote 0

Forum statistics

Threads
1,215,438
Messages
6,124,873
Members
449,192
Latest member
MoonDancer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top