VBA 中的引用命名范围

Reference Named Ranges in VBA

在我的 Excel 工作簿中,我将单个单元格设为命名范围。我没有使用 vba 来执行此操作,而是直接转到 Excel 中的 Formulas > Name_Manager > New。我现在想在我正在编写的宏中引用这个命名范围。如果该单元格的背景颜色与我命名范围的背景颜色相匹配,宏将复制该单元格的颜色。

当我在以下代码中将单元格的位置称为 Range("S2") 时,我的宏目前运行良好:

Trans_ECO_Row.Cells(, 13).Value = Trans_Queue_Row.Cells(, 14).Value
    If Trans_Queue_Row.Cells(, 14).Interior.Color = QueueSheet.Range("S2").Interior.Color Then
        Trans_ECO_Row.Cells(, 13).Interior.Color = Trans_Queue_Row.Cells(, 14).Interior.Color
    End If

但是,我通过转到 Excel 中的 Formulas > Name_Manager > New 将单元格 S2 命名为 MGRColor。如何在上面的代码中用 MGRColor 替换 S2?

您可以使用 Range() 函数执行此操作。命名范围将是函数的唯一参数,用双引号括起来:

Range("MGRColor")

您可以像使用任何其他范围对象一样使用它:

Range("MGRColor").Value = ...
Range("MGRColor").Interior.Color = ...
' Etc