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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Ok how about
VBA Code:
Sub Demo03()
Dim Rng As Range, Ds As Range, n As Long, Dic As Object, Source As Variant
Dim Ary 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")
    Ary = .Range("c2", .Range("c" & Rows.Count).End(xlUp)).Value2
    ReDim Nary(1 To UBound(Ary), 1 To 3)
    For n = 1 To UBound(Ary)
        If Dic.Exists(Ary(n, 1)) Then
            Nary(n, 1) = Source(Dic(Ary(n, 1)), 1)
            Nary(n, 2) = Source(Dic(Ary(n, 1)), 3)
        End If
    Next n
    Range("D2").Resize(UBound(Nary), 3).Value = Nary
End With


Application.ScreenUpdating = True

Debug.Print "Demo-03????:" & amp; amp; endTime - startTime
Debug.Print "It's done in: " & Timer - t & " seconds"
End Sub
 
Upvote 0
Solution
How many columns of data are you trying to return per row, not how many rows.

i have an error. in the line of code below
VBA Code:
Nary(n, 2) = Source(Dic(Ary(n, 1)), 3)
 

Attachments

  • error14092021.JPG
    error14092021.JPG
    15.7 KB · Views: 7
Upvote 0
i have an error. in the line of code below
VBA Code:
Nary(n, 2) = Source(Dic(Ary(n, 1)), 3)
my code below is wrong
VBA Code:
  Ds.Offset(, 1) = Source(Dic(Ds.Value), 1)
this is true
VBA Code:
Ds.Offset(, 1) = Source(Dic(Ds.Value), 2)
 
Upvote 0
Missed the fact that your source array only has two columns, try
VBA Code:
Source = .Range("h1").CurrentRegion.Resize(, 4)
 
Upvote 0
Missed the fact that your source array only has two columns, try
VBA Code:
Source = .Range("h1").CurrentRegion.Resize(, 4)
ok. I've coded from you below, I show a screenshot of the results of the time.
Can the time result be reduced to lower again from the current result?
 

Attachments

  • result time-1.JPG
    result time-1.JPG
    21.5 KB · Views: 13
Upvote 0
What's wrong with 10 seconds?
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,726
Members
449,465
Latest member
TAKLAM

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