+ Reply to Thread
Results 1 to 7 of 7

Extending the range of the current code

Hybrid View

  1. #1
    Registered User
    Join Date
    10-08-2014
    Location
    london
    MS-Off Ver
    2007
    Posts
    14

    Extending the range of the current code

    Hi Forum

    Below is some code, currently the code displays only in one row specifically cells A1,B1,C1, I need the same functions to happen for an additional 9 rows. Ive tried and failed for the last couple of hours trying to adjust it.

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$A$1" Then
            Range("$B$1").Value = Range("A1").Value - Range("C1").Value
            Range("C1").Value = Range("A1").Value
            
        
            
              End If
            
        End Sub
    Any help would be greatly appreciated
    Last edited by Fotis1991; 10-13-2014 at 08:59 AM. Reason: code taggs ..........

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Extending the range of the current code

    1) please note that moderator edited your post and added code tags - see http://www.excelforum.com/forum-rule...rum-rules.html why
    2) Try:
    Private Sub Worksheet_Change(ByVal Target As Range)
    dim cell as range
    If not intersect(Target, Range("A1:A10")) is nothing then 
      for each cell in intersect(Target, Range("A1:A10"))
        cell.offset(0,1).Value = cell.Value - cell.offset(0,2).Value
        cell.offset(0,2).Value = cell.Value 
      next cell
    End If
    
    End Sub
    Best Regards,

    Kaper

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Extending the range of the current code

    You should also really disable events if you are going to change cells in a Change event.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Extending the range of the current code

    Basically - yes, but here we change only "non-triggering" cells, so for changed cells cell.offset(0,x), where x=1;2
    intersect(Target, Range("A1:A10"))
    Is Nothing

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Extending the range of the current code

    I know but you're still doing unnecessary processing by raising the event again, and if the code changes in future it will be easy to overlook (at first anyway!). IMO, it's better practice to disable events if you're going to do something that will raise that event again.

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Extending the range of the current code

    As usually - there are pros and cons.
    I am not 100% convinced.
    You mentioned pro, so let me raise con
    If for some reason code stops while events are disabled, they will remain disabled until: either explicitely enabled by user (rather experienced one only) or excel is restarted.
    Especially for less experienced users it can be really frustrating situation.

    Just to show coys1717: code which includes Rory suggestion could be:
    Private Sub Worksheet_Change(ByVal Target As Range)
    dim cell as range
    If not intersect(Target, Range("A1:A10")) is nothing then 
      for each cell in intersect(Target, Range("A1:A10"))
        application.enableevents = false
        cell.offset(0,1).Value = cell.Value - cell.offset(0,2).Value
        cell.offset(0,2).Value = cell.Value 
        application.enableevents = true
      next cell
    End If
    End Sub

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Extending the range of the current code

    That's what error handlers are for.

+ 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. Replies: 3
    Last Post: 11-22-2012, 11:19 PM
  2. Help extending code into loop
    By beng404 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-18-2011, 11:27 AM
  3. Extending existing VBA code
    By TRJJK73 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-07-2008, 01:49 PM
  4. Help extending the peramitors of a working VBA code
    By howardjo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-03-2008, 06:21 AM
  5. Event Procedure - Extending code
    By TBD in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-29-2005, 11:05 AM

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