我正在尝试从表tblDetails
列Names
中获取唯一值,并将其粘贴到表tblNames
列名称中。
但是在以下代码的最后一行:
tbl2.Resize(d.Count) = Application.Transpose(d.keys)
...我收到此错误:
编译错误:
不允许分配常量
我不知道是什么导致了此错误。
任何帮助将不胜感激。
Sub Get_Unique_Values()
Dim dict As Object, arr, j, arrCustomers
Set dict = CreateObject("Scripting.Dictionary")
Dim tbl1 As ListObject, tbl2 As ListObject
Dim d As Object, i As Long, c As Variant
Set tbl1 = Worksheets("Sheet1").ListObjects("tblTest")
Set tbl2 = Worksheets("Sheet1").ListObjects("tblTest2")
If Not tbl2.DataBodyRange Is Nothing Then 'Clean tblTest2
tbl2.AutoFilter.ShowAllData
tbl2.DataBodyRange.Delete
End If
Set d = CreateObject("Scripting.Dictionary")
c = tbl1.ListColumns(1).DataBodyRange 'Loop through Table
For i = 1 To UBound(c, 1)
d(c(i, 1)) = 1
Next i
tbl2.Resize(d.Count) = Application.Transpose(d.keys) 'Export result to table
End Sub
答案 0 :(得分:0)
@JohnyL @ashleedawg @Harassed爸爸经过多次尝试,我设法得到了我想要的东西,谢谢大家的回应和指导。
Sub Get_Unique_Values()
Dim i As Long
Dim tbl1 As ListObject, tbl2 As ListObject
Dim d As Object
Dim c As Variant
Dim CountD As Long, LastRow As Long
Dim rng As Range
Set tbl1 = Worksheets("Sheet1").ListObjects("tblNames")
Set tbl2 = Worksheets("Sheet1").ListObjects("tblTest2")
If Not tbl2.DataBodyRange Is Nothing Then 'Clean tblTest2
tbl2.AutoFilter.ShowAllData
tbl2.DataBodyRange.Delete
End If
Set d = CreateObject("Scripting.Dictionary")
c = tbl1.ListColumns(1).DataBodyRange 'Loop through Table
For i = 1 To UBound(c, 1)
d(c(i, 1)) = 1
Next i
CountD = d.Count
LastRow = Sheet1.ListObjects("tblTest2").Range.Rows.Count
Set rng = Range("tblTest2[#All]").Resize(tbl2.Range.Rows.Count + CountD - 1, tbl2.Range.Columns.Count)
tbl2.Resize rng
Range("tblTest2[Column1]") = Application.Transpose(d.keys) 'Export result to table
End Sub