M
moso97ad
I need need to make a code in VBA that can perform a search and copy
and paste the the rows from one sheet in excel to another. Let me
explain it.
1. The user type in a number in cell "A1" in sheet 1 and hit the
"Search-button".
2. A search for the number in "A1" will begin in sheet 1 in column
A10
to A?????? (the number is not unique, so it can occur many times).
3. If the result of the search is that the number is found 10 times,
then these 10 rows must be copied and pasted to sheet 2. BUT it is
not
all the columns that need to be pasted, let us say it is only column
A, B, C, F and G.
I have tried to use the following code, but I can't figure out how to
change the search conditions. Right now I search for the word
"Significant", but I wan't to search for a number in cell A1. How can
I rewrite the code, so it adjusts to my search conditions?
Sub CopySignificant()
'Copy cells of cols A,F,E,D from rows containing "Significant" in
'col D of the active worksheet (source sheet) to cols
'A,B,C,D of Sheet2 (destination sheet)
Dim DestSheet As Worksheet
Set DestSheet = Worksheets("Sheet2")
Dim sRow As Long 'row index on source worksheet
Dim dRow As Long 'row index on destination worksheet
Dim sCount As Long
sCount = 0
dRow = 1
For sRow = 1 To Range("D65536").End(xlUp).Row
'use pattern matching to find "Significant" anywhere in cell
If Cells(sRow, "D") Like "*Significant*" Then
sCount = sCount + 1
dRow = dRow + 1
'copy cols A,F,E & D
Cells(sRow, "A").Copy Destination:=DestSheet.Cells(dRow, "A")
Cells(sRow, "F").Copy Destination:=DestSheet.Cells(dRow, "B")
Cells(sRow, "E").Copy Destination:=DestSheet.Cells(dRow, "C")
Cells(sRow, "D").Copy Destination:=DestSheet.Cells(dRow, "D")
End If
Next sRow
MsgBox sCount & " Significant rows copied", vbInformation, "Transfer
Done"
End Sub
Best regards
M
and paste the the rows from one sheet in excel to another. Let me
explain it.
1. The user type in a number in cell "A1" in sheet 1 and hit the
"Search-button".
2. A search for the number in "A1" will begin in sheet 1 in column
A10
to A?????? (the number is not unique, so it can occur many times).
3. If the result of the search is that the number is found 10 times,
then these 10 rows must be copied and pasted to sheet 2. BUT it is
not
all the columns that need to be pasted, let us say it is only column
A, B, C, F and G.
I have tried to use the following code, but I can't figure out how to
change the search conditions. Right now I search for the word
"Significant", but I wan't to search for a number in cell A1. How can
I rewrite the code, so it adjusts to my search conditions?
Sub CopySignificant()
'Copy cells of cols A,F,E,D from rows containing "Significant" in
'col D of the active worksheet (source sheet) to cols
'A,B,C,D of Sheet2 (destination sheet)
Dim DestSheet As Worksheet
Set DestSheet = Worksheets("Sheet2")
Dim sRow As Long 'row index on source worksheet
Dim dRow As Long 'row index on destination worksheet
Dim sCount As Long
sCount = 0
dRow = 1
For sRow = 1 To Range("D65536").End(xlUp).Row
'use pattern matching to find "Significant" anywhere in cell
If Cells(sRow, "D") Like "*Significant*" Then
sCount = sCount + 1
dRow = dRow + 1
'copy cols A,F,E & D
Cells(sRow, "A").Copy Destination:=DestSheet.Cells(dRow, "A")
Cells(sRow, "F").Copy Destination:=DestSheet.Cells(dRow, "B")
Cells(sRow, "E").Copy Destination:=DestSheet.Cells(dRow, "C")
Cells(sRow, "D").Copy Destination:=DestSheet.Cells(dRow, "D")
End If
Next sRow
MsgBox sCount & " Significant rows copied", vbInformation, "Transfer
Done"
End Sub
Best regards
M