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 File into Worksheet


Hi. I need to copy a xls file into an existing worksheet. For example,
am trying to copy the cell contents of test.xls from A4 to Z9 into
worksheet that I already have called, "TestSheet". How would I go abou
this?

I am currently opening the file through this code, but it open up a
entire new workbook. I need it to open in one of my exisitin
workbooks.worksheet("TestSheet"). Thanks!

<VBA
Function CopyData(sFileName As String, sSheetNo As String) As Boolean

Dim xlBook As Workbook
Dim xlSheet As Worksheet
Dim sFilePath As String
Dim LastRow As Long
Dim LastCol As Long

Const MyDir As String = "C:\temp\"

'sFilePath = ThisWorkbook.Path & "\" & sFileName
sFilePath = MyDir & sFileName
If Dir(sFilePath) = "" Then
CopyData = False
Exit Function
End If

Set xlBook = Workbooks.Open(sFilePath)
Set xlSheet = xlBook.Worksheets("Sheet1")

LastRow = xlSheet.Range("A9999").End(xlUp).Row
LastCol = xlSheet.Range("P1").End(xlToLeft).Column

xlSheet.Range("A4:" & "Z9" & LastRow).Copy
ThisWorkbook.Sheets("sheet" & sSheetNo).Range("a1") _
.PasteSpecial xlPasteValues

Set xlSheet = Nothing
Set xlBook = Nothing

CopyData = True

End Function
</VBA

--
Session10
-----------------------------------------------------------------------
Session101's Profile: http://www.excelforum.com/member.php...fo&userid=1405
View this thread: http://www.excelforum.com/showthread.php?threadid=52756

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Copy File into Worksheet

Function CopyData(sFileName As String, sSheetNo As String) As Boolean

Dim xlBook As Workbook
Dim xlSheet As Worksheet
Dim sFilePath As String
Dim LastRow As Long
Dim LastCol As Long

Const MyDir As String = "C:\temp\"

Application.ScreenUpdating = False

'sFilePath = ThisWorkbook.Path & "\" & sFileName
sFilePath = MyDir & sFileName
If Dir(sFilePath) = "" Then
CopyData = False
Exit Function
End If

Set xlBook = Workbooks.Open(sFilePath)
Set xlSheet = xlBook.Worksheets("Sheet1")

LastRow = xlSheet.Range("A65536").End(xlUp).Row
'LastCol = xlSheet.Range("IV4").End(xlToLeft).Column

xlSheet.Range("A4:" & "Z" & LastRow).Copy
ThisWorkbook.Sheets("sheet" & sSheetNo).Range("a1") _
.PasteSpecial xlPasteValues

Set xlSheet = Nothing
xlBook.Close Savechanges:=False
Set xlBook = Nothing

Application.ScreenUpdating = True
CopyData = True

End Function

Assume you are calling this function from VBA only.

--
Regards,
Tom Ogilvy

"Session101" wrote:


Hi. I need to copy a xls file into an existing worksheet. For example, I
am trying to copy the cell contents of test.xls from A4 to Z9 into a
worksheet that I already have called, "TestSheet". How would I go about
this?

I am currently opening the file through this code, but it open up an
entire new workbook. I need it to open in one of my exisiting
workbooks.worksheet("TestSheet"). Thanks!

<VBA
Function CopyData(sFileName As String, sSheetNo As String) As Boolean

Dim xlBook As Workbook
Dim xlSheet As Worksheet
Dim sFilePath As String
Dim LastRow As Long
Dim LastCol As Long

Const MyDir As String = "C:\temp\"

'sFilePath = ThisWorkbook.Path & "\" & sFileName
sFilePath = MyDir & sFileName
If Dir(sFilePath) = "" Then
CopyData = False
Exit Function
End If

Set xlBook = Workbooks.Open(sFilePath)
Set xlSheet = xlBook.Worksheets("Sheet1")

LastRow = xlSheet.Range("A9999").End(xlUp).Row
LastCol = xlSheet.Range("P1").End(xlToLeft).Column

xlSheet.Range("A4:" & "Z9" & LastRow).Copy
ThisWorkbook.Sheets("sheet" & sSheetNo).Range("a1") _
.PasteSpecial xlPasteValues

Set xlSheet = Nothing
Set xlBook = Nothing

CopyData = True

End Function
</VBA


--
Session101
------------------------------------------------------------------------
Session101's Profile: http://www.excelforum.com/member.php...o&userid=14054
View this thread: http://www.excelforum.com/showthread...hreadid=527567


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
Copy a worksheet from one file to another [email protected] Excel Discussion (Misc queries) 1 May 15th 08 03:48 AM
Copy worksheet from one file to another pokdbz Excel Discussion (Misc queries) 6 September 28th 07 09:23 PM
copy part of a worksheet into a worksheet in the same file/keepi. JTB Excel Worksheet Functions 1 September 23rd 06 09:13 AM
How do I copy a worksheet from a workbook as csv file Husker87 Excel Discussion (Misc queries) 6 March 26th 05 09:41 AM
copy a worksheet to another xls file Jac[_2_] Excel Programming 2 November 30th 03 05:13 PM


All times are GMT +1. The time now is 10:38 PM.

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"