Link to home
Start Free TrialLog in
Avatar of wellous
wellousFlag for Egypt

asked on

Copy row according to value in different sheet column with Macro

Dear All,

I have an Excel file which have 3 sheets ( Databse , Quotes , Report).
I want a Macro to check Column A in sheet (Quotes) Range (A2 :A.lastRow)
and to look for those values in sheet (Database) in Column (S) ...
If found then to copy the enitre row to sheet (Report) Range starting from (A4).


I attached a sample sheet for your ref.

If possible to make it in a new vision like copy all rows from Database sheet to Report sheet then make the application.screenupdate =true while deleting the others rows which do not equal to Column A in sheet (Quotes) .. i hope you get my point , when the macro run it will show how the rows are nicely pasted on the sheet report like someone is typing them ;)
Anyway this is not a must .. but would appreciate it a lot.

Thanks and Brgds
Wellous


Tracker.xls
SOLUTION
Avatar of krishnakrkc
krishnakrkc
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wchh
wchh

Amended Macro  from previous comment?
Sub x()

Dim r1 As Range, r2 As Range, r3 As Range, r4 As Range

With Sheets("Quotes")
    Set r1 = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With

With Sheets("Database")
    Set r2 = .Range("S4", .Range("S" & Rows.Count).End(xlUp))
End With

Sheets("Report").Range("4:" & Rows.Count).EntireRow.Delete
For Each r3 In r1
    For Each r4 In r2
        If r4 = r3    r4.EntireRow.Copy Sheets("Report").Range("A" & Sheets("Report").UsedRange.Rows.Count + 1)
    Next r4
Next r3

End Sub

Open in new window


Sub x()

Dim r1 As Range, r2 As Range, r3 As Range, r4 As Range

With Sheets("Quotes")
    Set r1 = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With

With Sheets("Database")
    Set r2 = .Range("S4", .Range("S" & Rows.Count).End(xlUp))
End With

Sheets("Report").Range("4:" & Rows.Count).EntireRow.Delete
For Each r3 In r1
    For Each r4 In r2
        If r4 = r3 then r4.EntireRow.Copy Sheets("Report").Range("A" & Sheets("Report").UsedRange.Rows.Count + 1)
    Next r4
Next r3

End Sub

Open in new window

Avatar of wellous

ASKER

Thank you ALL,
@Kris : Your code is working perfect :) your way about handling the dates and data types is great.
@StephenJR : Unfortunately , the database has blank rows ( lots)... your code brings first row only.
@ wchh: After you modified Stephan's code it works very well..

Can i make a little modification ? what if the user puts a quote which is not in database?
Whenever the quote is not found in database i want to write " quote not found" .

I tried to add this line of code  after line 16 of Wchh's code :
Else: Sheets("Report").Range("A" & Sheets("Report").UsedRange.Rows.Count) = "quote not found"
the macro starts to run slowly like the data is being written or slowly pasted row by row .. i love it :)
but it's putting "quote not found"  in the beginning of all report rows :(


Many thanks for your great support.
Wish you the best.
Wellous
Avatar of wellous

ASKER

Sorry if i couldn't explain very well .
To write " quote not found" in Report sheet , it's much better if the code can bring the quote value from the sheet ( Quotes) and write beside it " quote not found" in its row, So let's say in
sheet Report Range A to write the quote value taken from sheet ( Quotes) and in Range B to write " quote not found".

Thanks again.
Wellous

Sub x()

Dim r1 As Range, r2 As Range, r3 As Range, r4 As Range
Dim Cnt As Long

With Sheets("Quotes")
    Set r1 = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With

With Sheets("Database")
    Set r2 = .Range("S4", .Range("S" & Rows.Count).End(xlUp))
End With

Sheets("Report").Range("4:" & Rows.Count).EntireRow.Delete

For Each r3 In r1
    Cnt = 0
    For Each r4 In r2
       If r4 = r3 Then
       r4.EntireRow.Copy Sheets("Report").Range("A" & Sheets("Report").UsedRange.Rows.Count + 1)
       Cnt = Cnt + 1
       End If
    Next r4
    If Cnt = 0 Then
       Sheets("Report").Range("B" & Sheets("Report").UsedRange.Rows.Count + 1).Value = "quote not found"
       Sheets("Report").Range("S" & Sheets("Report").UsedRange.Rows.Count).Value = r3.Value
    End If
Next r3

End Sub

Open in new window

Avatar of wellous

ASKER

Thank you wchh , the code is running very well, but the entire row is not formated !
Can we formate it ? let's say to make the entire row red .

this is my last dimand hopefully ;) and then will give you the points.
Many thanks for your support
Much Appreciated,
Wellous
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wellous

ASKER

Thank you :)