Amend Code to work on Older Version e.g Office 2010

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have the following code which works on Office 2021. However, one of my users uses Office 2010 and gets a run time error

Kindly amend my Code


Code:
Sub Formula()
Dim LR As Long
With Sheets("Macro")
LR = .Cells(.Rows.Count, "L").End(xlUp).Row

.Range("E12:E" & LR + 14).ClearContents
.Range("e12").Formula2R1C1 = "=UNIQUE(R[-11]C[9]:R[2]C[9])"
.Range("e12:E" & LR + 14).Copy
.Range("E12:E" & LR + 14).PasteSpecial xlValues

End With
Application.CutCopyMode = False
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
There is no UNIQUE function in 2010.
 
Upvote 0
I am aware that there is no unique code. Is there any way to amend code using another code that will give the same result in an older version
 
Upvote 0
Add this function:

VBA Code:
Public Function UniqueListFromRange(rgInput As Range) As Variant
   Dim d As Object
   Dim rgArea As Excel.Range
   Dim dataSet
   Dim x As Long
   Dim y As Long

   Set d = CreateObject("Scripting.Dictionary")

   For Each rgArea In rgInput.Areas
      dataSet = rgArea.Value
      If IsArray(dataSet) Then
         For x = 1 To UBound(dataSet)
            For y = 1 To UBound(dataSet, 2)
               If Len(dataSet(x, y)) <> 0 Then d(dataSet(x, y)) = Empty
            Next y
         Next x
      Else
         d(dataSet) = Empty
      End If
   Next rgArea
   UniqueListFromRange = d.Keys
End Function

then change your code to:

VBA Code:
Sub Formula()
Dim LR As Long
With Sheets("Macro")
LR = .Cells(.Rows.Count, "L").End(xlUp).Row

.Range("E12:E" & LR + 14).ClearContents
dim v
v = uniquelistfromrange(.range("N1:N14"))
.Range("e12").resize(ubound(v) + 1).value = application.transpose(v)

End With
Application.CutCopyMode = False
End Sub

Formula is not a great name for a routine though.
 
Upvote 0
I am using 2010 and this code works:
VBA Code:
Sub test()
Dim LR As Long
Dim Dic As Object
Dim outarr()
With Sheets("Macro")
    LR = .Cells(.Rows.Count, "L").End(xlUp).Row + 14
    ary = .Range(.Cells(1, 12), .Cells(LR, 12))
    
    .Range("E12:E" & LR + 14).ClearContents
    
   Set Dic = CreateObject("Scripting.dictionary")
   For i = 1 To UBound(ary)
      Dic(ary(i, 1)) = ary(i, 1)
   Next i
ReDim outarr(1 To Dic.Count, 1 To 1)
 For i = 0 To Dic.Count - 1
  outarr(i + 1, 1) = Dic.Keys()(i)
 Next i
.Range(.Cells(12, 4), .Cells(11 + Dic.Count, 4)) = outarr
'.Range("e12").Formula = "sum(R[-11]C[9]:R[2]C[9])"
'.Range("e12:E" & LR + 14).Copy
'.Range("E12:E" & LR + 14).PasteSpecial xlValues

End With


End Sub
 
Upvote 0
Many thanks for your help. Will test your code tomorrow and let you know
 
Upvote 0
I am using 2010 and this code works:
VBA Code:
Sub test()
Dim LR As Long
Dim Dic As Object
Dim outarr()
With Sheets("Macro")
    LR = .Cells(.Rows.Count, "L").End(xlUp).Row + 14
    ary = .Range(.Cells(1, 12), .Cells(LR, 12))
   
    .Range("E12:E" & LR + 14).ClearContents
   
   Set Dic = CreateObject("Scripting.dictionary")
   For i = 1 To UBound(ary)
      Dic(ary(i, 1)) = ary(i, 1)
   Next i
ReDim outarr(1 To Dic.Count, 1 To 1)
 For i = 0 To Dic.Count - 1
  outarr(i + 1, 1) = Dic.Keys()(i)
 Next i
.Range(.Cells(12, 4), .Cells(11 + Dic.Count, 4)) = outarr
'.Range("e12").Formula = "sum(R[-11]C[9]:R[2]C[9])"
'.Range("e12:E" & LR + 14).Copy
'.Range("E12:E" & LR + 14).PasteSpecial xlValues

End With


End Sub
I am using 2010 and this code works:
VBA Code:
Sub test()
Dim LR As Long
Dim Dic As Object
Dim outarr()
With Sheets("Macro")
    LR = .Cells(.Rows.Count, "L").End(xlUp).Row + 14
    ary = .Range(.Cells(1, 12), .Cells(LR, 12))
   
    .Range("E12:E" & LR + 14).ClearContents
   
   Set Dic = CreateObject("Scripting.dictionary")
   For i = 1 To UBound(ary)
      Dic(ary(i, 1)) = ary(i, 1)
   Next i
ReDim outarr(1 To Dic.Count, 1 To 1)
 For i = 0 To Dic.Count - 1
  outarr(i + 1, 1) = Dic.Keys()(i)
 Next i
.Range(.Cells(12, 4), .Cells(11 + Dic.Count, 4)) = outarr
'.Range("e12").Formula = "sum(R[-11]C[9]:R[2]C[9])"
'.Range("e12:E" & LR + 14).Copy
'.Range("E12:E" & LR + 14).PasteSpecial xlValues

End With


End Sub
Hi Offthelip your code runs perfectly
I am using 2010 and this code works:
VBA Code:
Sub test()
Dim LR As Long
Dim Dic As Object
Dim outarr()
With Sheets("Macro")
    LR = .Cells(.Rows.Count, "L").End(xlUp).Row + 14
    ary = .Range(.Cells(1, 12), .Cells(LR, 12))
   
    .Range("E12:E" & LR + 14).ClearContents
   
   Set Dic = CreateObject("Scripting.dictionary")
   For i = 1 To UBound(ary)
      Dic(ary(i, 1)) = ary(i, 1)
   Next i
ReDim outarr(1 To Dic.Count, 1 To 1)
 For i = 0 To Dic.Count - 1
  outarr(i + 1, 1) = Dic.Keys()(i)
 Next i
.Range(.Cells(12, 4), .Cells(11 + Dic.Count, 4)) = outarr
'.Range("e12").Formula = "sum(R[-11]C[9]:R[2]C[9])"
'.Range("e12:E" & LR + 14).Copy
'.Range("E12:E" & LR + 14).PasteSpecial xlValues

End With


End Sub
Hi Offthelip your code runs perfectly
 
Upvote 0
Add this function:

VBA Code:
Public Function UniqueListFromRange(rgInput As Range) As Variant
   Dim d As Object
   Dim rgArea As Excel.Range
   Dim dataSet
   Dim x As Long
   Dim y As Long

   Set d = CreateObject("Scripting.Dictionary")

   For Each rgArea In rgInput.Areas
      dataSet = rgArea.Value
      If IsArray(dataSet) Then
         For x = 1 To UBound(dataSet)
            For y = 1 To UBound(dataSet, 2)
               If Len(dataSet(x, y)) <> 0 Then d(dataSet(x, y)) = Empty
            Next y
         Next x
      Else
         d(dataSet) = Empty
      End If
   Next rgArea
   UniqueListFromRange = d.Keys
End Function

then change your code to:

VBA Code:
Sub Formula()
Dim LR As Long
With Sheets("Macro")
LR = .Cells(.Rows.Count, "L").End(xlUp).Row

.Range("E12:E" & LR + 14).ClearContents
dim v
v = uniquelistfromrange(.range("N1:N14"))
.Range("e12").resize(ubound(v) + 1).value = application.transpose(v)

End With
Application.CutCopyMode = False
End Sub

Formula is not a great name for a routine though.
Hi RoryA

I get a run time message-Type Mismatch and code below is highlighted

Code:
 .Range("e12").Resize(UBound(v) + 1).Value = Application.Transpose(v)
 
Upvote 0

Forum statistics

Threads
1,215,537
Messages
6,125,389
Members
449,222
Latest member
taner zz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top