Solved

vba in excel

Posted on 2004-10-20
10
147 Views
Last Modified: 2010-05-02
good greetings!

using visual basic for application in excel. how can i retrive and insert values from/to cell?

and how can i trigger a print.

thanks your help is greatly appreciated.
JackOfPH
0
Comment
Question by:JackOfPH
  • 7
  • 3
10 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Try include the M$ Excel Object Library from the References, basically the syntax is same either in VB or VBA.
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 100 total points
Comment Utility
This is an example to Print Excel Document:

Private Sub Command6_Click()
   
    Dim excel As excel.Application
    Dim wb As Workbook
    Dim ws As Worksheet
   
    Dim tmp As String
   
    tmp = "c:\test.xls"
   
    If tmp = "" Then Exit Sub
   
    Set excel = CreateObject("Excel.Application")
   
    'excel.Visible = True
   
    Set wb = excel.Workbooks.Open(tmp)
   
    Set ws = wb.Worksheets(1)
   
    ws.PrintOut
   
    Set ws = Nothing
    wb.Close
    Set wb = Nothing
    excel.Quit
    Set excel = Nothing
   
End Sub
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
to Preview it, just change line:

ws.PrintOut

to:

ws.PrintPreview
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Here comes an example to read/write an Excel document:

Private Sub Command5_Click()
    Dim excel As excel.Application
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim tmp As String
   
    tmp = "c:\test.xls"

    Set excel = CreateObject("Excel.Application")
    excel.Visible = True
   
    Set wb = excel.Workbooks.Open(tmp)
   
    Set ws = wb.Worksheets("Sheet1")
   
    i = 2
    msgbox ws.Cells(i, 3)
   ws.Cells(i, 3) = "hello world, now is " & now()
        msgbox ws.Cells(i, 3)

    wb.Save
   
    Set ws = Nothing
    wb.Close
    Set wb = Nothing
    excel.Quit
    Set excel = Nothing

    MsgBox "Done!"

End Sub
0
 
LVL 15

Author Comment

by:JackOfPH
Comment Utility
ryancys

it's been a while!

yuor code works! thanks but still I have a little a problem left.
how can I exclude a certain Cell from printing. lets say cell(13,5)?

please help

Jack
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
what is your mean of exclude a certain Cell from printing ?
0
 
LVL 15

Author Comment

by:JackOfPH
Comment Utility
i will not include when i print the sheet.
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Excel App print whatever shows on worksheet, if you don't want to show a value of particular cell, you can try to set the cell's value to empty first, then print it, and then restore your value back.

You can even set the row's height or column's width to 0 programmingly to hire it if necessary.

regards
0
 
LVL 15

Author Comment

by:JackOfPH
Comment Utility
ok the cell contain a textbox with its fill color to black how can i remove it? or how it will not show when i print the sheet.
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
Can help send me a copy of your file? I wil try look for you shortly. Find my email from my profile.

regards
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now