Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VB & Excel

VB & Excel

VB & Excel

I am trying to write a BV program that calls an excel file and graphs the data.  I have figured out how to pull in the file but I am having difficulty in programming the part that tells Excel which cells to choose for the chart.  I have tried the methods via MSDN but I get messages about incorrect objet class types and other various bugs during compile time.   The range of cells I need to graph change with each file and I am trying to automate this by finding the last row of the file to figure the range.  I already know what columns to use and I can hard wire that it.  

Thanks in advance for any help!

RE: VB & Excel

Can you paste some of your code?

While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: VB & Excel

Thanks for you help...here is the code I am working on.  I am kind of new to VB but I love working with it ..except I think I am going to lose my mind during the learning process.  
Thanks again
Mike B

Private Sub Command1_Click()
FileName = CommonDialog1.FileName   Dim xlApp As Excel.Application
    Set xlApp = New Excel.Application
    Dim xlWkb As Excel.Workbook
    Set xlWkb = xlApp.Workbooks.Open(FileName)
    Dim xlSht As Excel.Worksheet
    Set xlSht = xlWkb.Sheets(1)
    Dim xlChart As Excel.Chart
    Set xlChart = xlWkb.Charts.Add
 '   Selection.End(xlDown).Select    'Move the cursor to the bottom (Ctrl+End)
 '   xlSht.Rows.End(xlLastCell).Select
 '  LastRow = ActiveCell.Row        'Name LastRow as the number value of the row

'This is where I get the error messages....!! the commented
'code are things I have tried but also got error messages

   xlSht.Cells(1, 1).Value = 0

'    xlApp.Application.CountA(ActiveSheet.Range("A:A")) = LastRow

        xlChart.ChartType = xlLine
'This was hard wired in to make sure the chart worked but
'I will insert the last row cell variable in here
        xlChart.SetSourceData xlSht.Range("A2:b599"), xlColumns
        xlChart.Visible = xlSheetVisible
        xlChart.ChartArea.Font.Size = 12
        xlChart.ChartArea.Font.Color = vbBlue
        Picture1.Picture = Clipboard.GetData(vbCFtiff)
End Sub

RE: VB & Excel

I have found that the best way to handle charting macros in Excel is not to. Maybe a little clarification is in order.  What I mean is, using Excel (not VBA), chart some "dummy" data that has more data points than you expect your called-in data to be. (There's ways to dynamically adjust the charted column length, but I won't get into that here.) Format the chart to look like you want. Then delete the dummy data, but leave the chart.  When VBA imports your new data into the sheet, have it overwrite the space where the deleted dummy data used to be and the new data will be charted.  If, for some reason, you want the chart hidden until the new data comes in, move the chart to a hidden sheet and then have the VBA macro copy and paste a copy of it into the desired location (or switch to the hidden sheet.)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close