1

Тема: VBA: Операции реляционной алгебры

Здравствуйте, помогите пожалуйста, необходимо написать код, для VBA на основании таблиц A и B, должны быть сделаны операции, а именно пересечение, объединение, разность и произведение, как видите реализация только пересечение, и объединение(код представлен ниже), но слегка с ошибками, разность и произведение, просто нет возможности сделать?


Sub Start()
Dim A() As String
Dim B() As String
Dim ASB() As String
Dim AUB() As String
i% = 3

Do
If Cells(i%, 2).Value = "" Then
n% = i% - 3
Exit Do
End If
i% = i% + 1
Loop

If n% = 0 Then
MsgBox "?????? A ????"
Exit Sub
End If

i% = 3
Do
If Cells(i%, 4).Value = "" Then
m% = i% - 3
Exit Do
End If
i% = i% + 1
Loop

If m% = 0 Then
MsgBox "?????? B ????"
Exit Sub
End If

' ??????????????? ??????????? ????????
ReDim A(1 To n%, 1 To 2) As String
ReDim B(1 To m%, 1 To 2) As String
ReDim ASB(1 To n% + m%, 1 To 2) As String
ReDim AUB(1 To n% + m%, 1 To 2) As String

' ?????? ?????? ?? ????? ? ???????
For i% = 1 To n%
A(i%, 1) = Cells(i% + 2, 2)
A(i%, 2) = Cells(i% + 2, 3)
Next i%

For i% = 1 To m%
B(i%, 1) = Cells(i% + 2, 4)
B(i%, 2) = Cells(i% + 2, 5)
Next i%

'???????? ?????? ??????

Range("F3:I40").Select
Selection.ClearContents
Range("A1").Select

' ????? ???????????

Intersect A(), B(), ASB(), k1%


' ????? ??????????? ?????????
For i% = 1 To k1%
Cells(i% + 2, 6) = ASB(i%, 1)
Cells(i% + 2, 7) = ASB(i%, 2)
Next i%

'For i% = 1 To k2%
'Cells(i% + 2, 8) = AUB(i%, 1)
'Cells(i% + 2, 9) = AUB(i%, 2)
'Next i%


End Sub

'::: ??????????? A ? B -> AB

Sub Intersect(A() As String, B() As String, AB() As String, k As Integer)
k% = 0
For i% = 1 To UBound(A, 1)
aa = A(i%, 1)
a1 = A(i%, 2)
q% = 0
For j% = 1 To UBound(B, 1)
If B(j%, 1) = aa Then
q% = -1
Exit For
End If
Next j%
If (q% <> 0) Then
q% = 0
For j% = 1 To k%
If AB(j%, 1) = aa Then
q% = -1
Exit For
End If
Next j%
If (q% = 0) Then
k% = k% + 1
AB(k%, 1) = aa
AB(k%, 2) = a1
End If
End If
Next i%

Range("H3").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("H4").Select
Range("H3").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("I3").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("H4").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("I4").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("H5").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("I5").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("H6").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("I6").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("H7").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("I7").Select
ActiveCell.FormulaR1C1 = "=RC[-6]"
Range("H8").Select
ActiveCell.FormulaR1C1 = "=R[-5]C[-4]"
Range("I8").Select
ActiveCell.FormulaR1C1 = "=R[-5]C[-4]"
Range("H9").Select
ActiveCell.FormulaR1C1 = "=R[-5]C[-4]"
Range("I9").Select
ActiveCell.FormulaR1C1 = "=R[-5]C[-4]"
Range("H10").Select
ActiveCell.FormulaR1C1 = "=R[-5]C[-4]"
Range("I10").Select
ActiveCell.FormulaR1C1 = "=R[-5]C[-4]"
Range("H11").Select
ActiveCell.FormulaR1C1 = "="
Range("H10").Select
ActiveCell.FormulaR1C1 = "=R[-4]C[-4]"
Range("I10").Select
ActiveCell.FormulaR1C1 = "=R[-4]C[-4]"
Range("H11").Select
ActiveCell.FormulaR1C1 = "=R[-4]C[-4]"
Range("I11").Select
ActiveCell.FormulaR1C1 = "=R[-4]C[-4]"
Range("I12").Select

End Sub