Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > Database

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 10-09-2009, 03:39 PM
ChrisCione ChrisCione is offline
Registered User
 
Join Date: Oct 2009
Posts: 2
VB code to export data to Excel

I have placed a command button on my form in MS Access to export specific data to populate specific cells in Excel. I won't be pulling ALL the data from my main table, just specific fields, and auto-populating specific cells in an Excel document (template) that I have created.

I don't know how to write code at all, but I found this on the web:
Code:
Dim appExcel As Excel.Application
  Dim wbk As Excel.Workbook
  Dim wks As Excel.Worksheet
  Set appExcel = GetObject("Excel.Application")
  Set wbk = appExcel.Workbook.Open("L:\Bper Spec\Cione\TEST\TEST (RSA).xls", , True)
  Set wks = wbk.Worksheet("RSA")
With xlSheet
.Cells(1, 1).Value = "I am in Row 1, Column A"
.Cells(3, 3).Value = "I am in Row 3, Column C"
'and so on, filling in the cells
End With
When I try to run the code, I get a compile error "Variable not defined" at the 5th line from the bottom "With xlSheet"

I've researched and researched to find a solution, but no dice.

Any help would be appreciated.
Reply With Quote
  #2  
Old 10-10-2009, 03:31 AM
Ron Weller Ron Weller is offline
Senior Member
 
Join Date: Aug 2004
Location: Orange, California
Posts: 1,098
with xlSheet should be with wks
Reply With Quote
  #3  
Old 10-10-2009, 05:13 PM
ChrisCione ChrisCione is offline
Registered User
 
Join Date: Oct 2009
Posts: 2
Tried it; got a runtime error.
Reply With Quote
  #4  
Old 10-11-2009, 12:54 PM
Ron Weller Ron Weller is offline
Senior Member
 
Join Date: Aug 2004
Location: Orange, California
Posts: 1,098
Changes in red - New code should look like this:
Code:
  Dim appExcel As Excel.Application
  Dim wbk As Excel.Workbook
  Dim wks As Excel.Worksheet

  Set appExcel = GetObject("Excel.Application")
  Set wbk = appExcel.Workbooks.Open("L:\Bper Spec\Cione\TEST\TEST (RSA).xls", , True)
  Set wks = wbk.Worksheets("RSA")
  With wks
    .Cells(1, 1).Value = "I am in Row 1, Column A"
    .Cells(3, 3).Value = "I am in Row 3, Column C"
    'and so on, filling in the cells
  End With
By the way: Even the corrected code will continue to error because you open the workbook Read Only
Code:
 Set wbk = appExcel.Workbooks.Open("L:\Bper Spec\Cione\TEST\TEST (RSA).xls", , True)
Reply With Quote
  #5  
Old 10-12-2009, 08:19 AM
Hack's Avatar
Hack Hack is offline
Super Moderator
 
Join Date: Apr 2007
Location: Sterling Heights, Michigan
Posts: 7,719
Quote:
Originally Posted by ChrisCione View Post
got a runtime error.
Ron's code will probably solve your problem for this, but I would suggest that in the future if something like this (an error) occurs that you specify what the error number is and what the error verbage is.

There are only about a zillion differnent error possibilities. Telling us specifically which error you are getting helps enormously.

Thanks.
__________________
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista

Microsoft MVP 2005/2006/2007/2008/2009
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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 Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Scan ip mapping and fill correct data that have country code Mark107 .NET 5 07-21-2008 10:41 PM
Pls help me in How to import Excel data into Ms Access suing VB code P.Gopi Chand VB Classic 2 12-27-2006 11:43 PM
Microsoft's C++ bigotry Phil Weber .NET 632 10-01-2003 01:00 AM
Need to reduce network trips and speed up ADO VB code!! Richard Lloyd VB Classic 8 06-22-2001 05:52 AM
Bad programming practices encouraged.... namely VB! b.a.h. .NET 90 04-17-2001 01:45 AM


All times are GMT -4. The time now is 12:28 AM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.