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 8367579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:11:45+00:00 2026-06-09T13:11:45+00:00

I need to create a macro (or function) to copy cells from an adjacent

  • 0

I need to create a macro (or function) to copy cells from an adjacent worksheet to the current worksheet if they meet certain criteria.

Below is the worksheet adjacent to the current worksheet that contains the Owner, Ticket, and Comments fields. I need to copy those fields to the appropriate application name and object (Concatenated as a unique ID) in the current worksheet.

enter image description here

Below is the current worksheet that I need to copy the above fields to. Notice that the applications are not listed in the same order. This will be the case as I never know which order the data will be in or if the same data will even be in the new worksheet.

enter image description here

So far I have tried this function:

=IF(INDIRECT(NextSheetName()&”!A3″)&INDIRECT(NextSheetName()&”!B3″) = A3&B3, INDIRECT(NextSheetName()&”!D3″), “0”)

Which will work only in the case that the worksheets have the same data in the same order.

Does anyone have any idea how this can be done?

  • 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-09T13:11:46+00:00Added an answer on June 9, 2026 at 1:11 pm

    If you want to do this using VBA, try the following. The code copies matching rows from the source worksheet to the target worksheet and records the matching row on the source to the target, in case you find that useful. I named my sheets “Source” and “Target” and am assuming that you are wanting to match on the concatenation of columns A and B.

    The number of rows in your source and target don’t matter, nor does the order in which the matches appear.

    I wrote two different versions. The first works, but I’m not crazy about it because it loops through the source range looking for a match for each value in the target. The second version uses a dictionary that is built once. Matching search terms is then done without having to loop through a range. Note that to use the dictionary, you’ll need a reference to the Microsoft Scripting Runtime.

    First Version: (functional, but requires multiple loops)

    Sub GetTwoColumnMatches()
    
        Dim wsrc As Worksheet
        Dim wTgt As Worksheet
        Dim rng As Range
        Dim cell As Range
        Dim lLastTargetRow As Long
        Dim lMatchedRow As Long
        Dim sConcat As String
    
        Set wsrc = Sheets("Source")
        Set wTgt = Sheets("Target")
        lLastTargetRow = wTgt.Range("A" & wTgt.Rows.Count).End(xlUp).Row
    
    
        Set rng = wTgt.Range("a2:a" & lLastTargetRow)
        For Each cell In rng
            sConcat = cell & cell.Offset(, 1)
            lMatchedRow = Matches(sConcat)
            If lMatchedRow <> 0 Then
                wTgt.Range("a" & cell.Row & ":e" & cell.Row).Value = _
                wsrc.Range("a" & lMatchedRow & ":e" & lMatchedRow).Value
                wTgt.Range("f" & cell.Row) = lMatchedRow
            End If
        Next
    End Sub
    
    Function Matches(SearchFor As String) As Long
        Dim wsrc As Worksheet
        Dim rng As Range
        Dim cell As Range
        Dim lLastSourceRow As Long
        Dim lSourceRow As Long
    
        Set wsrc = Sheets("Source")
        lLastSourceRow = wsrc.Range("a" & wsrc.Rows.Count).End(xlUp).Row
    
        Set rng = wsrc.Range("a2:a" & lLastSourceRow)
        Matches = 0
        For Each cell In rng
            If cell & cell.Offset(, 1) = SearchFor Then
                Matches = cell.Row
                Exit For
            End If
        Next
    End Function
    

    Second Version: (optimized, requires reference to Microsoft Scripting Runtime)

    Sub GetTwoColumnMatches()
    
        Dim wsrc As Worksheet
        Dim wTgt As Worksheet
        Dim rng As Range
        Dim cell As Range
        Dim srcRng As Range
        Dim srcCell As Range
    
        Dim lLastTargetRow As Long
        Dim lLastSourceRow As Long
        Dim lMatchedRow As Long
        Dim lSourceRow As Long
    
        Dim sConcat As String
        Dim dict As Dictionary
    
        Set wsrc = Sheets("Source")
        Set wTgt = Sheets("Target")
        lLastTargetRow = wTgt.Range("A" & wTgt.Rows.Count).End(xlUp).Row
    
        Set wsrc = Sheets("Source")
        lLastSourceRow = wsrc.Range("a" & wsrc.Rows.Count).End(xlUp).Row
    
        'Create the dictionary
        Set dict = New Dictionary
    
        Set srcRng = wsrc.Range("a2:b" & lLastSourceRow)
        For Each srcCell In srcRng
            sConcat = srcCell & srcCell.Offset(, 1)
            If Len(sConcat) > 0 Then dict.Add sConcat, srcCell.Row
        Next
    
        Set rng = wTgt.Range("a2:a" & lLastTargetRow)
        For Each cell In rng
            sConcat = cell & cell.Offset(, 1)
            lMatchedRow = dict.Item(sConcat)
            If lMatchedRow <> 0 Then
                wTgt.Range("a" & cell.Row & ":e" & cell.Row).Value = _
                wsrc.Range("a" & lMatchedRow & ":e" & lMatchedRow).Value
                wTgt.Range("f" & cell.Row) = lMatchedRow
            End If
        Next
    End Sub
    

    Here’s what your references will look like once you’ve correctly selected the Microsoft Scripting Runtime:

    Reference to Microsoft Scripting Runtime

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a Chart which will grab data from external sources when
I am working with Excel 2010. I need to create a macro, that after
I need to create a Excel 2010 macro, which changes the SQL Command text
I need to create a VBA Macro in Excel. The task is that when
Just started working on a c project. Need help with passing function pointers/macro functions/etc.
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass

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.