AllExperts > VB Script 
Search      
VB Script
Volunteer
Answers to thousands of questions
 Home · More VB Script Questions · Answer Library  · Encyclopedia ·
More VB Script Answers
Question Library

Ask a question about VB Script
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Miguel Zapico
Expertise
I can answer question about how to use scripts to consolidate data, connect different systems and automate tasks. I have no experience on using VBScript on web programming.

Experience
I have been using VBScript and Windows Scripting Host as my swiss tool for the last 6 years.

Organizations
New York PC users group (NYPC)
Independant Computer Consultants Association (ICCA)

Education/Credentials
Microsoft MCSE in Windows NT

 
   

You are here:  Experts > Computing/Technology > Basic > VB Script > VB script to copy/paste date in Excel

VB Script - VB script to copy/paste date in Excel


Expert: Miguel Zapico - 10/4/2006

Question
Hello,

I am trying to open an excel worksheet, paste data that will be in the clipboard to sheet 2.  Then copy the data from D1-D3, move to sheet 1, paste the data in C3, print sheet 1, and close the excel worksheet WITHOUT saving the changes.  Here is my script, which is not working for me.  I am using Macro Scheduler by MJNet.  

VBSTART
Option Explicit
Sub QTTankReport

Dim xlApp
Dim xlBook
Dim xlSheet

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("c:My DocumentsQT Tank Gauge.xls")
Set xlSheet = xlBook.Worksheets("Sheet2")

Range("D1:D46").Copy
set xlSheet = xlBook.Worksheets("Sheet1")
Range("C3").Select
Paste

PrintOut Copies=1, Collate=True
wb.Close False

End Sub

Answer
I have tweaked the script, the main issue is that you are declaring and setting the objects but you are not using them afterwards.  See also some comments on the body.

I have done this with Notepad:

VBSCRIPT
QTTankReport 'Call the sub

Sub QTTankReport

Dim xlApp
Dim xlBook
Dim xlSheet

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("c:\My Documents\QT Tank Gauge.xls")
Set xlSheet = xlBook.Worksheets("Sheet2")
xlapp.visible = true 'For debug purposes, show the Excel window

xlSheet.Range("D1:D46").Copy
set xlSheet = xlBook.Worksheets("Sheet1")
xlSheet.Activate 'The sheet needs to be active
xlSheet.Range("C3").Select
xlSheet.Paste

xlSheet.PrintOut , , 1, , , , True 'In the script, better use the long format
xlBook.Close False

End Sub

Hope this helps,
Miguel

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.