Cutting & pasting from one workbook to another
You mean, some code like this?
This needs to go into the Placements workbook's Workbook_Open event
processor. In Excel 2003 and earlier, easy way to get there is to
right-click on the Excel icon immediately to the left of the word "File" in
the main Excel menu and choose [View Code] from the list presented to you.
Once there, cut and paste the code below into the module. Make any changes
to the Const values that I set up for you so that workbook names, worksheet
names and starting data rows and "most used" column are all identified
properly for your setup.
The only "catch" to this is that both of the workbooks must be in the same
directory (same folder).
Private Sub Workbook_Open()
'when this workbook is opened, it will look for
'the other workbook (Dump) in the same folder
'and if it finds it there, it will open it
'also, then copy all rows from
'PlacementsSheet in this workbook to
'DumpSheet in the Dump workbook,
'deleting the row's data in the process
'in this workbook, and when finished
'will close the Dump.xls file
'
'you may change these Const values to
'match the file names, worksheet names
'and starting row for data in the books
'
Const dumpWBName = "dump.xls"
Const dumpWSName = "DumpSheet"
Const firstDumpRow = 2
Const placementSheetName = "PlacementsSheet"
Const firstPlacementDataRow = 2
'this next one should be a column
'on the PlacementsSheet that has
'data from firstPlacementDataRow all
'the way down to the last entry on the sheet
Const alwaysUsedColumn = "A"
Dim dumpWB As Workbook
Dim dumpWS As Worksheet
Dim nextDumpRow As Long
Dim placementWS As Worksheet
Dim lastPlacementRow As Long
Dim filePath As String
filePath = ThisWorkbook.Path & Application.PathSeparator _
& dumpWBName
If Dir$(filePath) = "" Then
'dump.xls not found, just quit
Exit Sub
End If
'see if we have anything to be copied
Set placementWS = ThisWorkbook.Worksheets(placementSheetName)
lastPlacementRow = placementWS.Range(alwaysUsedColumn & _
Rows.Count).End(xlUp).Row
If lastPlacementRow < firstPlacementDataRow Then
'no data to be transferred, just quit
Set placementWS = Nothing ' cleanup
Exit Sub
End If
'we do have work to do, continue on
'
'hide the operations from view
Application.ScreenUpdating = False
'no nags from the system
Application.DisplayAlerts = False
'open dump.xls without updating links
'dump.xls becomes the active workbook
Workbooks.Open filePath, 0
'back to this workbook
'set reference to dump.xls
Set dumpWB = Workbooks(dumpWBName)
'reference to the dump sheet
Set dumpWS = dumpWB.Worksheets(dumpWSName)
'find next available row on dump sheet
nextDumpRow = dumpWS.Range(alwaysUsedColumn & Rows.Count). _
End(xlUp).Offset(1, 0).Row ' never less than 2
If nextDumpRow < firstDumpRow Then
'safety net "just in case"
nextDumpRow = firstDumpRow
End If
'copy the rows
placementWS.Rows(firstPlacementDataRow & ":" & _
lastPlacementRow).Copy
'paste into dump.xls
dumpWS.Range("A" & nextDumpRow).PasteSpecial xlPasteAll
'delete the rows in this workbook
placementWS.Rows(firstPlacementDataRow & ":" & _
lastPlacementRow).Delete
'done, close up and go home
dumpWB.Close True
Application.DisplayAlerts = True
Set dumpWS = Nothing
Set dumpWB = Nothing
Set placementWS = Nothing
End Sub
"Janelle S" wrote:
Hi - I am after a VBA code that opening one workbook (placements) will
automatically open another workbook (dump) and then cut and paste rows from
the (dump) workbook into the (placements) workbook. Rows in (dump) will then
be deleted leaving it without data. The (dump) workbook will then save and
close and user will be returned to the (placements) workbook.
Hoping you can help. Thanks.
|