If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Find, Select, Copy And Paste Macro - Urgent

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-09-09, 14:38
munsonc munsonc is offline
Registered User
 
Join Date: Mar 2009
Posts: 2
Find, Select, Copy And Paste Macro - Urgent

Hi,

I'm a nit of a newbie to VB, I have the basics but I am stuck with a problem!

I have a list of names in a sheet that I want to search for in another sheet then highlight that row and copy it back into the original sheet next to the cell with the name in it.

So in sheet1 I have cell A4 = "ABC" and I want to search in sheet2 for ABC in Column B. When the row with "ABC" is found I want to copy the row of data after the name - so Column C to column S and then paste it back into sheet1 in cell A5 (next to the original name in cell A4).

There are a list of names in cells A4:33 so I want this process to repeat and find all of the names and the relevant data until it reaches the end of the list.

Please help,

Thanks,

Chris
Reply With Quote
  #2 (permalink)  
Old 03-10-09, 12:14
VBAExpert VBAExpert is offline
Registered User
 
Join Date: Mar 2009
Location: Worthing, UK
Posts: 16
Smile

Hi

Please find code below.. I hope it will work.. I have double checked and worked for me. I have also attached the workbook for you.

Please let me know if it works.




Code:
Sub FindSelectCopyPaste()

'By Muhammad Anisur Rahman
'Date: 10/03/2009
'Find Select- Copy and Paste Row from Sheet2 to Sheet 1


Dim r As Long
Dim LastRow1 As Long
Dim LastRow2 As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim strLookup As String



Set ws1 = Worksheets("Sheet1")

Set ws2 = Worksheets("Sheet2")

'Find LastRow for Sheet1
LastRow1 = ws1.Cells(65536, 1).End(xlUp).Row


'Find LastRow for Sheet2
LastRow2 = ws2.Cells(65536, 1).End(xlUp).Row


ws2.Select 'select sheet2



Set rngLookup = Range("B1", Cells(LastRow2, 2))


 For i = 4 To LastRow1 'LastRow of Sheet2
 
            strLookup = ws1.Cells(i, 1) 'There are a list of names in cells A4:A33... A(n) or more


            r = Application.WorksheetFunction.Match(strLookup, rngLookup, 0) 'Find Row number of Sheet2 as per above criteria

              
                With ws2
                
                'Copy the Row Range C(r):S(r) [r is a row number]
                'Paste into next column of criteria [Next to "ABC"] in sheet1,
                .Range(.Cells(r, 3), .Cells(r, 19)).Copy ws1.Cells(i, 2)
                
                End With
                


Next i


ws1.Select 'select Sheet1


End Sub
Reply With Quote
  #3 (permalink)  
Old 03-10-09, 12:47
munsonc munsonc is offline
Registered User
 
Join Date: Mar 2009
Posts: 2
Thanks I will give it a go and let you know!

C
Reply With Quote
  #4 (permalink)  
Old 03-12-09, 01:27
Chris Bode Chris Bode is offline
Registered User
 
Join Date: Feb 2009
Posts: 6
You can achieve this task using Macro as follows
1. Right click on the toolbar>from the popup select control box
2. from the control box that appears on the sheet, draw a command button on your sheet
3. double click the command button to open the code window and paste following codes
Code:
Private Sub CommandButton1_Click()
    Dim row As Integer, col As Integer
    row = 4
    col = 1
    
    For row = 4 To 33
        findMatch Sheet1.Cells(row, col).Value, row
    Next
End Sub

Private Sub findMatch(str As String, i As Integer)
    Dim row As Integer, col As Integer
    row = 1
    col = 2
    
    While Sheet2.Cells(row, col).Value <> ""
        If str = Sheet2.Cells(row, col).Value Then
            For col = 3 To 19
                Sheet1.Cells(i, col).Value = Sheet2.Cells(row, col).Value
            Next
            
        End If
        row = row + 1
    Wend
    
End Sub
Now execute the code and check the result
Reply With Quote
  #5 (permalink)  
Old 03-12-09, 16:31
VBAExpert VBAExpert is offline
Registered User
 
Join Date: Mar 2009
Location: Worthing, UK
Posts: 16
I gave the code with attachment... did u read or review my code/attachment..

cheers

Quote:
Originally Posted by Chris Bode
You can achieve this task using Macro as follows
1. Right click on the toolbar>from the popup select control box
2. from the control box that appears on the sheet, draw a command button on your sheet
3. double click the command button to open the code window and paste following codes
Code:
Private Sub CommandButton1_Click()
    Dim row As Integer, col As Integer
    row = 4
    col = 1
    
    For row = 4 To 33
        findMatch Sheet1.Cells(row, col).Value, row
    Next
End Sub

Private Sub findMatch(str As String, i As Integer)
    Dim row As Integer, col As Integer
    row = 1
    col = 2
    
    While Sheet2.Cells(row, col).Value <> ""
        If str = Sheet2.Cells(row, col).Value Then
            For col = 3 To 19
                Sheet1.Cells(i, col).Value = Sheet2.Cells(row, col).Value
            Next
            
        End If
        row = row + 1
    Wend
    
End Sub
Now execute the code and check the result
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On