![]() |
Tipp 0115
|
Excel-VBE - Worksheet-Ereignis hinzufügen
|
 |
|
Autor/Einsender: Datum: |
|
Angie 29.08.2001 |
|
Entwicklungsumgebung: |
|
VB 5 |
|
|
In diesem Beispiel wird gezeigt, wie in Excel eine neue Arbeitsmappe geöffnet
und ein Worksheet-Ereignis, hier das Worksheet_SelectionChange-Ereignis für
"Tabelle1", hinzugefügt werden kann. Der so dynamisch hinzugefügte Code kann
sowohl im Excel-VB-Editor weiter bearbeitet werden als auch mit der Arbeitsmappe
abgespeichert werden.
|
|
|
Option Explicit
Dim xlAppl As Object
Dim xlApplLiefNicht As Boolean
Dim xlWB As Object
Dim xlWBName As String
Dim xlTabellenName As String
Private Sub Form_Initialize()
On Error Resume Next
Set xlAppl = GetObject(, "Excel.Application")
If Err.Number <> 0 Then xlApplLiefNicht = True
Err.Clear
End Sub
Sub xlStarten()
On Error GoTo errorMsgExcel
If xlAppl Is Nothing Then _
Set xlAppl = CreateObject("Excel.Application")
On Error GoTo errorMsgXLMappe
Set xlWB = xlAppl.Workbooks.Add
On Error GoTo 0
xlWBName = xlWB.Name
Call Ereignis_Tabelle1_Einfuegen
xlAppl.Application.Visible = True
Exit Sub
errorMsgExcel:
MsgBox "Konnte keine Verbindung zu Excel herstellen !", _
16, "Problem"
Exit Sub
errorMsgXLMappe:
MsgBox "Es traten Probleme bei der Erstellung" & _
" einer neuen Arbeitsmappe auf !", 16, "Problem"
If xlApplLiefNicht Then xlAppl.Application.Quit
Set xlAppl = Nothing
Exit Sub
End Sub
Sub Ereignis_Tabelle1_Einfuegen()
Dim neuesEreignis As Object
Dim strCode As String
xlTabellenName = "Tabelle1"
Set neuesEreignis = xlWB.VBProject.VBComponents _
(xlTabellenName).CodeModule
strCode = _
"Private Sub Worksheet_SelectionChange(ByVal _" & vbCr & _
" Target As Excel.Range)" & vbCr & _
" 'Bei Klick in einer Zelle wird der aktiven" & vbCr & _
" 'Zelle die Farbe hellgrün zugewiesen und als" & vbCr & _
" 'Zellwert die Zell-Adresse hinzugefügt" & vbCr & _
" With ActiveCell" & vbCr & _
" .Interior.ColorIndex = 35" & vbCr & _
" .Value = ActiveCell.AddressLocal" & vbCr & _
" End With" & vbCr & _
"End Sub"
neuesEreignis.AddFromString strCode
Set neuesEreignis = Nothing
End Sub
Private Sub cmdBeenden_Click()
On Error Resume Next
If Not xlAppl Is Nothing Then
If Not xlWB Is Nothing Then
xlWB.Close SaveChanges:=False
Set xlWB = Nothing
End If
If xlApplLiefNicht Then xlAppl.Application.Quit
Set xlAppl = Nothing
End If
End Sub
|
|
|
|
|
Excel-Ereignisse im Überblick |
|
|
|
Um diesen Tipp ausführen zu können, muss Excel installiert sein und zur Entwurfszeit die Microsoft Excel x.0 Object Library in das
Projekt eingebunden werden.
|
Für die VBE-Programmierung ist zusätzlich das Einbinden der Objektbibliothek
Visual Basic 6.0 Extensibility notwendig.
|
|
Windows-Version |
95 |
 |
|
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
VB-Version |
VBA 5 |
 |
|
VBA 6 |
 |
|
VB 4/16 |
 |
|
VB 4/32 |
 |
|
VB 5 |
 |
|
VB 6 |
 |
|
|
|
Download (4,6 kB)
|
Downloads bisher: [ 1236 ]
|
|
|