【问题标题】:"Assignment to Constant" error when trying to get unique values from table尝试从表中获取唯一值时出现“分配给常量”错误
【发布时间】:2018-09-03 18:38:18
【问题描述】:

我正在尝试从表 tblDetailsNames 中获取唯一值,然后粘贴到表 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

【问题讨论】:

    标签: vba excel compiler-errors unique


    【解决方案1】:

    @JohnyL @ashleedawg @Harassed Dad 经过多次尝试后我设法得到了我想要的,感谢大家的回复和指导。

    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
    

    【讨论】:

      猜你喜欢
      • 2018-06-30
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2017-11-29
      相关资源
      最近更新 更多