1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| Option Explicit
Sub Hajoorita()
Dim rng As Range
Dim oTable() As String
Dim bool As Boolean
Dim i As Integer, j As Integer
With Worksheets("F1")
Set rng = .Range("A2")
ReDim oTable(1 To 2, 1 To 1)
oTable(1, 1) = rng
oTable(2, 1) = rng.Offset(0, 1)
For i = 1 To .Columns(1).Find("*", , , , , xlPrevious).Row - 2
bool = True
For j = LBound(oTable, 2) To UBound(oTable, 2)
If oTable(1, j) = rng.Offset(i, 0) Then
oTable(2, j) = oTable(2, j) + rng.Offset(i, 1)
bool = False
Exit For
End If
Next j
If bool Then
ReDim Preserve oTable(1 To 2, 1 To UBound(oTable, 2) + 1)
oTable(1, UBound(oTable, 2)) = rng.Offset(i, 0)
oTable(2, UBound(oTable, 2)) = rng.Offset(i, 1)
End If
Next i
End With
With Worksheets("F2")
Set rng = .Range("A1")
For i = 1 To .Columns(1).Find("*", , , , , xlPrevious).Row - 1
bool = True
For j = LBound(oTable, 2) To UBound(oTable, 2)
If oTable(1, j) = rng.Offset(i, 0) Then
oTable(2, j) = oTable(2, j) - rng.Offset(i, 1)
bool = False
Exit For
End If
Next j
If bool Then
ReDim Preserve oTable(1 To 2, 1 To UBound(oTable, 2) + 1)
oTable(1, UBound(oTable, 2)) = rng.Offset(i, 0)
oTable(2, UBound(oTable, 2)) = -rng.Offset(i, 1)
End If
Next i
End With
With Worksheets("F3")
.Range("A2:B" & .Columns(2).Find("*", , , , , xlPrevious).Row).Clear
Set rng = .Range("A1")
For j = LBound(oTable, 2) To UBound(oTable, 2)
rng.Offset(j, 0) = oTable(1, j)
rng.Offset(j, 1) = oTable(2, j)
Next j
End With
End Sub |