|
|
MS Excel - Берём значения (формулы) из ячеек
Простой пример:
Берём значения пары ячеек из книги MS Excel и вставляем в форму (или куда угодно)
Код из примера:
Private Sub GetDataFrom_ExcelWB(wbSoursePath As String)
Dim objExcelApp As Object
Dim objWorkbook As Object
Dim objWorkSheet As Object
Dim s$, vVal As Variant
On Error GoTo GetDataFrom_ExcelWB_Err
If Dir(wbSoursePath) = "" Then
MsgBox "Путь не указан!", vbCritical
Exit Sub
End If
Set objExcelApp = CreateObject("Excel.Application")
Set objWorkbook = objExcelApp.Workbooks.Open(wbSoursePath)
Set objWorkSheet = objWorkbook.WorkSheets(2)
With objWorkSheet
Debug.Print "Имя листа: " & objWorkSheet.Name
s = "D9"
vVal = .Range(s).Value
Debug.Print s & ": " & vVal
s = "E9"
vVal = .Range(s).FormulaR1C1
Debug.Print s & ": " & vVal
End With
GetDataFrom_ExcelWB_Bye:
On Error Resume Next
objExcelApp.Visible = True
Set objWorkSheet = Nothing
Set objWorkbook = Nothing
Set objExcelApp = Nothing
Err.Clear
Exit Sub
GetDataFrom_ExcelWB_Err:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"in procedure GetDataFrom_ExcelWB", vbCritical, "Error!"
Resume GetDataFrom_ExcelWB_Bye
End Sub
Ещё вариант:
Private Sub GetDataFrom_ExcelWB(wbSoursePath As String)
Dim objExcelApp As Object
Dim objWorkBook As Object
Dim objWorkSheet As Object
Dim s$, sTmp$, iVal%, vVal As Variant
On Error GoTo GetDataFrom_ExcelWB_Err
If Dir(wbSoursePath) = "" Then
MsgBox "Путь не указан!", vbCritical
Exit Sub
End If
Set objExcelApp = CreateObject("Excel.Application")
Set objWorkBook = objExcelApp.Workbooks.Open(wbSoursePath)
Set objWorkSheet = objWorkBook.WorkSheets(2)
With objWorkSheet
For iVal = 78 To 90
s = "A" & Chr$(iVal) & "6"
vVal = .Range(s).FormulaR1C1
Debug.Print s & ": " & vVal
Next iVal
End With
GetDataFrom_ExcelWB_Bye:
On Error Resume Next
objExcelApp.Visible = True
Set objWorkSheet = Nothing
Set objWorkBook = Nothing
Set objExcelApp = Nothing
Err.Clear
Exit Sub
GetDataFrom_ExcelWB_Err:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"in procedure GetDataFrom_ExcelWB", vbCritical, "Error!"
Resume GetDataFrom_ExcelWB_Bye
End Sub
|
|