运行时错误" 1004"具有命名范围值

时间:2017-04-10 12:00:03

标签: excel vba excel-vba range

尝试返回命名范围的值 - first_yr - 即单元格F1595。它有这个公式

=MIN(us_forcst_v2!J9:J14)

当我单步执行宏时,我在行处收到1004错误:

LY1 = Range("last_yr").Value 

此外,FY1变量为"为空"。我甚至用手动输入的值替换了公式,得到了相同的结果。

Sub CommandButton3_Click()
Dim us_pop_data As Worksheet
Dim i, FY1, LY1, NumYrs1 As Long
Dim first_yr, last_yr, test As Range
Application.ScreenUpdating = False
Sheets("us_pop_data").Range("test").ClearContents
With Sheets("us_pop_data").Activate
    'FY1 = Range("first_yr")
    FY1 = Range("f1595").Value
    LY1 = Range("last_yr").Value  'stops here
    NumYrs1 = LY1 - FY1

    For i = 0 To NumYrs1
        Cells(1596, 15 + i) = FY1 + i
    Next i
End With
Application.ScreenUpdating = True
End Sub

有人可以协助解决错误吗?

由于FY1和LY1现在获得了值,但是我做了一些更改并且更好一些,但是值没有放在由"单元格(1596,15 + i)= FY1 + 1"定义的区域中。 msgbox在循环时给出正确的值,但值永远不会放在范围内。

Sub CommandButton3_Click()
Dim us_pop_data As Worksheet
Dim i, FY1, LY1, NumYrs1 As Long
Dim frcstcolhdr_t1, first_yr, last_yr, test As Range

Set FY1 = Worksheets("us_pop_data").Range("first_yr")
Set LY1 = Worksheets("us_pop_data").Range("last_yr")
    With Sheets("us_pop_data")
        .Range("frcstcolhdr_t1").ClearContents
        NumYrs1 = LY1 - FY1
        For i = 0 To NumYrs1
            Cells(1596, 15 + i) = FY1 + i
            MsgBox "Value is" & Cells(1596, 15 + i).Value
         Next i
   End With
End Sub

谢谢!

0 个答案:

没有答案