AW: Daten aus einer Excel-Tabelle im Netz auslesen
08.03.2005 14:38:44
Matthias
Hallo Rico,
es gibt eine Routine, die Daten aus einer geschlossenen Datei auslesen kann. Diese ist nicht von mir, ich habe sie aber in den Lösungsvorschlag mit eingebaut.
Zuerst mal in Modul1 diesen Code:
'VBA Function to Get a Value From a Closed File
'VBA does not include a method to retrieve a value from a closed file.
'You can, however, take advantage of Excel's ability to work with linked
'files.
'This tip contains a VBA function that retrieves a value from a closed workbook.
'It does by calling an XLM macro.
'The GetValue Function
'The GetValue function, listed below takes four arguments:
'path: The drive and path to the closed file (e.g., "d:\files")
'file: The workbook name (e.g., "99budget.xls")
'sheet: The worksheet name (e.g., "Sheet1")
'ref: The cell reference (e.g., "C4")
Function GetValue(path, file, sheet, ref)
' Retrieves a value from a closed workbook
Dim arg As String
' Make sure the file exists
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue = "File Not Found"
Exit Function
End If
' Create the argument
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
' Execute an XLM macro
GetValue = ExecuteExcel4Macro(arg)
End Function
Dann in Modul2, dieses:
Option Explicit
Public NextTime As Date
Sub LeseWerte()
Dim Wert1, Wert2
Dim p$, f$, s$, a$
p = "\\server\directory"
f = "Dateiname.xls"
s = "123" 'Blatt
a = "F50" 'Zelle
Wert1 = GetValue(p, f, s, a)
s = "456" 'anderes Blatt
Wert2 = GetValue(p, f, s, a)
NextTime = Now + TimeValue("00:00:15") 'alle 15 Sekunden
Application.StatusBar = "Wert1:" & Format(Wert1, "0,0%") & _
" - Wert2:" & Format(Wert2, "0,0%") & " (Nächste Aktualisierung: " & NextTime & ")"
Application.OnTime NextTime, "LeseWerte"
End Sub
Sub LesenStop()
On Error Resume Next
Application.OnTime EarliestTime:=NextTime, Procedure:="LeseWerte", Schedule:=False
On Error GoTo 0
Application.StatusBar = False
End Sub
und, sehr wichtig, ins Modul "DieseArbeitsmappe":
Private Sub Workbook_Activate()
LeseWerte
End Sub
Private Sub Workbook_Deactivate()
LesenStop
End Sub
Beim Aktivieren der Mappe wird also eine Prozedur gestartet, die die Werte ausliest und in der Statusleiste anzeigt. (Wenn du es in Textboxen haben willst, kannst du es ja anpassen). Gleichzeitig wird die Zeit der nächsten ausführung festgelegt (im Beispiel 15 Sekunden zu Testzwecken).
Beim Deaktivieren der Mappe wird die OnTime-Prozedur gelöscht (und die Statuszeile in den Standardzustand versetzt).
Die Pfade in Modul2 müssen noch angepasst werden!
Ich hoffe, du kommst klar. Wenn nicht, nachfragen!
Grüße,
Matthias