Get all combinations - vba

I would like to generate all possible vectors where minimum and maximum of each element is known and some set of elements can have only the same value.
For example I have an input like this:
rid Set MaxId
1 a 1
2 b 2
3 c 2
4 c 2
5 c 2
Set identifies elements which all should always have the same value, MaxId identifies maximum integer atribute can have, minimum is always 1. From this data, we can create the following 4 combinations (denoted c1 - c4):
rid Set c1 c2 c3 c4
1 a 1 1 1 1
2 b 1 1 2 2
3 c 1 2 1 2
4 c 1 2 1 2
5 c 1 2 1 2
How can I do this using VBA? In my real data I have 100 rows with 5 different sets, resulting in total 80 variables where max Id is ranging between 1 and 5.
The example above is complete, there is no additional input to be provided. Let's consider different example:
rid Set MaxId
1 a 2
2 b 1
3 c 3
4 c 3
5 c 3
This would result in 6 possible combinations (2 x 1 x 3). There is only one 3 as this number is part of what I call "a set", identified by same letter c. The possible combinations are:
rid Set c1 c2 c3 c4 c5 c6
1 a 1 2 1 1 2 2
2 b 1 1 1 1 1 1
3 c 1 1 2 3 2 3
4 c 1 1 2 3 2 3
5 c 1 1 2 3 2 3

If I understand it right, then I would call your "sets" dimensions and your combinations possible addresses in those dimensions. For example in two dimensions x and y where x is in length 2 and y is in length 3 there are 6 possible points(x,y) if x and y elements of N. In three dimensions x, y and z where x is in length 2, y is in length 3 and z is in length 2 there are 12 possible points(x,y,z) if x, y and z elements of N.
For going through all addresses in dimensions normally nested loops are used. So I would do this here also.
Sub Dimensions()
With ThisWorkbook.Worksheets(1)
'create a dictionary for up to 5 different dimensions named "a" to "e"
'and their max length values
'using dictionary because mapping key (dimension name) to value (max length value)
Set dDimensions = CreateObject("Scripting.Dictionary")
dDimensions.Add "a", 9999 '9999 is the stop value which shows that this Dimension is not used
dDimensions.Add "b", 9999
dDimensions.Add "c", 9999
dDimensions.Add "d", 9999
dDimensions.Add "e", 9999
'get the dimension definitions from A2:B[n]
r = 2
Do While .Cells(r, 1) <> ""
sDimension = .Cells(r, 1).Value
lMax = .Cells(r, 2).Value
If lMax > 0 And dDimensions.exists(sDimension) Then
'if inconsistent definitions for length of dimensions exists,
'for example "a" with max length 3 and "a" with max length 2,
'then take the lowest max length definition, in example "a" with 2
If dDimensions.Item(sDimension) > lMax Then dDimensions.Item(sDimension) = lMax
End If
r = r + 1
Loop
'calculate the count of possible combinations
lCount = 1
For Each sDimension In dDimensions
lMax = dDimensions.Item(sDimension)
If lMax < 9999 Then lCount = lCount * lMax
Next
'create a dictionary for the results
'up to 5 different Dimensions named "a" to "e"
'and their possible values in lCount possible combinations
Set dResults = CreateObject("Scripting.Dictionary")
Dim aPointAddresses() As Long
ReDim aPointAddresses(lCount - 1)
dResults.Add "a", aPointAddresses
dResults.Add "b", aPointAddresses
dResults.Add "c", aPointAddresses
dResults.Add "d", aPointAddresses
dResults.Add "e", aPointAddresses
'go through all possible addresses and fill the dResults
lCount = 0
For a = 1 To dDimensions.Item("a")
For b = 1 To dDimensions.Item("b")
For c = 1 To dDimensions.Item("c")
For d = 1 To dDimensions.Item("d")
For e = 1 To dDimensions.Item("e")
If dDimensions.Item("a") < 9999 Then
arr = dResults.Item("a")
arr(lCount) = a
dResults.Item("a") = arr
End If
If dDimensions.Item("b") < 9999 Then
arr = dResults.Item("b")
arr(lCount) = b
dResults.Item("b") = arr
End If
If dDimensions.Item("c") < 9999 Then
arr = dResults.Item("c")
arr(lCount) = c
dResults.Item("c") = arr
End If
If dDimensions.Item("d") < 9999 Then
arr = dResults.Item("d")
arr(lCount) = d
dResults.Item("d") = arr
End If
If dDimensions.Item("e") < 9999 Then
arr = dResults.Item("e")
arr(lCount) = e
dResults.Item("e") = arr
End If
lCount = lCount + 1
If dDimensions.Item("e") = 9999 Then Exit For
Next
If dDimensions.Item("d") = 9999 Then Exit For
Next
If dDimensions.Item("c") = 9999 Then Exit For
Next
If dDimensions.Item("b") = 9999 Then Exit For
Next
If dDimensions.Item("a") = 9999 Then Exit For
Next
'now dResults contains an array of possible point addresses for each used dimension
'key:="dimension", item:={p1Addr, p2Addr, p3Addr, ..., pNAddr}
'clear the result range
.Range("D:XFD").Clear
'print out the results in columns D:XFD
.Range("D1").Value = "p1"
.Range("D1").AutoFill Destination:=.Range("D1:XFD1")
r = 2
Do While .Cells(r, 1) <> ""
sDimension = .Cells(r, 1).Value
arr = dResults.Item(sDimension)
.Range(.Cells(r, 4), .Cells(r, 4 + UBound(arr))).Value = arr
r = r + 1
Loop
End With
End Sub

Related

Generate combinations of 0s and 1s

I have two rows of cells: A1, B1, C1 and A2, B2, C2. In each of these 2 rows there can only be a single 1, with the two other cells set as 0. Here is one example occurrence:
A B C
+---+---+---+
1 | 1 | 0 | 0 |
+---+---+---+
2 | 0 | 1 | 0 |
+---+---+---+
I'm trying to iterate over all 9 possible combinations: 3 possibilities for the first row times 3 possibilities for the second. How would I go about doing this?
Here is one way:
Function delta(i As Long, n As Long) As Variant
'returns an array of length n
'consisting of all 0 with one 1
'at index i
Dim A As Variant
ReDim A(1 To n) As Long
A(i) = 1
delta = A
End Function
Sub IterateOver()
Dim i As Long, j As Long
For i = 1 To 3
Range("A1:C1").Value = delta(i, 3)
For j = 1 To 3
Range("A2:C2").Value = delta(j, 3)
'stub for real code:
MsgBox "Continue?", vbQuestion, "Test"
Next j
Next i
End Sub
This will generate all 8 combinations of 0 and 1 for 3 columns
Dim i As Integer
For i = 1 To 2^3
Range("A" & i) = (i-1) % 2
Range("B" & i) = Int((Range("A" & i))/2) % 2
Range("C" & i) = Int((Range("B" & i))/2) % 2
Next i
Try to extend it yourself

Excel VBA Code - Combinations with restrictions

I have to produce a list of combinations which are indicators to fields as I am trying to produce some conditions to interrogate data.I have some code for the combinations from here.
In the example of Power/Torque/Cylinders with a combination of 1,2,4:
I need to work out the combinations of those 3 fields, so the output would be:
So essentially all combinations but not from the same 'bucket' if that makes sense?
Edit:
The number of combinations (ie 3 in the example) will change as per the link I provided. The combinations from the link determine which field to look at or use. Eg combination 123 would be the first 3 fields in the image. Combination 1,2 would be tge first 2 and 1,3 would be first and last. I have the code for that.
Now we have the combination buckets, need to work through the combinations across thise fields.
Also I am looking for guidance on how to approach the algo and not necessarily someone to do it for me 😊
As another example, if 1,2,3 were the column combination, the expected output would be:
20-0.5-200
20-0.5-300
20-0.5-400
etc
Nested Loop:
Sub allCombo(set1 As Range, set2 As Range, set3 As Range)
Dim c1, c2, c3, n
For Each c1 In set1
For Each c2 In set2
For Each c3 In set3
n = n + 1
Debug.Print "#" & n, c1, c2, c3
Next c3
Next c2
Next c1
End Sub
Example Usage:
Sub test()
allCombo [I2:I4], [J2:J3], [L2:L3]
End Sub
Result:
#1 20 0.5 4
#2 20 0.5 8
#3 20 0.8 4
#4 20 0.8 8
#5 30 0.5 4
#6 30 0.5 8
#7 30 0.8 4
#8 30 0.8 8
#9 40 0.5 4
#10 40 0.5 8
#11 40 0.8 4
#12 40 0.8 8
Here is an option that is completely dynamic:
Option Explicit
Sub MakeCombos()
Dim myCols As Variant, i As Long, j As Long, myCombos() As Variant
Dim temp() As Variant, LastRow As Long, lngCol As Long, myLens() As Long
Dim index() As Long, totalCombs As Long, count As Long
'' Prompt user for columns N.B. there is no
'' data validation, so enter with caution
myCols = Split(InputBox("Enter the columns as a comma separated list: ", "Column Combos 3000"), ",")
ReDim myCombos(0 To UBound(myCols))
ReDim index(0 To UBound(myCols))
ReDim myLens(0 To UBound(myCols))
totalCombs = 1
'' This loop is simply populating myCombos
'' with the chosen columns. We are also populating
'' myLens with the maximum length of each column
For i = 0 To UBound(myCols)
lngCol = CLng(myCols(i))
With ActiveSheet
LastRow = .Cells(.Rows.count, lngCol).End(xlUp).Row
End With
ReDim temp(0 To LastRow - 2)
For j = 2 To LastRow
temp(j - 2) = Cells(j, lngCol)
Next j
myCombos(i) = temp
myLens(i) = LastRow - 2
'' Get the total number of combinations
totalCombs = totalCombs * (LastRow - 1)
Next i
'' This is where the magic happens. Note, we
'' don't have nested for loops. Rather, we are keeping
'' up with the correct index with the appropriately
'' named array "index". When one of the indices exceeds
'' the maximum length, we reset that index and increment
'' the next index until we have enumerated every combo
While (count < totalCombs)
For j = 0 To UBound(myCols)
Cells(count + 20, j + 1) = myCombos(j)(index(j))
Next j
j = UBound(index)
index(j) = index(j) + 1
Do While index(j) > myLens(j)
index(j) = 0
j = j - 1
If j < 0 Then Exit Do
index(j) = index(j) + 1
Loop
count = count + 1
Wend
End Sub
Here is the example input:
And here is the top of the output for entering 1,2,4 at the prompt:
And here is the top of the output for entering 2,3 at the prompt:
Here is a sub that first determines the number of items in columns I, J, L and adjust the loops accordingly:
Sub SteveP()
Dim N1 As Long, N2 As Long, N3 As Long, K As Long
Dim m1 As Long, m2 As Long, m3 As Long
Dim a As Variant, b As Variant, c As Variant
N1 = Cells(Rows.Count, "I").End(xlUp).Row
N2 = Cells(Rows.Count, "J").End(xlUp).Row
N3 = Cells(Rows.Count, "L").End(xlUp).Row
K = 1
For m1 = 2 To N1
a = Cells(m1, "I")
For m2 = 2 To N2
b = Cells(m2, "J")
For m3 = 2 To N3
c = Cells(m3, "L")
Cells(K, "M") = a
Cells(K, "N") = b
Cells(K, "O") = c
K = K + 1
Next m3
Next m2
Next m1
End Sub

Counting and summing unique data in several columns then matching it with another unique data

This is an addition question of my previous post. I was not able to find a way to figure it out for hours nor also to find an idea from online search.
Suppose I have the following data (the actual data can be thousands or millions) in Excel sheet (Table 1):
Name Entry No. ID Expense 1 Expense 2
A 1 A1 14 5
B 2 B4 12 7
B 2 B5 20 8
C 3 C0 19 7
D 4 - 0 0
A 1 A1 11 6
A 1 A2 20 5
E 5 - 0 0
F 6 - 0 0
C 3 C0 15 5
B 2 B5 20 4
B 2 B5 16 3
B 2 B5 12 7
B 2 B6 18 8
A 1 A1 10 1
A 1 A1 14 7
A 1 A2 10 2
B 2 B3 13 7
B 2 B3 14 1
B 2 B3 11 4
The character (-) in column No. ID above can be also a number 0 or a blank cell.
I want to format the above data as follow (Table 2)
Name Entry No. ID Number of ID Sum of Expense 1 Sum of Expense 2
A 1 A1 2 49 19
A 1 A2 2 30 7
B 2 B3 4 38 12
B 2 B4 4 12 7
B 2 B5 4 68 22
B 2 B6 4 18 8
C 3 C0 1 34 12
D 4 - 0 0 0
E 5 - 0 0 0
F 6 - 0 0 0
Column Number of ID means A has 2 IDs (A1 and A2), B has 4 IDs (B1, B2, B3, and B4), C has 1 ID (C0), and D, E, and F have no ID. Column Sum of Expense 1 and 2 are the sum of all expense for each no. ID.
The best I can get by using Pivot Table is like this
How does one perform a task like Table 2 in MS Excel? If possible a VBA script of it.
This code after modifications works when running it from the same Workbook (doesn't matter which Worksheet).
Added an array to dynamic add number Sum of Expense types.
This code covers the logic needed to convert you table's data just like you wanted.
Sub OrganizeTable()
Dim TableArray() As Variant
Dim i, j, k, i_tmp, LastRow As Long
Dim Sum_Count As Integer
Dim SheetData, SheetResult As Excel.Worksheet
Dim StringTemp As String
Dim LongMin, LongMax As Long
Dim SumExpense() As Long
Dim Number_of_ID As Long
Dim Number_of_Expense_Type As Integer
' Number_of_Expense_Type = number of expense type you have in your
Number_of_Expense_Type = InputBox("Enter number of expense type ", "Expense Type counter")
Set SheetData = ActiveWorkbook.Worksheets("Sheet1")
LastRow = SheetData.Cells(SheetData.Rows.Count, "A").End(xlUp).row
Set SheetResult = ActiveWorkbook.Worksheets("Sheet2")
Erase TableArray
ReDim TableArray(1 To LastRow - 1, 1 To 3 + Number_of_Expense_Type) ' create array with exact number of Project names
ReDim SumExpense(1 To Number_of_Expense_Type)
i = 2
' insert all table's data into multi-dimensional array (easier and faster to manipulate later)
While SheetData.Cells(i, 1) <> ""
For j = 1 To 3 + Number_of_Expense_Type
TableArray(i - 1, j) = SheetData.Cells(i, j)
Next
i = i + 1
Wend
LongMin = LBound(TableArray())
LongMax = UBound(TableArray())
' this loop is for sorting the array according to Name, and then No. ID
For i = LongMin To LongMax - 1
For j = i + 1 To LongMax
' 1st rule: check for Name Value in Column A
If TableArray(i, 1) > TableArray(j, 1) Then
For k = 1 To 3 + Number_of_Expense_Type
StringTemp = TableArray(i, k)
TableArray(i, k) = TableArray(j, k)
TableArray(j, k) = StringTemp
Next
End If
' 2nd rule: check for No. ID in Column c
If TableArray(i, 1) = TableArray(j, 1) And TableArray(i, 3) > TableArray(j, 3) Then
For k = 1 To 3 + Number_of_Expense_Type
StringTemp = TableArray(i, k)
TableArray(i, k) = TableArray(j, k)
TableArray(j, k) = StringTemp
Next
End If
Next
Next
i = 1
j = 2 ' this is the Row number where the sorted table will start
k = 1 ' this is the Column number where the sorted table will start
While i <= LongMax
SheetResult.Cells(j, k) = TableArray(i, 1)
SheetResult.Cells(j, k + 1) = TableArray(i, 2)
SheetResult.Cells(j, k + 2) = TableArray(i, 3)
For Sum_Count = 1 To Number_of_Expense_Type
SumExpense(Sum_Count) = TableArray(i, 4 + Sum_Count - 1)
Next
' this IF and WHILE loop are for accumulating the Sum Expense 1 and Sum Expense 2 for the same ID type
If i + 1 <= LongMax Then
While TableArray(i, 3) = TableArray(i + 1, 3) And TableArray(i, 1) = TableArray(i + 1, 1)
For Sum_Count = 1 To Number_of_Expense_Type
SumExpense(Sum_Count) = SumExpense(Sum_Count) + Val(TableArray(i + 1, 4 + Sum_Count - 1))
Next
i = i + 1
Wend
End If
' this IF and WHILE loop are for counting how many Num of ID you have per Name
Number_of_ID = 0
If TableArray(i, 3) <> "-" Then
Number_of_ID = 1
For i_tmp = 1 To LongMax - 1
While Cells(j, k) = TableArray(i_tmp + 1, 1) And TableArray(i_tmp, 1) = TableArray(i_tmp + 1, 1) And TableArray(i_tmp, 3) <> TableArray(i_tmp + 1, 3)
Number_of_ID = Number_of_ID + 1
i_tmp = i_tmp + 1
Wend
Next
Else
Number_of_ID = 0
End If
SheetResult.Cells(j, k + 3) = Number_of_ID
For Sum_Count = 1 To Number_of_Expense_Type
SheetResult.Cells(j, k + 4 + Sum_Count - 1) = SumExpense(Sum_Count)
SumExpense(Sum_Count) = 0
Next
Number_of_ID = 0
j = j + 1
i = i + 1
Wend
' writing down the headers for you table
SheetResult.Cells(1, k) = "Name"
SheetResult.Cells(1, k + 1) = "Entry"
SheetResult.Cells(1, k + 2) = "No. ID"
SheetResult.Cells(1, k + 3) = "Number of ID"
For Sum_Count = 1 To Number_of_Expense_Type
SheetResult.Cells(1, k + 4 + Sum_Count - 1) = "Sum of Expense " & Sum_Count
Next
Set SheetData = Nothing
Set SheetResult = Nothing
End Sub
Below code might help:
Assumptions:
1. Your data is in ActiveSheet
2. Result will be displayed in the Sheet2
Sub Demo()
Dim dict1 As Object, dict2 As Object
Dim c1 As Variant, c2 As Variant
Dim i As Long, lastRow As Long, targetRow As Long, count As Long
Dim targetWS As Worksheet
Set targetWS = ThisWorkbook.Sheets("Sheet2")
Set dict1 = CreateObject("Scripting.Dictionary")
Set dict2 = CreateObject("Scripting.Dictionary")
'get last row with data
lastRow = Cells(Rows.count, "A").End(xlUp).Row
'assign unique values in Column A (Name) to dict1
c1 = Range("A2:A" & lastRow)
For i = 1 To UBound(c1, 1)
dict1(c1(i, 1)) = 1
Next i
'assign unique values in Column C (No. Id) to dict2
c2 = Range("C2:C" & lastRow)
For i = 1 To UBound(c2, 1)
dict2(c2(i, 1)) = 1
Next i
'write headers in Sheet2
targetWS.Cells(1, 1) = "Name"
targetWS.Cells(1, 2) = "Entry"
targetWS.Cells(1, 3) = "No. Id"
targetWS.Cells(1, 4) = "Number of ID"
targetWS.Cells(1, 5) = "Sum of Expense 1"
targetWS.Cells(1, 6) = "Sum of Expense 2"
'fill data in table
targetRow = 2 '-->targetRow will keep the counter for new row in Sheeet2
'loop through unique values of Name through dict1
For Each k1 In dict1.Keys
count = 0
'loop through unique No. ID through dict2 to match values in dict1 and dict2
For Each k2 In dict2.Keys
If k2 Like k1 & "*" Then '-->match values of dict1 and dict2
count = count + 1
'fill data in table if match found
targetWS.Cells(targetRow, 1) = k1
targetWS.Cells(targetRow, 3) = k2
targetWS.Cells(targetRow, 4) = dict2(k2)
targetWS.Cells(targetRow, 5) = Application.WorksheetFunction.SumIf(Range("C2:C" & lastRow), k2, Range("D2:D" & lastRow))
targetWS.Cells(targetRow, 6) = Application.WorksheetFunction.SumIf(Range("C2:C" & lastRow), k2, Range("E2:E" & lastRow))
targetRow = targetRow + 1
End If
Next k2
'fill data if no match found
If count = 0 Then
targetWS.Cells(targetRow, 1) = k1
targetWS.Cells(targetRow, 3) = "-"
targetWS.Cells(targetRow, 5) = 0
targetWS.Cells(targetRow, 6) = 0
targetRow = targetRow + 1
End If
Next k1
'get values for Entry and Number of ID
For i = 2 To targetWS.Cells(Rows.count, "A").End(xlUp).Row
targetWS.Cells(i, 2) = Range("A:A").Find(What:=targetWS.Cells(i, 1), LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=True).Offset(0, 1).Value
targetWS.Cells(i, 4) = Application.WorksheetFunction.CountIf(targetWS.Range("A1:A" & lastRow), targetWS.Cells(i, 1))
Next i
End Sub
Note: Above code will not display data in ascending order like A1-A2-B3-B4-B5-B6-C0 instead data will be displayed in the order of appearance like A1-A2-B4-B5-B6-B3-C0
See image for reference:
You can have VBA to copy the pivot table data and paste it as normal table.

find values between range, fill in next cells

I'm looking for a way for copying down all data found between a range and paste it in a next column.
A= text data
B= Random numbers but always starting from 1
C= some data
D= My needed solution
Example:
A B C D
018404.00 1 20 20
018404x0 2 0 20
018404f1 2 0 20
018404v1 3 0 20
11000-0532 4 0 20
1004-1101 5 0 20
0720-0125 3 0 20
0810-0001 3 0 20
0710-0040 3 0 20
052269.00 1 0 80
052269v6 2 0 80
11001-0000 3 0 80
1001-1110 4 0 80
0720-0500 2 0 80
0810-0001 2 80 80
0720-0002 2 0 80
052275.00 1 0 160
052275v2 2 160 160
When the value in column B is 1 then find value in column C (in Range B:B from 1 to 1) copying it to D
I have tried it with a formula but this limits the depth. If value on Column C isto far from the 1 row it doesn't work.
=IF(AND(B2=1;C2=0);IF(B3=1;0;IF(C3=0;IF(C4=0;C5;C4);C3));IF(C2>0;C2;I1))
So I think I need a vba solution.
i think you need something like this.
This code search max value between your 1 and 1 value
Sub FindValBetweenOne()
Dim LastRow As Long
Dim FindVal As Long
LastRow = Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
For I = 1 To LastRow
If Cells(I, "B").Value = 1 Then 'find next "1"
FindVal = Cells(I, "C").Value
J = I + 1
Do While (J <= LastRow And Cells(J, "B").Value <> 1)
If Cells(J, "C").Value > FindVal Then
FindVal = Cells(J, "C")
End If
J = J + 1
Loop
End If
Cells(I, "D").Value = FindVal
Next I
End Sub

Collapsing duplicate row entries and count them?

I think what I'm trying to do is pretty basic, but I'm brand new to VBA so I'm getting stuck and the answers I've found are close, but not quite right.
I have a list of row entries, like this:
1 4 32 2 4
2 6 33 1 3
1 4 32 2 4
4 2 30 1 5
Notice that rows 1 and 3 are duplicates. I'd like to only have a single instance of each unique row but I don't want to just delete the duplicates, I want to report how many of each type there are. Each row represents an inventory item, so deleting duplicate entries without indicating total quantity would be very bad!
So, my desired output would look something like this, where the additional 6th column counts the total number of instances of each item:
1 4 32 2 4 2
2 6 33 1 3 1
4 2 30 1 5 1
My data sets are larger than just these 5 columns, they're closer to 10 or so, so I'd like to put that last column at the end, rather than to hardcode it to the 6th column (i.e., column "F")
Update:
I found some code that worked with minor tweaking, and it worked this morning, but after messing around with some other macros, when I came back to this one it was telling me that I have a "compile error, wrong number of arguments or invalid property assignment" and it seemed to be unhappy with the "range". Why would working code stop working?
Sub mcrCombineAndScrubDups2()
For Each a In range("A1", Cells(Rows.Count, "A").End(xlUp))
For r = 1 To Cells(Rows.Count, "A").End(xlUp).Row - a.Row
If a = a.Offset(r, 0) And a.Offset(0, 1) = a.Offset(r, 1) And a.Offset(0, 2) = a.Offset(r, 2) Then
a.Offset(0, 4) = a.Offset(0, 4) + a.Offset(r, 4)
a.Offset(r, 0).EntireRow.Delete
r = r - 1
End If
Next r
Next a
End Sub
Assuming that your data starts from A1 on a worksheet named ws1, the following code removes the duplicated rows. Not by shifting the whole table but deleting the entire row.
Sub deletedupe()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim row1 As String
Dim row2 As String
i = 1
j = 1
k = 1
Do While Sheets("ws1").Cells(i, 1).Value <> ""
row1 = ""
j = 1
Do While Sheets("ws1").Cells(i, j).Value <> ""
row1 = row1 & Sheets("ws1").Cells(i, j).Value & " "
j = j + 1
Loop
k = i + 1
Do While Sheets("ws1").Cells(k, 1).Value <> ""
row2 = ""
j = 1
Do While Sheets("ws1").Cells(k, j).Value <> ""
row2 = row2 & Sheets("ws1").Cells(k, j).Value & " "
j = j + 1
Loop
If row1 = row2 Then
Sheets("ws1").Rows(k).EntireRow.Delete
Else
k = k + 1
End If
Loop
i = i + 1
Loop
End Sub