Solved

vba in excel

Posted on 2004-10-20
10
150 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 52

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 52

Accepted Solution

by:
Ryan Chong earned 100 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 52

Expert Comment

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

ws.PrintOut

to:

ws.PrintPreview
0
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!

 
LVL 52

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 52

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 52

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 52

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 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