der folgende VBA Code schreibt Daten in den Bereich "Value" bzw. Spalte K.
Ich weiss aber nicht wie und wo das passiert.
_____________________________________________________________________________
Option Explicit
Option Base 1
Public oServer As OPCServer
Public TagGroups As OPCGroups
Public TagGroup As OPCGroup
Public Tags As OPCItems
Public TagServerHdl() As Long, TagErrors() As Long
Public OPCEvents As OPCEvents
Dim Errors() As Long, NoofTags As Long
Dim iAnzTags%
Private Sub Auto_Open()
Connect_Click
End Sub
Sub Connect_Click()
If [Status] = "Connected" Then
ActiveSheet.Shapes("OPC").TextFrame.Characters.Text = "Connect & Read"
DisconnectOPC
Else
ActiveSheet.Shapes("OPC").TextFrame.Characters.Text = "Disconnect"
OPCConnect
End If
End Sub
Private Sub OPCConnect()
'Bei err GoTo Connect_Error
Dim ProgID As String, Node
'Verbindungsversuch
Set oServer = New OPCServer
oServer.Connect ActiveSheet.[ProgID], ActiveSheet.[Server]
'Verb ok, generiere browser und tag gruppen
[Status] = "Connected"
Set TagGroups = oServer.OPCGroups
TagGroups.DefaultGroupIsActive = True
Set TagGroup = TagGroups.Add("Tags")
TagGroup.UpdateRate = 1000
TagGroup.IsSubscribed = True
Set Tags = TagGroup.OPCItems
Tags.DefaultIsActive = True
Set OPCEvents = New OPCEvents
Set OPCEvents.Server = oServer
Set OPCEvents.LiveGroups = TagGroups
Set OPCEvents.LiveGroup = TagGroup
AddTags
Dim tst As Long, stst As String
stst = oServer.GetErrorString(tst)
Call Ausw
Exit Sub
Connect_Error:
[Status] = Err.Description
stst = stst
End Sub
Private Sub DisconnectOPC()
Set OPCEvents = Nothing
Set Tags = Nothing
Set TagGroup = Nothing
Set TagGroups = Nothing
Set oServer = Nothing
[Status] = ""
' mzi anzeigen löschen
Dim j%
For j = 2 To 20 'iAnzTags + 1
Cells(j, 9) = "Disconnected"
Cells(j, 10) = ""
Cells(j, 11) = ""
Cells(j, 12) = ""
Next j
'
End Sub
Private Sub AddTags()
Dim NoofRequests%, Tag() As String, Tagno() As Long, k%, j%, c% 'mzi % = as int
c = [tagname].Column
k = [Validity].Column
Range(Cells(2, k), Cells(10000, [Quality].Column)).ClearContents
NoofRequests = Application.CountA(Columns([tagname].Column)) - 1
If NoofRequests = 0 Then Exit Sub
ReDim Tag(NoofRequests), Tagno(NoofRequests)
For j = 2 To NoofRequests + 1
Tag(j - 1) = Cells(j, c)
Next
Tags.Validate NoofRequests, Tag(), TagErrors()
NoofTags = 0
For j = 2 To NoofRequests + 1
If TagErrors(j - 1) = 0 Then
NoofTags = NoofTags + 1
Cells(j, k) = "VALID"
Tag(NoofTags) = Cells(j, c)
Tagno(NoofTags) = j
Else
Cells(j, k) = "INVALID"
End If
Next j
iAnzTags = NoofTags
If NoofTags > 0 Then Tags.AddItems NoofTags, Tag(), Tagno(), TagServerHdl(), TagErrors()
End Sub
__________________________________________________________________________________
Kann mir hier jemand sagen wie das gemacht wird? Es funktioniert aber wo?
Vielen Dank M.Zimmermann.