这些单独引用的单元格可以转换为范围吗?

时间:2018-04-27 10:10:43

标签: excel vba excel-vba

我有点不确定如何使用以下而不是单元格来命名范围。

此工作表上的单元格遍布整个地方,以后需要添加更多工作表,因此使用命名范围会让我的生活变得更加容易。

我只是不确定Const属性是否允许我使用命名范围,如果是这样的话。

'Establish if Mandatory Cells have been completed
Const EM_1 = "I6,I8,B12:I12,B16:I16,I18,I28,I30,I32,I36,I38,I40,I44,I46,I48,"
Const EM_2 = "I56,I58,I60,I66,I68,I70,I72,I74,I78,I80,I82,I88,I96,I98,"
Const EM_3 = "B104:I104,I106,B111:I111,B115:I115,L6:Z120"

Const EM = EM_1 & EM_2 & EM_3   'Required ranges on Sheet "Engagement Form"
Const DP = "B9:B19"             'Required ranges on Sheet "Data Protection Crib Sheet"
Const AA = "B9:B44"             'Required ranges on Sheet "A & A Crib Sheet"
Const GC = "B9:B17"             'Required ranges on Sheet "Governance Crib Sheet"

Dim wsEM As Worksheet, wsDP As Worksheet, wsAA As Worksheet, wsGC As Worksheet

Set wsEM = ThisWorkbook.Worksheets("Engagement Form")     'Reference to the sheet
Set wsDP = ThisWorkbook.Worksheets("Data Protection Crib Sheet")
Set wsAA = ThisWorkbook.Worksheets("A & A Crib Sheet")
Set wsGC = ThisWorkbook.Worksheets("Governance Crib Sheet")

Dim rng As Variant, invalid As Long

For Each rng In Array(wsEM.Range(EM), wsDP.Range(DP), wsAA.Range(AA), wsGC.Range(GC))
    'rng.Cells.Count = total cells in required range for the sheet being processed
    'Application.WorksheetFunction.CountA(rng) = total cells containing data
    invalid = invalid + (rng.Cells.Count - Application.WorksheetFunction.CountA(rng))
Next

If invalid > 0 Then
MsgBox "Something's missing. Please check and try again. There are " & _
                  invalid & " incomplete fields.", vbOKOnly
Exit Sub
End If

1 个答案:

答案 0 :(得分:1)

不,你不能使范围保持不变。常量必须是ByteBooleanIntegerLongCurrencySingleDouble,{{ 1}}(目前不支持),DecimalDateString

请参阅文档:Const Statement