vba - 尝试从表中获取唯一值时出现“Assignment to Constant”错误

标签 vba excel compiler-errors unique

我正在尝试从表tblDetailsNames中获取唯一值,并将其粘贴到表tblNames列名称中。

但是,在下面的代码的最后一行:
tbl2.Resize(d.Count) = Application.Transpose(d.keys)
...我收到此错误:

Compile error:
Assignment to constant not permitted



我不知道是什么导致此错误。

任何帮助将不胜感激。
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

最佳答案

@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

关于vba - 尝试从表中获取唯一值时出现“Assignment to Constant”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52154821/

相关文章:

excel - 雅虎财经历史股价幂查询返回301响应

excel - 选择所有有数据的单元格

vba - 使用工作表变量获取工作表属性

c++ - 编译Boost::Python时出现问题

arrays - List<Int> 上不允许进行数组访问

vba - Word VBA .SaveAs2 弹出消息(在某​​些情况下)

excel - Charts.Add 方法使用 FormulaR1C1 导致意外结果

types - 类型错误和可判定性-编译器

vba - 使用对象浏览器中未列出的对象属性

vba - 在 Excel 2007 中自动导入文本