0

I have the following code:

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

However, when I scan new box nothing happens, is all of the syntax correct? I get no errors, any input will be greatly appreciated.

6
  • 1
    Have you tried to put a breakpoint on Case "NEW-BOX"? Commented Feb 26, 2013 at 10:22
  • 1
    Insert a Case Else: MsgBox "You provided " & UDase(Target.Value)before the End Select statement to see what is being checked. Commented Feb 26, 2013 at 10:25
  • 1
    What is it you want to print in the MsgBox? Commented Feb 26, 2013 at 10:26
  • @AndreyGordeev I have tried a breakpoint however the code executes fine and just does not set the variable. Commented Feb 26, 2013 at 10:32
  • @mattboy I would like it to print the range of the selection, so I can check that the variable is being set, however it does not set at all Commented Feb 26, 2013 at 10:33

2 Answers 2

2

Is this what you're after?

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

EDIT: If you want to set the FinalAddr to the selection and then test that it works:

   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
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry no, I am trying to set the FinalAddr variable to the range, and then get the value of that range and pass it to the voice synthesis in the next case
Then just remove the .address
Right, just remove .Address in second example to get the value, see updated post.
Thank you, this is correct and solved my problem, everything now works fine.
0

If you want the value in the cell you should be using Finaladdr = Selection.Value, but if you want the address the selection you should use = Selection.AddressLocal()

However if you want to refer to the range youll need to use Set and then .AddressLocal() when refering to the cells address.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.