+ Reply to Thread
Results 1 to 6 of 6

Find cell in Range and If found then copy to next sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    02-07-2013
    Location
    Slovakia
    MS-Off Ver
    Excel 2010
    Posts
    31

    Find cell in Range and If found then copy to next sheet

    Hello, I need a macro that will do the following:

    I have two sheets with data with many columns and rows, on the sheet1 in column B are listed serial numbers which I want to find in sheet2 (they are also listed in column B). When the serno will match, then copy entire row from sheet1 (for the specific serno.) to the place where the serno is placed in sheet2, but start pasting from column A.

    Hope I explained it clear, any suggestions ?

  2. #2
    Registered User
    Join Date
    02-07-2013
    Location
    Slovakia
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Find cell in Range and If found then copy to next sheet

    Well I was able to put the code together alone, because nobody has replied to my thread This is what I have so far, but I would like to insert a msgbox into code, so user can define the source and target range. Please can you help me with it ?

    Sub Find_Serialnumber()
    
        Dim c As Range, d As Range
        Dim dCol As Integer
        
        ' Speed
        calc = Application.Calculation
        With Application
         .Calculation = xlCalculationManual
         .ScreenUpdating = False
         End With
        
        Worksheets("Source").Activate
        For Each c In Worksheets("Source").Range("B1:B748")
            For Each d In Worksheets("Target").Range("B1:B3487")
                
                If c = d Then
                On Error Resume Next
                dCol = d.Column
                
                c.EntireRow.Copy d.Offset(0, -1)
                On Error Resume Next
                
                
                    Exit For
                End If
            Next
        Next
       Application.CutCopyMode = False
       Worksheets("Target").Activate
      Range("B1").Select
    
    End Sub
    Last edited by arlu1201; 02-20-2013 at 04:57 AM.

  3. #3
    Forum Contributor
    Join Date
    01-25-2013
    Location
    near Philly, PA USA
    MS-Off Ver
    Excel 2019
    Posts
    182

    Re: Find cell in Range and If found then copy to next sheet

    try:
    Sub TestInputBox()
        Dim myRange As Range
     
        Set myRange = Application.InputBox(Prompt:= _
            "Please Select a Range", _
            Title:="InputBox Method", Type:=8)
     
        If myRange Is Nothing Then
            ' Range is blank
        Else
            myRange.Select
        End If
    End Sub

  4. #4
    Registered User
    Join Date
    02-07-2013
    Location
    Slovakia
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Find cell in Range and If found then copy to next sheet

    Thanks bob33 for your answer, I know how the msgbox code looks like but how to implement it to my code ? The code should look something like this:

    Sub Find()
    
        Dim c As Object, d As Object
        Dim dCol As Integer
        Dim Source As Range
        Dim Target As Range
        
        ' Speed
       ' calc = Application.Calculation
       ' With Application
       ' .Calculation = xlCalculationManual
       ' .ScreenUpdating = False
       '  End With
        
        Set Source = Application.InputBox( _
            Prompt:="Please enter a source range to search for", _
            Title:="InputSerialNumber", _
            Default:=ActiveCell.Address, _
            Type:=8)
        If Source Is Nothing Then
            MsgBox "No source range selected"
        'Else
        '   Source.Select
        End If
        
         On Error Resume Next
        Set Target = Application.InputBox( _
            Prompt:="Please enter a target range to search in", _
            Title:="OutputSerialNumber", _
            Default:=ActiveCell.Address, _
            Type:=8)
        If Target Is Nothing Then
            MsgBox "No source range selected"
        'Else
        '    Target.Select
        End If
         
        Worksheets("Source").Activate
        c = Worksheets("Source").Source  - HERE IS THE PROBLEM PART, HOW TO DEFINE THAT FOR EACH CELL FROM SELECTED RANGE FROM PREVIOUS STEP, SERCH FOR MATCH AND THEN COPY WHOLE ROW FROM SOURCE SHEET TO THE RIGHT PLACE IN TARGET SHEET
        d = Worksheets("Target").Target
         For Each c In Worksheets("Source").Source
            For Each d In Worksheets("Target").Target
            
                If c = d Then
                On Error Resume Next
                dCol = Target.Column
                
                Source.EntireRow.Copy Target.Offset(0, -1) 
                On Error Resume Next
                
                
                    'Exit For
                End If
            Next
        Next
       Application.CutCopyMode = False
       Worksheets("Target").Activate
      Range("A1").Select
    
    End Sub
    Any ideas how to resolve it ? Thanks in advance!
    Last edited by arlu1201; 02-20-2013 at 04:58 AM.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Find cell in Range and If found then copy to next sheet

    Sachy,

    Welcome to the forum.

    I have added code tags to your posts. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. If you need more information on how to use them, check my signature below this post.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Registered User
    Join Date
    02-07-2013
    Location
    Slovakia
    MS-Off Ver
    Excel 2010
    Posts
    31

    Re: Find cell in Range and If found then copy to next sheet

    After reading some similar threads and also googling around (which takes pretty much time), I was able to put together a functional version of code, that does exactly what I want. So here it is, if anybody is dealing with same problem:
    Public Sub SerialSearch()
    Dim cell1 As Range
    Dim Source As Range
    Dim cell2 As Range
    Dim Target As Range
    
    Set Source = Application.InputBox( _
            Prompt:="Please enter a source range to search for", _
            Title:="InputSerialNumber", _
            Default:=ActiveCell.Address, _
            Type:=8)
        If Source Is Nothing Then
            MsgBox "No source range selected"
    
         End If
        
         On Error Resume Next
    
        Set Target = Application.InputBox( _
            Prompt:="Please enter a target range to search in", _
            Title:="OutputSerialNumber", _
            Default:=ActiveCell.Address, _
            Type:=8)
        If Target Is Nothing Then
            MsgBox "No source range selected"
    
        End If
    
        On Error Resume Next
    
    For Each cell1 In Source
        With Target
            Set cell2 = .Find(cell1.Value, LookIn:=xlValues)
            
    If cell2.Value <> cell1.Value Then
               
                cell1.Interior.ColorIndex = 3 'When searching serial numb. is not found, change its cell color into red  
    Else
                    Worksheets("Source").Activate
                    Set aaa = ActiveSheet.Range(cell1.Offset(0, -1), cell1.Offset(0, 32))
                    aaa.Copy
                    
                    Worksheets("Target").Activate
                    cell2.Offset(0, -1).Select
                    ActiveSheet.Paste
                    
                End If
        End With
    Next cell1
    
    End Sub
    Last edited by Sachy; 03-20-2013 at 11:04 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1