Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 132
Default Copy from another file and close automatically

I have the following code that opens to a predefined dir for the user to
select a file (the Subject), the contents of the only sheet are copied to a
specified sheet ("Ledger") in the Template file. I need to close the Subject
sheet after the contents are copied. Otherwise, I have several very large
sheets that remain open until closed manually. My code is an adaption of
other macros used elsewhere (thanks to the many contibutors here) and fails
at Selection.PasteSpecial. Sorry if it's a bit messy.

Sub OpenLedger()

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Select ""YES"" to proceed to Open a Job Ledger Data File, ""NO"" to
view Current File only"
Style = vbYesNoCancel + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Open a New Ledger Data File " ' Define title.

Response = MsgBox(Msg, Style, Title, Help, Ctxt)

If Response = vbYes Then
Dim myFileName As Variant
Dim wkbk As Workbook
Dim MyPath As String

MyPath = "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
MsgBox "Select a Job GL File to use"
ChDrive "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
ChDir "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
' ChDir MyPath - this didn't work
myFileName = Application.GetOpenFilename("Excel Files, *.xls")
If myFileName = False Then
Exit Sub 'user hit cancel
End If

'MsgBox myFileName & " has been selected" 'just to prove that you got it

'to open the file now that you have it:
Set wkbk = Workbooks.Open(Filename:=myFileName)
Else
Exit Sub
End If
ActiveSheet.Cells.Select
Selection.Copy
Application.DisplayAlerts = False
ActiveWindow.Close
Windows("JobCost Template V3.3.xls").Activate
Sheets("Ledger").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Sheets("Summary").Select
Application.DisplayAlerts = True

End Sub
--
Jim
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,316
Default Copy from another file and close automatically

After copying your data from the source workbook, add this line of code to
close it w/o saving:

wkbk .Close SaveChanges:=False

--
Kevin Backmann


"Jim G" wrote:

I have the following code that opens to a predefined dir for the user to
select a file (the Subject), the contents of the only sheet are copied to a
specified sheet ("Ledger") in the Template file. I need to close the Subject
sheet after the contents are copied. Otherwise, I have several very large
sheets that remain open until closed manually. My code is an adaption of
other macros used elsewhere (thanks to the many contibutors here) and fails
at Selection.PasteSpecial. Sorry if it's a bit messy.

Sub OpenLedger()

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Select ""YES"" to proceed to Open a Job Ledger Data File, ""NO"" to
view Current File only"
Style = vbYesNoCancel + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Open a New Ledger Data File " ' Define title.

Response = MsgBox(Msg, Style, Title, Help, Ctxt)

If Response = vbYes Then
Dim myFileName As Variant
Dim wkbk As Workbook
Dim MyPath As String

MyPath = "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
MsgBox "Select a Job GL File to use"
ChDrive "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
ChDir "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
' ChDir MyPath - this didn't work
myFileName = Application.GetOpenFilename("Excel Files, *.xls")
If myFileName = False Then
Exit Sub 'user hit cancel
End If

'MsgBox myFileName & " has been selected" 'just to prove that you got it

'to open the file now that you have it:
Set wkbk = Workbooks.Open(Filename:=myFileName)
Else
Exit Sub
End If
ActiveSheet.Cells.Select
Selection.Copy
Application.DisplayAlerts = False
ActiveWindow.Close
Windows("JobCost Template V3.3.xls").Activate
Sheets("Ledger").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Sheets("Summary").Select
Application.DisplayAlerts = True

End Sub
--
Jim

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 132
Default Copy from another file and close automatically

I amended the code to that below and still received an error on
"Selection.PastSpecial...".

It appears to me that the data copied to the clipboard is being lost on
close or I need to respond "yes" to the retain clipboard data prompt.

ActiveSheet.Cells.Select
Selection.Copy
Application.DisplayAlerts = False
wkbk.Close SaveChanges:=False
'ActveWindow.Close
Windows("JobCost Template V3.3.xls").Activate
Sheets("Ledger").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Sheets("Summary").Select
Range("A16").Select
Application.DisplayAlerts = True
--
Jim


"Kevin B" wrote:

After copying your data from the source workbook, add this line of code to
close it w/o saving:

wkbk .Close SaveChanges:=False

--
Kevin Backmann


"Jim G" wrote:

I have the following code that opens to a predefined dir for the user to
select a file (the Subject), the contents of the only sheet are copied to a
specified sheet ("Ledger") in the Template file. I need to close the Subject
sheet after the contents are copied. Otherwise, I have several very large
sheets that remain open until closed manually. My code is an adaption of
other macros used elsewhere (thanks to the many contibutors here) and fails
at Selection.PasteSpecial. Sorry if it's a bit messy.

Sub OpenLedger()

Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Select ""YES"" to proceed to Open a Job Ledger Data File, ""NO"" to
view Current File only"
Style = vbYesNoCancel + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Open a New Ledger Data File " ' Define title.

Response = MsgBox(Msg, Style, Title, Help, Ctxt)

If Response = vbYes Then
Dim myFileName As Variant
Dim wkbk As Workbook
Dim MyPath As String

MyPath = "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
MsgBox "Select a Job GL File to use"
ChDrive "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
ChDir "N:\MINING\PLANT\SERVICES\AXAPTA\Job Analysis\Seraji"
' ChDir MyPath - this didn't work
myFileName = Application.GetOpenFilename("Excel Files, *.xls")
If myFileName = False Then
Exit Sub 'user hit cancel
End If

'MsgBox myFileName & " has been selected" 'just to prove that you got it

'to open the file now that you have it:
Set wkbk = Workbooks.Open(Filename:=myFileName)
Else
Exit Sub
End If
ActiveSheet.Cells.Select
Selection.Copy
Application.DisplayAlerts = False
ActiveWindow.Close
Windows("JobCost Template V3.3.xls").Activate
Sheets("Ledger").Select
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Sheets("Summary").Select
Application.DisplayAlerts = True

End Sub
--
Jim

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
Automatically copy the file name to the worksheet tab Bob Excel Discussion (Misc queries) 3 July 12th 07 03:31 AM
Close automatically a userform FARAZ QURESHI Excel Discussion (Misc queries) 4 January 7th 07 07:46 PM
recrding macro,copy data thn close file, get messge abut clpbord PaddyBoy Excel Discussion (Misc queries) 2 October 25th 06 04:38 AM
Copy File Automatically on Opening It Dolores Excel Discussion (Misc queries) 4 December 30th 05 07:12 PM
Automatically Close Dialog Boxes chriskane Excel Worksheet Functions 2 August 9th 05 12:14 PM


All times are GMT +1. The time now is 09:40 PM.

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

About Us

"It's about Microsoft Excel"