?
Solved

vba in excel

Posted on 2004-10-20
10
Medium Priority
?
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 53

Expert Comment

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

Accepted Solution

by:
Ryan Chong earned 400 total points
ID: 12365351
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 53

Expert Comment

by:Ryan Chong
ID: 12365355
to Preview it, just change line:

ws.PrintOut

to:

ws.PrintPreview
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Ryan Chong
ID: 12365381
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
ID: 12377946
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
 
LVL 53

Expert Comment

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

Author Comment

by:JackOfPH
ID: 12378262
i will not include when i print the sheet.
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 12378320
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
ID: 12378337
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 53

Expert Comment

by:Ryan Chong
ID: 12378359
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 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