| |
|
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.
|
 |

03-09-09, 14:38
|
|
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
|
|

03-10-09, 12:14
|
|
Registered User
|
|
Join Date: Mar 2009
Location: Worthing, UK
Posts: 16
|
|
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
|
|

03-10-09, 12:47
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 2
|
|
|
|
Thanks I will give it a go and let you know!
C
|
|

03-12-09, 01:27
|
|
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
|
|

03-12-09, 16:31
|
|
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
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|