Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy spreadsheet into another file.


Looking for code to:

Copy "Master" spreadsheet to O:\Sales_Tax and rename spreadsheet to
Last Month. (For example February, if I executed the code today)

Thanks,

Met


--
Metrazal
------------------------------------------------------------------------
Metrazal's Profile: http://www.excelforum.com/member.php...o&userid=31648
View this thread: http://www.excelforum.com/showthread...hreadid=518833

  #2   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Copy spreadsheet into another file.

If I understand you correctly, you want to save a copy of the active workbook
with a filename that represents the previous month. If so, this should do it:

Sub SaveCopyAs_LastMonth()
Dim sFileName As String
sFileName = "O:\Sales_Tax" & MonthName(Month(Date) - 1) & ".xls"
ActiveWorkbook.SaveCopyAs sFileName
End Sub

=OR-
Did you want to copy the ActiveSheet into a new workbook and save it? If so,
post back.

Regards,
GS
  #3   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Copy spreadsheet into another file.

Oops! I forgot one separator. Change this line:

sFileName = "O:\Sales_Tax" & MonthName(Month(Date) - 1) & ".xls"

to:

sFileName = "O:\Sales_Tax\" & MonthName(Month(Date) - 1) & ".xls"

Regards,
GS


"GS" wrote:

If I understand you correctly, you want to save a copy of the active workbook
with a filename that represents the previous month. If so, this should do it:

Sub SaveCopyAs_LastMonth()
Dim sFileName As String
sFileName = "O:\Sales_Tax" & MonthName(Month(Date) - 1) & ".xls"
ActiveWorkbook.SaveCopyAs sFileName
End Sub

=OR-
Did you want to copy the ActiveSheet into a new workbook and save it? If so,
post back.

Regards,
GS

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy spreadsheet into another file.


I am looking to save it in an exsisting workbook called
O:\Sales_Tax.xls


Thanks,
Met


--
Metrazal
------------------------------------------------------------------------
Metrazal's Profile: http://www.excelforum.com/member.php...o&userid=31648
View this thread: http://www.excelforum.com/showthread...hreadid=518833

  #5   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Copy spreadsheet into another file.

Okay, I understand now. Here's some code that should work:

Sub MoveSheetsToOtherBook()
' Moves the selected sheet(s) to a specified workbook
' If the workbook isn't open, it opens it.
' If the file doesn't exist, it exits.
' Requires bBookIsOpen(), bFileExists functions

Dim wbkTarget As Workbook, wbkSource As Workbook
Dim Shts As Sheets

Const sPath As String = "O:\"
Const sName As String = "Sales_Tax.xls"

Set wbkSource = ActiveWorkbook 'or ThisWorkbook

'If wbkSource has been saved, you can't move all its sheets.
'If wbkSource was created from a template but not saved yet,
'delete the following line and its corresponding End If.
If Not ActiveWindow.SelectedSheets.Count = ActiveWorkbook.Sheets.Count Then
Set Shts = ActiveWindow.SelectedSheets

'Get a reference to wbkTarget
If Not bBookIsOpen(sName) Then
If bFileExists(sPath & sName) Then
Set wbkTarget = Workbooks.Open(sPath & sName)
Else
MsgBox "The target file does not exist !", vbExclamation + vbOKOnly
Exit Sub
End If
Else
Set wbkTarget = Workbooks(sName)
End If

'Move the sheet(s) to wbkTarget, save & close it.
wbkSource.Sheets(Shts).Move
after:=wbkTarget.Sheets(wbkTarget.Sheets.Count)
With wbkTarget
.Save
.Close
End With
End If

End Sub


Function bBookIsOpen(wbkName) As Boolean
' Checks if a specified workbook is open.
'
' Arguments: wbkName The name of the workbook
'
' Returns: True if the workbook is open

Const sSource As String = "bBookIsOpen()"

Dim x As Workbook
On Error Resume Next
Set x = Workbooks(wbkName)
bBookIsOpen = (Err = 0)

End Function


Function bFileExists(fileName As String) As Boolean
' Checks if a file exists in the specified folder
'
' Arguments: fileName The fullname of the file
'
' Returns: TRUE if the file exists

Const sSource As String = "bFileExists()"

On Error Resume Next
bFileExists = (Dir$(fileName) < "")

End Function

'**
Regards,
GS


  #6   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Copy spreadsheet into another file.

Oops! You did say copy, ..not move. -Sorry about not catching that!
Replace MoveSheetsToOtherBook() with this:

Sub CopySheetToOtherBook()
' Copies the active sheet to a specified workbook.
' If the workbook isn't open, it opens it.
' If the file doesn't exist, it exits.
' Requires bBookIsOpen(), bFileExists functions

Dim wks As Worksheet, wbkTarget As Workbook
Dim sNewName As String

Const sPath As String = "O:\"
Const sName As String = "Sales_Tax.xls"

Set wks = ActiveSheet
sNewName = MonthName(Month(Date) - 1)


'Get a reference to wbkTarget
If Not bBookIsOpen(sName) Then
If bFileExists(sPath & sName) Then
Set wbkTarget = Workbooks.Open(sPath & sName)
Else
MsgBox "The target file does not exist !", vbExclamation + vbOKOnly
Exit Sub
End If
Else
Set wbkTarget = Workbooks(sName)
End If

'Move the sheet(s) to wbkTarget, save & close it.
wks.Copy after:=wbkTarget.Sheets(wbkTarget.Sheets.Count)
With wbkTarget
ActiveSheet.Name = sNewName
.Save
.Close
End With

End Sub


Regards,
GS
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I copy spreadsheet data as shown to another spreadsheet? trainer07 Excel Discussion (Misc queries) 2 August 7th 06 09:39 PM
In Excel I want to copy text from spreadsheet to spreadsheet Kris Excel Worksheet Functions 3 June 9th 06 07:58 PM
Unable to copy another spreadsheet to the current spreadsheet with rmcompute Excel Programming 2 November 18th 05 01:19 AM
using vba copy data from .csv file into an excel spreadsheet vineeta Excel Programming 1 October 31st 03 12:57 PM
How to open another Excel spreadsheet to copy data into current spreadsheet ? Ricky Pang Excel Programming 0 July 13th 03 01:59 PM


All times are GMT +1. The time now is 09:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"