根据常用单元格值创建范围并创建匹配的范围名称

时间:2019-06-05 10:43:12

标签: excel vba range

我有一个文件,其中所有数据都位于A列中。此外,我在G列中具有许多唯一标识符。我想根据G列中的标识符对A列中的所有数据进行分组

Sub createRanges()

Dim LastRowAll, LastRowUnique As Integer
Dim x, y
Dim rng As Variant

Range("G3").Select
Selection.End(xlDown).Select
LastRowAll = ActiveCell.Row

Range("A2").Select
Selection.End(xlDown).Select
LastRowUnique = ActiveCell.Row


For Each x In Range("G3:G" & LastRowUnique)
   For Each y In Range("A2:A" & LastRowAll)
      If IsEmpty(rng) And y = x Then
         rng = y.Address(0, 0)
      ElseIf y = x Then
         rng = rng & "," & y.Address(0, 0)
      End If
   Next y

   rng_formula = "=SUBSTITUTE(x.Value,""-"","""")"
   rng_name = Application.Evaluate(rng_formula)
   ' This yields error 2029
   Range(rng).Name = rng_name
   With ThisWorkbook
     .Names(rng).Delete
   End With
Next x

End Sub

为什么以下内容在我的脚本中不起作用;

 rng_formula = "=SUBSTITUTE(x.Value,""-"","""")"
 rng_name = Application.Evaluate(rng_formula)
如果我检查元素,则

X.value等于"2016-07-15"。我需要删除-,因为在命名范围内不可能使用此字符。

谢谢

0 个答案:

没有答案