How to make VBA code excute time shorter and how to get correct output for mismatch condition for VBA code?

Sherli

New Member
Joined
Apr 27, 2020
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
Hi, everyone. I'm quite new in VBA. I have face 2 problem after I run my code. I have 2000+ row in my worksheet but the pic that I attach, below just a piece of my data.

(1) My code 1 is work but it take around 10 minutes. May I know is it got other way to make it run faster?
This is my code 1:
VBA Code:
'Code 1
Sub FillSAintoraw()
    Dim wb2 As Workbook
    Dim A As String, ASp1 As String
    Dim xA As Long, yA As Long
    
    Set wb2 = Workbooks("jj")
    
    A = wb2.Worksheets("A1").Range("A" & Rows.Count).End(xlUp).Row
    ASp1 = wb2.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For xA = 2 To A
        COM = wb2.Worksheets("A1").Range("A" & xA) & wb2.Worksheets("A1").Range("B" & xA)
        For yA = 2 To ASp1
            If COM = wb2.Worksheets("Sheet1").Range("A" & yA) & wb2.Worksheets("Sheet1").Range("B" & yA) Then
                wb2.Worksheets("A1").Range("D" & xA).Value = wb2.Worksheets("Sheet1").Range("D" & yA)
            Exit For
            End If
        Next yA
    Next xA
End Sub
Output of code 1
upload.PNG


(2) My code 2 is work too but the output is incorrect. The output of code 2 should be same with the output as code 1. May I know how can I solve it?
This is my code 2:
VBA Code:
[/B]
'Code 2
Sub FillSAintoraw()
    Dim wb2 As Workbook
    Dim A As String, ASp1 As String
    Dim xA As Long, yA As Long
    
    Set wb2 = Workbooks("jj")
    
    A = wb2.Worksheets("A1").Range("A" & Rows.Count).End(xlUp).Row
    ASp1 = wb2.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For xA = 2 To A
        COM = wb2.Worksheets("A1").Range("A" & xA) & wb2.Worksheets("A1").Range("B" & xA) & wb2.Worksheets("A1").Range("D" & xA)
        For yA = 2 To ASp1
            If COM <> wb2.Worksheets("Sheet1").Range("A" & yA) & wb2.Worksheets("Sheet1").Range("B" & yA) & wb2.Worksheets("Sheet1").Range("D" & yA) Then
                wb2.Worksheets("A1").Range("D" & xA).Value = wb2.Worksheets("Sheet1").Range("D" & yA)
            Exit For
            End If
        Next yA
    Next xA
End Sub
[B]
Output of code 2
Upload code 2.PNG

Thanks for the help.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
It's always nice to see that people did try themselves. Kudos to you.
Just explain in a concise manner without leaving anything out what you want to achieve

BTW, you use "Dim" but you don't have "Option Explicit" at the top I suspect.
 
Upvote 0
How about
VBA Code:
Sub Sherli()
   Dim Ary As Variant
   Dim i As Long
   Dim Dic As Object
   Dim Txt As String
   
   Set Dic = CreateObject("scripting.dictionary")
   With Workbooks("JJ").Sheets("Sheet1")
      Ary = .Range("A2:D" & .Range("A" & Rows.Count).End(xlUp).Row)
   End With
   For i = 1 To UBound(Ary)
      Txt = Ary(i, 1) & "|" & Ary(i, 2)
      Dic(Txt) = Ary(i, 4)
   Next i
   With Workbooks("JJ").Sheets("A1")
      Ary = .Range("A2:D" & .Range("A" & Rows.Count).End(xlUp).Row)
      For i = 1 To UBound(Ary)
         Txt = Ary(i, 1) & "|" & Ary(i, 2)
         If Dic.Exists(Txt) Then Ary(i, 4) = Dic(Txt)
      Next i
      .Range("A2").Resize(UBound(Ary), 4).Value = Ary
   End With
End Sub
 
Upvote 0
The code is work nicely. Really thanks for your help, Fluff. :)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,956
Latest member
JPav

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