John.McLaughlin
Board Regular
- Joined
- Jul 19, 2011
- Messages
- 169
Why do I keep pasting to the first 5 columns, instead of finding the last empty row in column A?
Thanks in advance!
Thanks in advance!
VBA Code:
Sub PostTools()
' TOOLSPost - post from OPEN ORDER TOOLS SS to Scheduled TOOLS SS
'
'
' Copy items to sheet.
'
'
Application.ScreenUpdating = False
Dim wbTarget As Workbook 'workbook where the data is to be pasted SCHEDULED:TOOLS
Dim wbThis As Workbook 'workbook from where the data is to copied Open Order"Tools
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
' ------------------ End find last row
'set to the current active workbook (the source book - Open Order)
Set wbThis = ActiveWorkbook
'unhide TOOLS sheet in the Open Order workbook and select it
Sheets("TOOLS").Visible = True
Sheets("TOOLS").Select
'get the active sheetname of the Open Order workbook
strName = ActiveSheet.Name
' Activate the Scheduled Workbook and select the Tools worksheet
Workbooks("SCHEDULED.xlsm").Activate
Set wbTarget = ActiveWorkbook
Sheets("TOOLS").Select
'activate the Open Order source book
wbThis.Activate
'clear any thing on clipboard to maximize available memory
Application.CutCopyMode = False
' copy the range from source book
wbThis.Sheets("TOOLS").Range("A5:Z5").Copy
' Assign the Worksheets
Set SrcWks = wbThis.Sheets("TOOLS")
Set DstWks = wbTarget.Sheets("TOOLS")
' Get all cells in the Source Range starting with row 5
Set SrcRng = SrcWks.Range("A5:Z5")
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, 26)
' Find the next empty row in the Destination Range starting at row 3
' Set DstRng = DstWks.Range("A3:E3")
Set DstRng = DstWks.Range("A3:A3")
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
DstRng.Offset(N, 0).Resize(, 5).Value = SrcRng.Rows(r).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 TOOLS sheet and select order sheet
Sheets("TOOLS").Visible = False
Sheets("ORDER").Select
Application.CutCopyMode = False
Application.ScreenUpdating = True
Range("A1").Select
wbThis.Activate
ActiveSheet.Unprotect
Range("F30").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!H$30].Value = Now()
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False
Application.CutCopyMode = False
Application.ScreenUpdating = True
Set wbTarget = Nothing
Set wbThis = Nothing
End Sub
Last edited by a moderator: