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
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