John.McLaughlin
Board Regular
- Joined
- Jul 19, 2011
- Messages
- 169
When copying Cells A thru E from the source workbook, it only pastes columns C & D in the destination workbook?
My Source workbook is an individual customers order with a worksheet named "List"
My destination workbook is named Pending and contains the worksheet "Sold"
The macro finds the last empty Row in the destination worksheet ok, but only the data in column C & D from the source workbook are pasted to the destination worksheet?
What am I missing?
Thanks in advance!
My Source workbook is an individual customers order with a worksheet named "List"
My destination workbook is named Pending and contains the worksheet "Sold"
The macro finds the last empty Row in the destination worksheet ok, but only the data in column C & D from the source workbook are pasted to the destination worksheet?
What am I missing?
Thanks in advance!
Code:
Sub Macro5()
'
Application.ScreenUpdating = False
Dim wbTarget As Workbook 'workbook where the data is to be pasted
Dim wbThis As Workbook 'workbook from where the data is
Dim strName As String 'name of the source sheet/ target workbook
Dim filelink As String ' name of workbook
Dim targetFile As String
' ----------------- Find last row
Dim DstRng As Range
Dim DstWks As Worksheet
Dim LastRow As Long
Dim N As Long, r As Long
Dim SrcRng As Range
Dim SrcWks As Worksheet
'set to the current active workbook (the source worksheet)
Set wbThis = ActiveWorkbook
'unhide sheet and select it
Sheets("List").Visible = True
Sheets("List").Select
'get the active sheetname of the open workbook
strName = ActiveSheet.Name
' Activate open workbook
Workbooks("Pending.xlsm").Activate
Set wbTarget = ActiveWorkbook
Sheets("Sold").Select
'activate the source book
wbThis.Activate
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
' copy the range from source book
wbThis.Sheets("List").Range("A5:E5").Copy
' Assign the Worksheets
Set SrcWks = wbThis.Sheets("List")
Set DstWks = wbTarget.Sheets("Sold")
' Get all cells in the Source Range starting with row 5
Set SrcRng = SrcWks.Range("A5:E5")
LastRow = SrcWks.Cells(Rows.Count, "B").End(xlUp).Row
If LastRow < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcRng.Resize(LastRow - SrcRng.Row + 1, 5)
' Find the next empty row in the Destination Range starting at row 3
Set DstRng = DstWks.Range("A3:E3")
LastRow = DstWks.Cells(Rows.Count, "A").End(xlUp).Row
Set DstRng = IIf(LastRow < DstRng.Row, DstRng, DstRng.Offset(LastRow - DstRng.Row + 1, 0))
' Copy the Source cells to the next empty Destination row if the Source Cell in "A" is not empty
For r = 1 To SrcRng.Rows.Count
If SrcRng.Cells(r, "A") <> "" Then
SrcRng.Rows(r).Copy DstRng.Offset(N, 0)
DstRng.Offset(N, 0).Value = DstRng.Offset(N, 0).Value
N = N + 1
End If
Next r
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
'save the target book
wbTarget.Save
'activate the source book again
wbThis.Activate
'hide List sheet and select order sheet
Sheets("List").Visible = False
Sheets("ORDER").Select
Application.CutCopyMode = False
Application.ScreenUpdating = True
Range("A1").Select
wbThis.Activate
ActiveSheet.Unprotect
Range("F23").Select
With Selection.Font
.Name = "Trebuchet MS"
.FontStyle = "Bold"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.Color = 5287936
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
[ORDER!G$23].Value = Now()
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False
Application.CutCopyMode = False
Application.ScreenUpdating = True
Set wbTarget = Nothing
Set wbThis = Nothing
'
End Sub