vba set x = range(a); method' Range'对象' _Global'失败

时间:2015-04-23 17:38:34

标签: vba range global

Set x = Range("services" & sheetname)上的以下代码段失败,错误消息为method 'Range' of object '_Global' failed。 有人可以帮忙吗?

Sub add_Click()
    Dim sheetname As String
    Dim x As Range
    sheetname = selectSheetName(form1.age.value)
    Sheets(sheetname).activate
    Set x = Range("services" & sheetname) '<-- error here
end sub
Function selectSheetName(byval age as integer)
    If (age > 20 And age < 36) Then
        selectSheetName = "21-35"
    End If
    If (age > 35 And age < 61) Then
        selectSheetName = "36-60"
    End If
    If (age > 60 And age < 81) Then
        selectSheetName = "61-80"
    End If
End Function`

有3张名为:&#34; 21-35&#34 ;; &#34; 36-60&#34 ;; &#34; 61-80&#34;

在每张工作表中都有一个名为&#34; services&#34; &安培;片材。

例如在表格&#34; 21-35&#34;范围是&#34; services21-35&#34;。

1 个答案:

答案 0 :(得分:1)

services21-35不是有效的范围名称:我无法创建该名称,因为它包含-