+ Reply to Thread
Results 1 to 5 of 5

convert a single-celll range to a cell

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2003
    Posts
    31

    convert a single-celll range to a cell

    The code below generates a type mismatch error as shown. I think it's because 'Target' is a single-cell range, not a cell proper, so 'Target.Value' fails but I don't know how to convert the range to a cell. Help appreciated.

    Private Sub Worksheet_Change(ByVal Target As Range)
    	If Intersect(Target, Range("InputRange")) Is Nothing Then
    		Exit Sub  'the cell that changed isn't in the range we're concerned with
    	ElseIf Target.Value = "" Then                                                                         '<------ type mismatch error here
    		Target.Value = Target.Offset(0, 3).Value    're-assert the default value
    	End If
    End Sub
    Thanks - Jim

    Moderators Note: Please follow Forum Rule #3 and use code tags. Added this time, but please use them in the future…Thanks.
    Last edited by jeffreybrown; 12-28-2012 at 03:35 PM.

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: convert a single-celll range to a cell

    Hi Jim,

    The usage of Target.Value is correct in this context. Perhaps the compare against "" (which implies it is a string) is causing the problem?

    You could try

    
    ElseIf LenB(Target.Value) = 0 Then
    i.e. LenB is the fastest way to check if a variable is empty
    If you like my contribution click the star icon!

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: convert a single-celll range to a cell

    The Target.Value is looking for the Range("InputRange")) . What is in it? It looks like a named range. The code will trigger if the target and named range intersect

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("InputRange")) Is Nothing Then
           'the cell that changed isn't in the range we're concerned with
           If Target.Value = "" Then
           'Do something
             ElseIf Target.Value = "" Then                                                                         '<------ type mismatch error here
            Target.Value = Target.Offset(0, 3).Value    're-assert the default value
            End If
                End If
    End Sub

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: convert a single-celll range to a cell

    The If statement will only work if Target is one cell and doesn't have an error.
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("InputRange")) Is Nothing Or Target.Cells.Count > 1 Or IsError(Target.Value) Then
            Exit Sub  'the cell that changed isn't in the range we're concerned with
        ElseIf Target.Value = "" Then
            Application.EnableEvents = False    '<------ type mismatch error here
            Target.Value = Target.Offset(0, 3).Value    're-assert the default value
            Application.EnableEvents = True
        End If
    End Sub
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    10-10-2003
    Posts
    31

    Re: convert a single-celll range to a cell

    Thanks all for the quick help. Olaf, I couldn't your solution to work (continued to get mismatch errors); AB33, I didn't try your solution because Norie's solution (which I saw at the same time) was more desirable with respect to error catching and since it worked perfectly I never looked back. Again, thanks all. - Jim (Moderator, sorry about the lack of code tags.)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1