Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB & Excel

Status
Not open for further replies.

goldkn222

New member
Jan 25, 2002
2
US
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!
Mike
 
Replies continue below

Recommended for you

Can you paste some of your code? DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
DSI,
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()
CommonDialog1.ShowOpen
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.Range("A1").Select
xlSht.Cells(1, 1).Value = 0
xlSht.Cells.SpecialCells(xlCellTypeLastCell).Select

' 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.Legend.Clear
xlChart.ChartArea.Font.Size = 12
xlChart.ChartArea.Font.Color = vbBlue
xlChart.ChartArea.Select
xlChart.ChartArea.Copy
Picture1.Picture = Clipboard.GetData(vbCFtiff)


End Sub
 
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.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top