PC Review


Reply
Thread Tools Rate Thread

Copy a range of information to a worksheet in a specific file

 
 
=?Utf-8?B?VGhlbyBEZWdy?=
Guest
Posts: n/a
 
      22nd Jun 2007
Below is some code that was created with the help of this sight as well as
some reference books. The code works wonders for what I want it to do but I
would like to improve upon it. Currently I print my worksheet, Copy it to a
new worksheet, Save the Work Sheet use a cell location for the name of the
file, and then it clears the worksheet. What I would like to improve with
this code would be to have it copy the information to another file located in
another directory (example C:\"Original Directory" to c:\"New Directory"
Could someone offer me a suggestion as to how to accomplish this. The code is
posted below. Thanks
Sub All_in_One()

' Prints the Time Sheet
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

' Copies the Time Sheet to the Time Record Tab
Dim sh1 As Worksheet, sh2 As Worksheet
Dim rng1 As Range, rng2 As Range
Set sh1 = Worksheets("Time Sheet")
Set sh2 = Worksheets("Time Record")
Set rng1 = sh1.Range("a11:AE26")
Set rng2 = GetRealLastCell(sh2)
Set rng2 = sh2.Cells(rng2.Row + 1, 1)
rng1.Copy
rng2.PasteSpecial xlValues

' Clears the Time Sheet
Range("C1216").Select
Selection.ClearContents
Range("F12:O16").Select
Selection.ClearContents
Range("C2125").Select
Selection.ClearContents
Range("F21:O25").Select
Selection.ClearContents
ActiveWindow.SmallScroll Down:=-9
Range("F1").Select

' Saves the Time Sheet to a new File Naming it by the Employees Name

Dim CurrentWorkbook As Workbook
Dim NewWorkbook As Workbook
Dim Rng As Range

Set CurrentWorkbook = ActiveWorkbook
Set NewWorkbook = Workbooks.Open(Filename:="Time.xls")
CurrentWorkbook.Sheets(Array("Time Sheet")).Copy
after:=NewWorkbook.Worksheets(1)
Set Rng = Sheets("Time Sheet").Range("b5")
ActiveWorkbook.SaveAs _
Filename:=Rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal
NewWorkbook.Close savechanges:=False
CurrentWorkbook.Close savechanges:=False





End Sub

Public Function GetRealLastCell(sh As Worksheet) As Range
Dim RealLastRow As Long
Dim RealLastColumn As Long
On Error Resume Next
RealLastRow = _
sh.Cells.Find("*", sh.Range("a1"), , , xlByRows, xlPrevious).Row
RealLastColumn = _
sh.Cells.Find("*", sh.Range("a1"), , , xlByColumns, xlPrevious).Column
If RealLastRow < 1 Then RealLastRow = 1
If RealLastColumn < 1 Then RealLastColumn = 1
Set GetRealLastCell = sh.Cells(RealLastRow, RealLastColumn)
End Function


 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      22nd Jun 2007
ActiveWorkbook.SaveAs _
Filename:=Rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal

would become

newWorkbook.SaveAs _
Filename:="C:\New Directory\" &Rng.Value & ".xls", _
FileFormat:=xlWorkbookNormal


assuming "C:\New Directory" already exists.

--
Regards,
Tom Ogilvy

"Theo Degr" wrote:

> Below is some code that was created with the help of this sight as well as
> some reference books. The code works wonders for what I want it to do but I
> would like to improve upon it. Currently I print my worksheet, Copy it to a
> new worksheet, Save the Work Sheet use a cell location for the name of the
> file, and then it clears the worksheet. What I would like to improve with
> this code would be to have it copy the information to another file located in
> another directory (example C:\"Original Directory" to c:\"New Directory"
> Could someone offer me a suggestion as to how to accomplish this. The code is
> posted below. Thanks
> Sub All_in_One()
>
> ' Prints the Time Sheet
> ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
>
> ' Copies the Time Sheet to the Time Record Tab
> Dim sh1 As Worksheet, sh2 As Worksheet
> Dim rng1 As Range, rng2 As Range
> Set sh1 = Worksheets("Time Sheet")
> Set sh2 = Worksheets("Time Record")
> Set rng1 = sh1.Range("a11:AE26")
> Set rng2 = GetRealLastCell(sh2)
> Set rng2 = sh2.Cells(rng2.Row + 1, 1)
> rng1.Copy
> rng2.PasteSpecial xlValues
>
> ' Clears the Time Sheet
> Range("C1216").Select
> Selection.ClearContents
> Range("F12:O16").Select
> Selection.ClearContents
> Range("C2125").Select
> Selection.ClearContents
> Range("F21:O25").Select
> Selection.ClearContents
> ActiveWindow.SmallScroll Down:=-9
> Range("F1").Select
>
> ' Saves the Time Sheet to a new File Naming it by the Employees Name
>
> Dim CurrentWorkbook As Workbook
> Dim NewWorkbook As Workbook
> Dim Rng As Range
>
> Set CurrentWorkbook = ActiveWorkbook
> Set NewWorkbook = Workbooks.Open(Filename:="Time.xls")
> CurrentWorkbook.Sheets(Array("Time Sheet")).Copy
> after:=NewWorkbook.Worksheets(1)
> Set Rng = Sheets("Time Sheet").Range("b5")
> ActiveWorkbook.SaveAs _
> Filename:=Rng.Value & ".xls", _
> FileFormat:=xlWorkbookNormal
> NewWorkbook.Close savechanges:=False
> CurrentWorkbook.Close savechanges:=False
>
>
>
>
>
> End Sub
>
> Public Function GetRealLastCell(sh As Worksheet) As Range
> Dim RealLastRow As Long
> Dim RealLastColumn As Long
> On Error Resume Next
> RealLastRow = _
> sh.Cells.Find("*", sh.Range("a1"), , , xlByRows, xlPrevious).Row
> RealLastColumn = _
> sh.Cells.Find("*", sh.Range("a1"), , , xlByColumns, xlPrevious).Column
> If RealLastRow < 1 Then RealLastRow = 1
> If RealLastColumn < 1 Then RealLastColumn = 1
> Set GetRealLastCell = sh.Cells(RealLastRow, RealLastColumn)
> End Function
>
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to open file and copy information from worksheet Ragav Microsoft Excel Programming 1 20th Sep 2011 03:30 AM
Copy only specific information from one colum to another worksheet Brandon Microsoft Excel Misc 2 19th Nov 2008 04:07 AM
Copy a range from a CSV file in a webpage to my local worksheet Jav Pa Microsoft Excel Programming 4 25th Aug 2004 01:57 AM
Count specific information in a certain range Microsoft Excel Worksheet Functions 1 17th Mar 2004 12:27 AM
Copy information from a specific range; not the entire worksheet. Paul Microsoft Excel Programming 9 12th Oct 2003 03:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:19 AM.