LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.sqlserver.dts,microsoft.public.sqlserver.programming,microsoft.public.excel.programming,microsoft.public.scripting.vbscript
external usenet poster
 
Posts: 4
Default vb script in dts throws error when operating on Excel file - helpplease!

On Oct 5, 6:50 pm, Tom Lavedas wrote:
On Oct 5, 1:17 pm, Edward wrote:





SQL Server 2000
DTS
VBScript
Excel


I have to manipulate an Excel file within a DTS package. Put simply,
I need to move the contents of certain cells in one Worksheet into
diffferent cells on another worksheet.


Here's my code:


Function Main


Dim e_app
Dim e_wbook
Dim e_wksheet1
Dim e_wksheet2
Dim e_wksheet3
Dim sFilename


sFilename = "\\Server1\Shared\Import\ManagerQuestionnaire. xls"


' Create the Excel Object
Set e_app = CreateObject("Excel.Application")


' Open up the Excel Spreadsheet
Set e_wbook = e_app.Workbooks.Open(sFilename)


' Which sheet do we get our data from
Set e_wksheet1 = e_wbook.Worksheets("Sheet1")
Set e_wksheet2 = e_wbook.Worksheets("Sheet2")


e_wksheet1.Range("A7").Select
e_wksheet1.Copy


e_wksheet2.Select <-- BARFS HERE WITH "Select method of worksheet
class failed"
e_wksheet2.Range("A1").Select <-- If I remove the above line it
barfs with "Select method of Range class failed"


I've tried setting e_wksheet2 to the e_wbook.Worksheets("Sheet1")
(same as e_wksheet1) but it still fails. The code parses correctly.
It obviously gets beyond the Select method call on e_wksheet1 so why
is it failing on e_wksheet2?


Thanks


Edward


Try directly addressing the ranges that you want to use instead of the
very problematic cell Select approach that the macro recorder tends to
provide, something like this ...

' Which sheet do we get our data from
Set e_wksheet1 = e_wbook.Worksheets("Sheet1")
Set e_wksheet2 = e_wbook.Worksheets("Sheet2")

e_wksheet2.Range("A1") = e_wksheet1.Range("A7")


Hi Tom

This approach worked really well - thanks.

I also need to copy a range of cells, and this is the approach that
I'm trying:

e_wksheet3.Range("A1:I3") = e_wksheet1.Range("A19:I22")

Although the script runs without error, the target worksheet is
blank. However, if I substitute this line with

e_wksheet3.Range("A1") = e_wksheet1.Range("A19")

the cell at A1 is populated.

Any thoughts?

Thanks

Edward
 
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
VBS Script and ClearContents -- Code Throws An Error garibaldi Excel Programming 4 November 15th 07 03:45 AM
Find Throws Error 91 Dave Birley Excel Programming 13 May 17th 07 07:01 PM
Excel 2000 - Excel 2003 now throws 'runtime error' Ralph L Excel Programming 3 April 10th 06 05:20 AM
How do I perform a certain function if VBA throws up an error? Matt[_37_] Excel Programming 2 February 24th 06 03:06 PM


All times are GMT +1. The time now is 04:22 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"