从Excel返回一个值
我有一个问题,从Excel表中获取价值。 我正在按照教程,但它不起作用。 我也包括Excel引用到word中,但仍然不能返回值
这是我的代码:
Sub Return_a_Value_from_Excel() Dim mySpreadsheet As Excel.Workbook Dim strSalesTotal As String Set mySpreadsheet = GetObject("C:\Documents\Book1.xls") strSalesTotal = mySpreadsheet.Application.Range("Test").Value Set mySpreadsheet = Nothing Selection.TypeText "Current sales total: $" & strSalesTotal & "." Selection.TypeParagraph End Sub
我只想返回在Excel中写入的值
你做错了,你应该遵循Excel对象模型即
应用程序 – >工作簿 – >表格 – >范围/图表
在下面的行中:
strSalesTotal = mySpreadsheet.Application.Range("Test").Value
你跳过表格,你的代码应该是
strSalesTotal = mySpreadsheet.Sheets("Sheet1").Range("A1").Value
请代之以“Sheet1”和“A1”
或者像这样尝试,如果你没有明确地定义它只在表单范围内有效:
strSalesTotal = mySpreadsheet.Range("Test").Value
要么
strSalesTotal = mySpreadsheet.[Test]