Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:08:25+00:00 2026-06-01T08:08:25+00:00

I have some code that loops through a series of sheets in a workbook

  • 0

I have some code that loops through a series of sheets in a workbook and tries to find a match to a value in another sheet.

Private Sub MatchData(NewMIARep As Worksheet, MaxRow As Long, wkbFinalized As Workbook)
Dim wksFinalized As Worksheet
Dim lCount As Long
Dim lFinMaxRow As Long
Dim DataRange As Variant
Dim SearchRange As Variant
Dim FoundRange As Range

Application.Calculation = xlCalculationManual

With NewMIARep

    DataRange = .Range("J2:K" & MaxRow)
    SearchRange = .Range("A2:A" & MaxRow)

    For Each wksFinalized In wkbFinalized.Sheets
        lFinMaxRow = GetMaxRow(wksFinalized)
        If lFinMaxRow > 1 Then
            For lCount = 1 To MaxRow - 1
                If Len(DataRange(lCount, 1)) = 0 And Len(DataRange(lCount, 2)) = 0 Then
                    Set FoundRange = wksFinalized.Range("A2:A" & lFinMaxRow).Find(What:=SearchRange(lCount, 1), _
                        LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False, SearchFormat:=False)
                    If Not FoundRange Is Nothing Then
                        DataRange(lCount, 1) = FoundRange.Offset(ColumnOffset:=12).Value
                        DataRange(lCount, 2) = FoundRange.Offset(ColumnOffset:=2).Value
                        Set FoundRange = Nothing
                    End If
                End If
            Next lCount
        End If
    Next wksFinalized

.Range("J2:K" & MaxRow).Value = DataRange
.Range("J2:J" & MaxRow).NumberFormat = "mm/dd/yyyy"

End With

Application.Calculation = xlCalculationAutomatic

As this goes through every sheet in wkbFinalized, and each sheet has 30,000-60,000 or so records, and I loop another 5,000-6,000 times within that loop for each of the items I want to search for, this tends to slow down quite a bit (not the fastest machine in the world, but I have no choice in the matter).

I know I can’t do this specifically, but I’m looking for a function that will work like
wkbFinalized.Find(...)
vs.
wkbFinalized.Sheets(n).Find(...).

Does such a function exist?

OR Is there a way to somehow preload all the data from all sheets into one range before searching, so that the internal loop only runs once? (and would this be any more or less efficient?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:08:26+00:00Added an answer on June 1, 2026 at 8:08 am

    This was easier than I thought. I just needed to find the right muse, I suppose. This doesn’t directly address searching when duplicates exist, but for my case, each search term was unique across all worksheets, so this did work.

    Private Sub MatchData(NewMIARep As Worksheet, MaxRow As Long, wkbFinalized As Workbook)
    
    Dim wksFinalized As Worksheet
    Dim lCount As Long
    Dim lFinMaxRow As Long
    Dim DataRange As Variant
    Dim SearchRange As Variant
    Dim FoundRange As Range
    Dim FindRange As Range
    Dim colBill As New Collection
    Dim colDate As New Collection
    
        Application.Calculation = xlCalculationManual
    
        With NewMIARep
    
            DataRange = .Range("J2:K" & MaxRow)
            SearchRange = .Range("A2:A" & MaxRow)
    
            For Each wksFinalized In wkbFinalized.Sheets
                lFinMaxRow = GetMaxRow(wksFinalized)
                If lFinMaxRow > 1 Then
    
                    Set FindRange = wksFinalized.Range("A2:M" & lFinMaxRow)
    
                    For lCount = 1 To lFinMaxRow - 1
                        ' Keep one collection per item to pull from in search.
                        ' This can be expanded to one collection for each column you want to search.
                        ' I chose to use the direct value, but I suppose you could also grab the column(/number) or row number, 
                        ' or anything else about the cell found to use as a reference instead.
                        ' Do this for all sheets BEFORE doing the lookups to avoid extra looping.
                        If Not InCollection(colBill, FindRange(lCount, 1).value) Then
                            colBill.Add FindRange(lCount, 3).value, FindRange(lCount, 1).value
                            colDate.Add FindRange(lCount, 13).value, FindRange(lCount, 1).value
                        End If
    
                    Next lCount
                End If
            Next wksFinalized
    
    
            For lCount = 1 To MaxRow - 1
                If Len(DataRange(lCount, 1)) = 0 And Len(DataRange(lCount, 2)) = 0 Then
                    If InCollection(colBill, CStr(SearchRange(lCount, 1))) Then
                        ' For each search term, if we have a match in our previously created collections,
                        ' then it exists somewhere in the source workbook, but we don't care on which sheet it resides.
                        ' Simply pull the value from each collection that matches the key of the search term.
                        DataRange(lCount, 1) = colDate.item(CStr(SearchRange(lCount, 1)))
                        DataRange(lCount, 2) = colBill.item(CStr(SearchRange(lCount, 1)))
                    End If
                End If
            Next lCount
    
            .Range("J2:K" & MaxRow).value = DataRange
            .Range("J2:J" & MaxRow).NumberFormat = "mm/dd/yyyy"
    
        End With
    
        Application.Calculation = xlCalculationAutomatic
    
    End Sub
    
    'The InCollection function was pulled from some other source online. 
    'It is not my own creation.
    
    Public Function InCollection(ColToCheck As Collection, KeyToCheck As String) As Boolean
    
    Dim vTemp As Variant
    Dim errNumber As Long
    
        InCollection = False
    
        Set vTemp = Nothing
        Err.Clear
    
        On Error Resume Next
        vTemp = ColToCheck.item(KeyToCheck)
    
        InCollection = (CLng(Err.Number) <> 5)
        On Error GoTo 0    '5 is not in, 0 and 438 represent incollection
    
        Err.Clear
    
        Set vTemp = Nothing
    
    End Function
    

    This runs in much less time than the original version.

    Here’s the same as above, but using Scripting.Dictionary objects instead, eliminating the need for the second function (InCollection):

    Private Sub MatchData(NewMIARep As Worksheet, MaxRow As Long, wkbFinalized As Workbook)
    
    Dim wksFinalized As Worksheet
    Dim lCount As Long
    Dim lFinMaxRow As Long
    Dim DataRange As Variant
    Dim SearchRange As Variant
    Dim FoundRange As Range
    Dim FindRange As Range
    Dim dictBill As Object
    Dim dictDate As Object
    
    
        Application.Calculation = xlCalculationManual
    
        Set dictBill = CreateObject("Scripting.Dictionary")
        Set dictDate = CreateObject("Scripting.Dictionary")
    
        With NewMIARep
    
            DataRange = .Range("J2:K" & MaxRow)
            SearchRange = .Range("A2:A" & MaxRow)
    
            For Each wksFinalized In wkbFinalized.Sheets
                lFinMaxRow = GetMaxRow(wksFinalized)
                If lFinMaxRow > 1 Then
    
                    Set FindRange = wksFinalized.Range("A2:M" & lFinMaxRow)
    
                    For lCount = 1 To lFinMaxRow - 1
                        ' Keep one collection per item to pull from in search.
                        ' This can be expanded to one collection for each column you want to search.
                        ' I chose to use the direct value, but I suppose you could also grab the column(/number) or row number,
                        ' or anything else about the cell found to use as a reference instead.
                        ' Do this for all sheets BEFORE doing the lookups to avoid extra looping.
                        If Not dictBill.Exists(FindRange(lCount, 1).Value) Then
                            dictBill.Add FindRange(lCount, 1).Value, FindRange(lCount, 3).Value
                            dictDate.Add FindRange(lCount, 1).Value, FindRange(lCount, 13).Value
                        End If
    
                    Next lCount
                End If
            Next wksFinalized
    
    
            For lCount = 1 To MaxRow - 1
                If Len(DataRange(lCount, 1)) = 0 And Len(DataRange(lCount, 2)) = 0 Then
                    If Not dictBill.Exists(CStr(SearchRange(lCount, 1))) Then
                        ' For each search term, if we have a match in our previously created collections,
                        ' then it exists somewhere in the source workbook, but we don't care on which sheet it resides.
                        ' Simply pull the value from each collection that matches the key of the search term.
                        DataRange(lCount, 1) = dictDate.Item(CStr(SearchRange(lCount, 1)))
                        DataRange(lCount, 2) = dictBill.Item(CStr(SearchRange(lCount, 1)))
                    End If
                End If
            Next lCount
    
            .Range("J2:K" & MaxRow).Value = DataRange
            .Range("J2:J" & MaxRow).NumberFormat = "mm/dd/yyyy"
    
        End With
    
        Application.Calculation = xlCalculationAutomatic
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some image processing code that loops through 2 multi-dimensional byte arrays (of
I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
I have code already that loops through each member of my website and can
i have this code below that loops through a data structure builds up a
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that looks like: static const std::string and( AND ); This
I have some code that looks like this. There is also an autoincrement field
So I have some code that looks like this: <asp:BoundField DataField=CreatedOn HeaderText=Created on SortExpression=CreatedOn
In VS 2005, I have some code that looks like this: ifs.open(foo); while (!ifs.eof())
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.