I am totally new to Excel VBA. But I have a requirement to copy some data from let us say sheet1 to sheet2. I have several columns and rows in sheet1 (source sheet). Now I have to copy only Five columns data from within that where "Ferro Manganese" found in particular one coloum and if found then just copy the data's from the rows of the other 4 columns also and then paste in to the destination rows of the specified column there in Sheet2. Till this step I have done it successfully. Now the users are entering new data to the source sheet the next day and I only need to update those data to the destination sheet. The new data can be selected based on new Serial no on the sheet1 which is available in coloum "A". For every new entry they enter a increased serial number (Which is unique). so If I can check the "Ferro Manganese" condition from the newly entered serial number (from the point Serial no increased) then I can only copy those new data to the destination sheet using the successful code i am attaching it here. I need to learn how to write that extra if condition for increased serial no condition depending on which the paste function will work for only the new data. Due to office data populated in the sheet I could not upload it. But from the code the description I think you masters will understand it easily.
Please help me.
Please help me.
VBA Code:
Sub Datafetch()
Dim lastrow As Long, erow As Long
Dim LastRowIndex As Integer
Dim RowIndex As Integer
Dim UsedRng As Range
lastrow = Worksheets("SIPL Details").Cells(Rows.Count, 1).End(xlUp).Row
erow = Worksheets("Ferro Tracking").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrow
If Worksheets("SIPL Details").Cells(i, 7).Value = "Ferro Manganese" Or Worksheets("SIPL Details").Cells(i, 7).Value = "Silico Manganese" Then
Worksheets("SIPL Details").Cells(i, 3).Copy
Worksheets("SIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 2)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "SIPL"
Worksheets("SIPL Details").Cells(i, 7).Copy
Worksheets("SIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 4)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "SIPL"
Worksheets("SIPL Details").Cells(i, 14).Copy
Worksheets("SIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 7)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "SIPL"
Worksheets("SIPL Details").Cells(i, 9).Copy
Worksheets("SIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 11)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "SIPL"
Worksheets("SIPL Details").Cells(i, 5).Copy
Worksheets("SIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 13)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "SIPL"
End If
Next i
For i = 2 To lastrow
If Worksheets("ESPL Details").Cells(i, 7).Value = "Ferro Manganese" Or Worksheets("ESPL Details").Cells(i, 7).Value = "Silico Manganese" Then
Worksheets("ESPL Details").Cells(i, 3).Copy
Worksheets("ESPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 2)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "ESPL"
Worksheets("ESPL Details").Cells(i, 7).Copy
Worksheets("ESPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 4)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "ESPL"
Worksheets("ESPL Details").Cells(i, 14).Copy
Worksheets("ESPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 7)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "ESPL"
Worksheets("ESPL Details").Cells(i, 9).Copy
Worksheets("ESPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 11)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "ESPL"
Worksheets("ESPL Details").Cells(i, 5).Copy
Worksheets("ESPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 13)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "ESPL"
End If
Next i
For i = 2 To lastrow
If Worksheets("BSIPL Details").Cells(i, 7).Value = "Ferro Manganese" Or Worksheets("BSIPL Details").Cells(i, 7).Value = "Silico Manganese" Then
Worksheets("BSIPL Details").Cells(i, 3).Copy
Worksheets("BSIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 2)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "BSIPL"
Worksheets("BSIPL Details").Cells(i, 7).Copy
Worksheets("BSIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 4)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "BSIPL"
Worksheets("BSIPL Details").Cells(i, 14).Copy
Worksheets("BSIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 7)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "BSIPL"
Worksheets("BSIPL Details").Cells(i, 9).Copy
Worksheets("BSIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 11)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "BSIPL"
Worksheets("BSIPL Details").Cells(i, 5).Copy
Worksheets("BSIPL Details").Paste Destination:=Worksheets("Ferro Tracking").Cells(erow + i, 13)
Worksheets("Ferro Tracking").Cells(erow + i, 1).Value = "BSIPL"
End If
Next i
Set UsedRng = ActiveSheet.UsedRange
LastRowIndex = UsedRng.Row - 1 + UsedRng.Rows.Count
Application.ScreenUpdating = False
For RowIndex = LastRowIndex To 1 Step -1
If Application.CountA(Rows(RowIndex)) = 0 Then
Rows(RowIndex).Delete
End If
Next RowIndex
Application.ScreenUpdating = True
End Sub
Last edited by a moderator: