return a cell value

jp3892
10-01-2004, 06:58 AM
How do u return the value of a cell from an open Excel workbook? I have opened 2 workbooks (xlWkb1 and xlWkb2) and I need to get the value from cell ("a50") into the variable "my_val". Here's part of the code:

Dim xlApp As Excel.Application
Set xlApp = New Excel.Application

Dim xlWkb2 As Excel.Workbook
Set xlWkb2 = xlApp.Workbooks.Open("C:\Inverse Program\d13Results()\data" & ".xls")
Dim xlSht2 As Excel.Worksheet
Set xlSht2 = xlWkb2.Worksheets(1)

For i = 0 To 0

Dim xlWkb1 As Excel.Workbook
Set xlWkb1 = xlApp.Workbooks.Open("C:\Inverse Program\d13Results()\" & i & ".xls")
Dim xlSht1 As Excel.Worksheet
Set xlSht1 = xlWkb.Worksheets(1)
Dim xlChart As Excel.Chart
Set xlChart = xlWkb.Charts.Add
Dim chrtitle As String
Dim my_val As Single

'add data to the chart

xlChart.ChartType = xlXYScatter
xlChart.SetSourceData xlSht.Range("k:k,l:l"), xlColumns
chrtitle = xlSht.Range("b1").Value
xlChart.Visible = xlSheetVisible
xlChart.Legend.Clear
xlChart.Axes(xlValue).HasMajorGridlines = False

'get value for "a50"

xlWkb2.Activate
my_val = ActiveCell.Offset(50,0).Value

rick_deacha
10-01-2004, 08:13 AM
You could make reference to the Range itself

xlWkb2.Activate
my_val = Range("A50").Value

jp3892
10-01-2004, 08:24 AM
No, it has to be relative because I want to move around the sheet according to a loop index number. It DOES work though so what is the problem with my code?!

rick_deacha
10-01-2004, 08:30 AM
Well then use the Cells Object
xlWkb2.Activate
my_val = Cells(50,1).Value ' Cells(row,column)
BTW you didnt mentioned that it was needed in a loop :-\

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum