Dies funktionierte für mich mit Microsoft Access VBA und Microsoft Excel VBA. Es ist kein sehr effizienter Code, aber es funktioniert. Ich konnte die resultierende Datei sowohl in Access als auch in Excel öffnen.
Set die sDBF*
und sOutDBF*
Variablen dies Ihre eigenen Pfade anzupassen.
Sub VBASolution()
Dim oRS
Dim sConn
Dim sDBFPath, sOutDBFPath
Dim sDBFName, sOutDBFName
Dim oDict
Dim curTID, curZone, sZones
Dim oConn
Dim oFS
Dim sTableName
sDBFPath = "C:\Path\To\DBFs\"
sDBFName = "Input.dbf"
sOutDBFPath = "C:\Path\To\DBFs\"
sOutDBFName = "RESULTS.dbf"
sConn = "Driver={Microsoft dBASE Driver (*.dbf)}; DriverID=277; Dbq="
Set oRS = CreateObject("ADODB.Recordset")
oRS.Open "SELECT DISTINCT tid, zone FROM " & sDBFName, sConn & sDBFPath
Set oDict = CreateObject("Scripting.Dictionary")
Do While Not oRS.EOF
curTID = oRS.Fields("tid").Value
curZone = oRS.Fields("zone").Value
If Not oDict.Exists(curTID) Then
oDict.Add curTID, CreateObject("Scripting.Dictionary")
End If
If Not oDict(curTID).Exists(curZone) Then
oDict(curTID).Add curZone, curZone
End If
oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing
Set oConn = CreateObject("ADODB.Connection")
oConn.Open sConn & sOutDBFPath
'Delete the resultant DBF file if it already exists.
Set oFS = CreateObject("Scripting.FileSystemObject")
With oFS
If .FileExists(sOutDBFPath & "\" & sOutDBFName) Then
.DeleteFile sOutDBFPath & "\" & sOutDBFName
End If
End With
sTableName = oFS.GetBaseName(sOutDBFName)
oConn.Execute "CREATE TABLE " & sTableName & " (tid int, zone varchar(80))"
Dim i, j
For Each i In oDict.Keys
curTID = i
sZones = ""
For Each j In oDict(i)
sZones = sZones & "," & j
Next
sZones = Mid(sZones, 2)
oConn.Execute "INSERT INTO " & sTableName & " (tid, zone) VALUES ('" & curTID & "','" & sZones & "')"
Next
oConn.Close
Set oConn = Nothing
Set oDict = Nothing
Set oFS = Nothing
End Sub
EDIT: Für was es wert ist, gilt dies auch für mich gearbeitet, indem sie in eine VBScript VBS-Datei (Text) in Windows XP und das Hinzufügen dieser Zeile zum Ende der Datei einfügen:
Call VBASolution()
Ich weiß nicht, ob Office installiert werden muss oder ob die entsprechenden dbf-Treiber mit Windows geliefert werden.
Dies ist mit 'Python' und 'Vba' markiert, aber alle den Code an swers sind in Python. Ich kann dafür leicht ein VBA-Beispiel bereitstellen. Können Sie klären, ob die Lösung in einer der beiden Sprachen oder falsch getaggt sein soll? – transistor1
@ Transistor1, gehen Sie voran und bieten Sie die VBA-Lösung. –