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

The Archive Base Latest Questions

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

I’m trying to write a macro that will copy the rows if 3 conditions

  • 0

I’m trying to write a macro that will copy the rows if 3 conditions are met. Such as:

If “A” = B and “D” = E and “F” = G
Then copy row into next available row on sheet 2

If “A” = C and “D” = F and “F” = H
Then copy row into next available row on sheet 2

I need to repeat the above steps up to 50 times. The columns will not change

This is what I have so far:

`Sub SearchForString()

Dim LSearchRow As Integer
Dim LCopyToRow As Integer

On Error GoTo Err_Execute

'Start search in row 4
LSearchRow = 4

'Start copying data to row 2 in Sheet2 (row counter variable)
LCopyToRow = 2

While Len(Range("A" & CStr(LSearchRow)).Value) > 0

    'If value in column E = "Mail Box", copy entire row to Sheet2
    'If value in column D = "0", copy entire row to Sheet2
    'If value in column A = "5", copy entire row to Sheet2
    'If Range("E" & CStr(LSearchRow)).Value = "Mail Box" Then
    If Range("F" & CStr(LSearchRow)).Value = "Mail Box" And _
        Range("E" & CStr(LSearchRow)).Value = "0" And _
        Range("A" & CStr(LSearchRow)).Value = "5" Then

 'If Range("E" & CStr(LSearchRow)).Value = "Mail Box" Then

        'Select row in Sheet1 to copy
        Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
        Selection.Copy

        'Paste row into Sheet2 in next row
        Sheets("Sheet2").Select
        Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
        ActiveSheet.Paste

        'Move counter to next row
        LCopyToRow = LCopyToRow + 1

        'Go back to Sheet1 to continue searching
        Sheets("Sheet1").Select

        End If

    LSearchRow = LSearchRow + 1

    Wend

'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select

'MsgBox "All matching data has been copied."

'Exit Sub


        'Search 2

         'Start search in row 4
LSearchRow = 4

'Start copying data to row 3 in Sheet2 (row counter variable)
LCopyToRow = 3

While Len(Range("A" & CStr(LSearchRow)).Value) > 0

    'If value in column E = "Mail Box", copy entire row to Sheet2
    'If value in column D = "1", copy entire row to Sheet2
    'If value in column A = "5", copy entire row to Sheet2
    'If Range("E" & CStr(LSearchRow)).Value = "Mail Box" Then
    If Range("F" & CStr(LSearchRow)).Value = "Mail Box" And _
        Range("E" & CStr(LSearchRow)).Value = "1" And _
        Range("A" & CStr(LSearchRow)).Value = "5" Then

 'If Range("E" & CStr(LSearchRow)).Value = "Mail Box" Then

        'Select row in Sheet1 to copy
        Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
        Selection.Copy

        'Paste row into Sheet2 in next row
        Sheets("Sheet2").Select
        Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
        ActiveSheet.Paste

        'Move counter to next row
        LCopyToRow = LCopyToRow + 1

        'Go back to Sheet1 to continue searching
        Sheets("Sheet1").Select

    End If

    LSearchRow = LSearchRow + 1

Wend

'Position on cell A3
Application.CutCopyMode = False
Range("A3").Select

MsgBox "All matching data has been copied."

Exit Sub

 Err_Execute:
MsgBox "An error occurred."

End Sub
  • 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-09T12:11:33+00:00Added an answer on June 9, 2026 at 12:11 pm

    I think there’s probably a better way to do what you’re trying to acheive, but maybe this will help you along…

    Sub Tester()
        SearchForString "5", "0", "Mail Box"
        SearchForString "5", "1", "Mail Box"
    End Sub
    
    Sub SearchForString(ColA, ColE, ColF)
    
    Dim LSearchRow As Long
    Dim shtSearch As Worksheet
    Dim shtCopyTo As Worksheet
    Dim rw As Range
    
        LSearchRow = 4 'Start search in row 4
    
        Set shtSearch = Sheets("Sheet1")
        Set shtCopyTo = Sheets("Sheet2")
    
        Do While Len(shtSearch.Cells(LSearchRow, 1).Value) > 0
    
            Set rw = shtSearch.Rows(LSearchRow)
    
            If rw.Cells(6).Value = ColF And rw.Cells(5).Value = ColE And _
                                            rw.Cells(1).Value = ColA Then
    
                rw.Copy shtCopyTo.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                Exit Do '? you say there's only one result to find
            End If
            LSearchRow = LSearchRow + 1
        Loop
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

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.