Sub B_MakeUniqueNumbers()
Dim cols As Integer
Dim BaseFormula As String
Dim UniqueFormula As String
Application.ScreenUpdating = False 'this runs the code withought updating the screen (it runs faster this way)
' old formula for BaseFormula "DEC2HEX(SUMPRODUCT(H5:O5, 10^{7,6,5,4,3,2,1,0}))" '(Old formula used)
BaseFormula = "Dec2Hex(H5, 6) & " - " & Dec2Hex(I5, 6) & " - " & Dec2Hex(J5, 6) & " - " & Dec2Hex(K5, 6) & " - " & Dec2Hex(L5, 6) & " - " & Dec2Hex(M5, 6) & " - " & Dec2Hex(N5, 6) & " - " & Dec2Hex(O5, 6)"
UniqueFormula = BaseFormula
Dim LastRow As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row 'here we figure out how many rows are used in B column so we can "fill down" the formula
cols = Application.WorksheetFunction.RoundUp((ActiveSheet.UsedRange.Columns.Count - 7) / 8, 0)
'Above, we count the columns and subtract the first seven cols since they are the address; then round up to the nearest set of 8 cols
' For i = 1 To cols - 1 'this will loop to build the formula based on number of columns
' Dim temp As String
' temp = UniqueFormula
'UniqueFormula = temp & " & " & ""DEC2HEX(H5,6)&"-"&DEC2HEX(I5,6)&"-"&DEC2HEX(J5,6)&"-"&DEC2HEX(K5,6)&"-"&DEC2HEX(L5,6)&"-"&DEC2HEX(M5,6)&"-"&DEC2HEX(N5,6)&"-"&DEC2HEX(O5,6)" .Offset(, i * 8).Address(False, False)
' Next i
'old formula DEC2HEX(SUMPRODUCT(" & Range("H5:O5").Offset(, i * 8).Address(False, False) & ", 10^{7,6,5,4,3,2,1,0}))"
'Now the formula is created, and we'll insert it and "Fill down" to the bottom of the sheet
Range("a5").Offset(, cols * 8 + 8).Select 'Position coursor just outside actively used columns to create formula
Selection.Formula = "=" & UniqueFormula 'Insert formula
Range(Selection.Address, Cells(LastRow, cols * 8 + 9)).FormulaR1C1 = Range(Selection.Address).FormulaR1C1 'and "Fill Down" to the last used row
Application.ScreenUpdating = True 'turn back on screen updating
With ActiveSheet
.AutoFilterMode = False 'clear out any current filters that may be present
.Range("4:4").AutoFilter ' and apply our new filter across the 4th row
End With
End Sub
Bookmarks