copy to other workbook

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Could use a little help

attempting again here to copy if two criteria is met to another workbook. Using Code below

Sub copyMC()


Dim lastrow As Long
Dim lastrow2 As Long
Dim i As Long

lastrow = Workbooks("Book10").Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lastrow2 = Workbooks("SalesMaster").Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To lastrow

If Cells(i, "A").Value = Date And Cells(i, "B").Value = "Sales" Then

Rows(i).Copy Destination:=Workbooks("Book10").Sheets("Sheet1").Range("A" & lastrow2 + 1)
lastrow2 = Workbooks("SalesMaster").Sheets("sheet1").Cells(Rows.Count, "A").End(xlUp).Row
'Workbooks("Book10").Sheets("Sheet1").Rows(1).Copy Destination:=workbook(sSheets("Sheet1").Rows(1)

The above is an attempt to copy header row

End If


Next i




End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Assuming that you are trying to copy from "SalesMaster" to "Book10"
Try
Code:
Sub copyMC()


    Dim lastrow As Long
    Dim i As Long
    Dim Ws As Worksheet
    
    Set Ws = Workbooks("Book10").Sheets("Sheet1")

  
    With Workbooks("SalesMaster").Sheets("sheet1")
        lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
        .Rows(1).Copy Ws.Range("A1")
    
        For i = 2 To lastrow
            If .Cells(i, "A").Value = Date And .Cells(i, "B").Value = "Sales" Then
                .Rows(i).Copy Destination:=Ws.Range("A" & Rows.Count).End(xlUp).Offset(1)
             End If
        Next i
    End With
End Sub
 
Upvote 0
Not a problem, try this
Code:
Sub copyMC()


    Dim lastrow As Long
    Dim i As Long
    Dim Ws As Worksheet
    
    Set Ws = Workbooks("SalesMaster").Sheets("Sheet1")

  
    With Workbooks("Book10").Sheets("sheet1")
        lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
        .Rows(1).Copy Ws.Range("A1")
    
        For i = 2 To lastrow
            If .Cells(i, "A").Value = Date And .Cells(i, "B").Value = "Sales" Then
                .Rows(i).Copy Destination:=Ws.Range("A" & Rows.Count).End(xlUp).Offset(1)
             End If
        Next i
    End With
End Sub
 
Upvote 0
changed the sheet names works perfect.

Not accustomed to the with and end with, I'll figure it out.

thank you
 
Upvote 0
It's simply a short cut.
Everywhere you have something starting with a . is part of the with.
so
Code:
lastrow = Workbooks("Book10").Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
is the same as
Code:
With Workbooks("Book10").Sheets("sheet1")
        lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
Hope that makes some sense
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,801
Members
449,337
Latest member
BBV123

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