Excel无法读出变量

时间:2013-02-26 10:19:04

标签: excel excel-vba excel-2007 vba

我有以下代码:

Dim FinalAddr As Range
Dim Final As String

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
On Error Resume Next


Select Case UCase(Target.Value)


   Case "NEW-BOX"
        Selection.Offset(-1, 2).Select
        FinalAddr = Range(Selection.Address)
        MsgBox FinalAddr ' Debug, nothing comes up however
        Selection.ClearContents
        Selection.Offset(1, -2).Select

   Case "END-BOX"
        Final = FinalAddr.Value
        Application.Speech.Speak (Final)

    End Select
End Sub

然而,当我扫描新盒子时没有任何反应,是不是所有的语法都正确?我没有错误,任何输入都将非常感激。

2 个答案:

答案 0 :(得分:2)

这就是你要追求的吗?

   Case "NEW-BOX"
        Selection.Offset(-1, 2).Select
        MsgBox Selection.Address ' Debug, nothing comes up however
        Selection.ClearContents
        Selection.Offset(1, -2).Select

编辑:如果你想将FinalAddr设置为选择,然后测试它是否有效:

   Case "NEW-BOX"
        Selection.Offset(-1, 2).Select
        Set FinalAddr = Selection
        MsgBox FinalAddr ' Debug, nothing comes up however
        Selection.ClearContents
        Selection.Offset(1, -2).Select

答案 1 :(得分:0)

如果您想要单元格中的值,则应使用Finaladdr = Selection.Value,但如果您希望地址为选择,则应使用= Selection.AddressLocal()

但是,如果您想参考范围,则需要在引用单元格地址时使用Set然后.AddressLocal()