Consulting

Results 1 to 8 of 8

Thread: Solved: Msg Box code for a RANGE

  1. #1

    Solved: Msg Box code for a RANGE

    the following only works for E14. I want to see the MSG BOX upon the return of "1 EMT" for any cell within range E14:E200, but when I use "E14:E200" as Range, I get a Runtime Error 13: Type mismatch. Any ideas?

    I should mention that I am already using Validation List for the values in this range, so that option is out.

    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rng As Range
    Set rng = Range("e14")
    If Not Intersect(Target, rng) Is Nothing Then
    If rng = ("1 EMT") Then
    MsgBox "option for move-in/move-out only"
    End If
    End If
    Set rng = Nothing
    End Sub
    [/VBA]
    Last edited by Bruce Gyger; 07-08-2010 at 01:25 PM.

  2. #2
    Try omitting the parens from this If rng =("1 EMT")

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I think it is Target you want to check, not Rng.
    [vba]Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    Dim rng As Range
    Set rng = Range("E14:E200")
    If Not Intersect(Target, rng) Is Nothing Then
    If Target = "1 EMT" Then
    MsgBox "option for move-in/move-out only"
    End If
    End If
    Set rng = Nothing
    End Sub [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4

    Mack wins the prize

    That did it. Target rather than rng. Thanks.

  5. #5
    Try this
    I added a line to avoid debug if you tried for example to clear the contents of more than one cell within the range(E14:E200)
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
        Dim rng As Range
        Set rng = Range("E14:E200")
        If Not Intersect(Target, rng) Is Nothing Then
            If Target.Count > 1 Then Exit Sub
            If Target = "1 EMT" Then
                MsgBox "Option for move-in/move-out only"
            End If
        End If
        Set Target = Nothing
    End Sub

  6. #6
    Excellent. Thank you.

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Yasser
    If Target.Count > 1 Then Exit Sub
    Best to place this right after the Dim statements to minimise the lines of code to be processed.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Nice tip Mr. mdmackillop
    We're learning from you every day...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •