Copy the the data between sheets ( conditionally).

diimoo1

New Member
Joined
Jun 12, 2011
Messages
34
Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
The below code works and return one single name match ( each run) and it basically copies the data from sheet one to sheet two using Name as a key. Now would
like create loop to go through both sheets and compare the names, and
if the name exist in sheet one then copy the corresponding value into sheet two.

<o:p></o:p>

Please let me know the possibilities.

Thanks.
<o:p></o:p>
Sheet one: “Cargo”
Cargo Summary
Request <o:p></o:p>

Total 1 Total 2
Lori Trump
Susan doo
Dii Kon 888 111
Moo Kevin
Ajax James
Alex Trapek
Kevin O'neil
Ming Kii
Kung Kwan
Dii Kii
Nung No
Li Morgan
Total <o:p></o:p>

Sheet2:” ORDER”
New Cargo <o:p></o:p>

Cargo Captain Sea Name Total A Total B
General ZW N/A Lori Trump 122 66
General DE N/A Moris Bee 40 56
General DD N/A Dii Kon 888 78
General DD N/A Moo Kevin 127 99
General DD N/A Ajax James 24 33
General DD N/A Alex Trapek 231 12
General MM N/A Kevin O'neil 0 10
General Total
Reeefer HU N/A Dii Kon 0 33
Reefer HU N/A Kevin O'neil 515 55
Reefer Total 515
Fish cargo NI N/A Moris Bee 0 67
Fish cargo NI N/A Ajax James 0 83
Fish cargo Total <o:p></o:p>

<o:p> </o:p>
<o:p> </o:p>
‘Here the is the working code:
Option Explicit <o:p></o:p>

Option Compare Text <o:p></o:p>
Public Captain As String
Public LastRow As Double
Public Variable(15) As Variant
Public VarTemp(15) As Variant <o:p></o:p>

Sub Cargo_Data()
Dim iCol As Long
Dim x As Long, x1 As Long, x2 As Long
Dim NewBook As Workbook
Dim bFind As Boolean
Dim Name As String
Dim iTmp As Long <o:p></o:p>

Worksheets("Cargo").Select <o:p></o:p>
'clear the array variable
For x1 = 1 To 15
Variable(x1) = 0
VarTemp(x1) = 0
Next x1 <o:p></o:p>

Captain = Name
If Captain <> " " Then
' GoTo Name
End If <o:p></o:p>

ActiveWorkbook.Sheets("Order").Select
' Worksheets("Order").Select
Range("A6").Select
Selection.End(xlDown).Select
LastRow = ActiveCell.Row <o:p></o:p>

For x1 = 6 To LastRow
If Cells(x1, 4) = "Dii Kon" Then <o:p></o:p>

Call AddTo(Variable(2), Cells(x1, 5))
Call AddTo(Variable(3), Cells(x1, 6)) <o:p></o:p>

End If <o:p></o:p>
Next x1 <o:p></o:p>
'--------------------------------------------------------------------------­----------------------------
'Imports to the data <o:p></o:p>

Worksheets("Cargo").Select
Range("A7").Select
Selection.End(xlDown).Select
LastRow = ActiveCell.Row
For x1 = 7 To LastRow
If Cells(x1, 1) = "Dii Kon" Then <o:p></o:p>

For iCol = 2 To 15
Select Case iCol
Case 2 To 3 <o:p></o:p>

Cells(x1, iCol) = Variable(iCol)
End Select
Next iCol <o:p></o:p>

Exit For
End If
Next x1 <o:p></o:p>

End Sub <o:p></o:p>
Sub AddTo(ByRef vValue As Variant, vNew As Variant)
If IsNumeric(vNew) Then
If IsNumeric(vValue) Then
vValue = vValue + vNew
ElseIf vValue = "na" Or vValue = "" Then
vValue = vNew
End If
ElseIf vNew = "na" Then
If vValue = 0 Then
vValue = "na"
End If
End If <o:p></o:p>

End Sub <o:p></o:p>
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Just claridy one more thing:
Copies the value from Sheet2 to Sheet1.
the current code return only one instance by hardcoding each name insidethe code.

Dii Kon 888 111

Would like to place a loop dynamically:

Thanks in advance
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,233
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top