+ Reply to Thread
Results 1 to 6 of 6

Streching cells using worksheet_change

Hybrid View

  1. #1
    Registered User
    Join Date
    08-01-2013
    Location
    Verona - Italy
    MS-Off Ver
    Excel 2007-2010-2013
    Posts
    33

    Streching cells using worksheet_change

    Hi all, I have a problem using Worksheet_Change. The code is the following:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
    MsgBox (Target.Value)
        Select Case Target
            Case 1
                   '{action 1}
            Case 2
                   '{action 3}
        End Select
    End If
    End Sub
    The problem arises when I put a value in one of the target cells (a1:a5) and I try to extend the same value with the common "streching" trick with the mouse. It gives me code error no. 13: type mismatch. Can someone explain it?
    Thank you very much

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Streching cells using worksheet_change

    Add this at the top and it will avoid the error
    If Target.Count > 1 Then Exit Sub

  3. #3
    Registered User
    Join Date
    08-01-2013
    Location
    Verona - Italy
    MS-Off Ver
    Excel 2007-2010-2013
    Posts
    33

    Re: Streching cells using worksheet_change

    ..but can I also run the code for each cell modified with stretching?

  4. #4
    Registered User
    Join Date
    08-01-2013
    Location
    Verona - Italy
    MS-Off Ver
    Excel 2007-2010-2013
    Posts
    33

    Re: Streching cells using worksheet_change

    Yes cool trick! thanks!

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Streching cells using worksheet_change

    Yes, perhaps better to loop through like so
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim r As Range
    
    If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
        For Each r In Target
            MsgBox r.Value
            Select Case Target
                Case 1
                   '{action 1}
                Case 2
                   '{action 3}
            End Select
        Next r
    End If
    
    End Sub

  6. #6
    Registered User
    Join Date
    08-01-2013
    Location
    Verona - Italy
    MS-Off Ver
    Excel 2007-2010-2013
    Posts
    33

    Re: Streching cells using worksheet_change

    That's perfect. Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Worksheet_Change VBA question - More than one cell - Dependant Cells
    By dredre609 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-28-2014, 03:23 AM
  2. Worksheet_Change not firing when pasting to multiple cells.
    By bg819 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-17-2014, 11:41 AM
  3. Applying code to specific cells when using worksheet_change?
    By Bevmachine in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-13-2013, 11:37 AM
  4. error in Worksheet_Change with merged cells
    By lluisponce in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-19-2010, 02:36 PM
  5. [SOLVED] Worksheet_Change with discontinuous cells
    By JKG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2005, 03:05 PM

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