+ Reply to Thread
Results 1 to 10 of 10

Extending Range in Code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-10-2017
    Location
    Wellington, New Zealand
    MS-Off Ver
    2010
    Posts
    15

    Exclamation Extending Range in Code

    Hi,

    I have some code that stores the values the are typed into a cell. This is so I can track dates and how much they changed from start to finish.

    The code that I have does this function, but it only works for A1 and nothing else.

    I want this to work on a line by line basis as each row in excel is a different piece of work.

     Private Sub Worksheet_Change(ByVal Target As Range)
        Dim lCol As Long
          If Not Intersect(Target, Range("A1")) Is Nothing Then
              Application.EnableEvents = False
              'Get the last column for Row A. This will determine where the value needs to be copied
              lCol = Cells(1, Columns.Count).End(xlToLeft).Column
              'Copy to the first row of the next column found above
              Cells(1, lCol + 1).Value = Range("A1").Value
              Application.EnableEvents = True
          End If
    
    End Sub
    Any help would be great!

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Extending Range in Code

    If Not Intersect(Target, Range("A1")) Is Nothing Then
    this line is what triggers the code, it's looking for a cell value change in the specified range (A1). To extend the range change A1 to..... A1:A10000.....or A1:Z1.....or A1:Z10000, whatever range is needed.

    However, the code to copy bits around is done with:

    lCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Cells(1, lCol + 1).Value = Range("A1").Value
    this only looks for the next available cell in row 1. In order to make this copy to the next available cell of whichever row was just changed, change it to:

    lCol = Cells(target.row, Columns.Count).End(xlToLeft).Column
    Cells(target.row, lCol + 1).Value = Range("A" & target.row).Value

  3. #3
    Registered User
    Join Date
    01-10-2017
    Location
    Wellington, New Zealand
    MS-Off Ver
    2010
    Posts
    15

    Re: Extending Range in Code

    That worked perfectly! Thanks for you help BellyGas.

    If possible I have one more question. If I wanted to offset the output from changes in column A to start printing them at the end of the spreadsheet (say from column Z onwards), is this possible?

    Thanks
    Last edited by trobb; 01-19-2017 at 04:47 PM.

  4. #4
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Extending Range in Code

    Not too sure what you mean but I'll have a guess.

    lCol = Cells(target.row, Columns.Count).End(xlToLeft).Column
    Cells(target.row, lCol + 1).Value = Range("A" & target.row).Value
    This bit selects the last column with data in it for the row that's changed and writes to it. To force this to write to column Z replace with:

    range("Z" & target.row).value = Range("A" & target.row).Value

  5. #5
    Registered User
    Join Date
    01-10-2017
    Location
    Wellington, New Zealand
    MS-Off Ver
    2010
    Posts
    15

    Re: Extending Range in Code

    Hi

    I don't think I explained the problem the best. I'll try again. Sorry if you have already suggested the fix above.

    People will enter dates into column A, of which each row will be an individual piece of work. I want to be able to record every new date input into column A for each row. So the code that you fixed up for me worked (it records the newly input dates like I wanted starting in column B and so on). I need this code to work how it is now (printing all input dates) but I need the printing to start in column Z but column A will still remain the place where people input dates.

    Thanks so much for you help BellyGas!
    Last edited by trobb; 01-25-2017 at 08:49 PM.

  6. #6
    Registered User
    Join Date
    01-10-2017
    Location
    Wellington, New Zealand
    MS-Off Ver
    2010
    Posts
    15

    Re: Extending Range in Code

    Please Help!

  7. #7
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Extending Range in Code

    I'm still not fully understanding you.....I think what you mean is you want the date logging to be in column Z rather than B.....and this will do that:

    range("Z" & target.row).value = Range("A" & target.row).Value

  8. #8
    Registered User
    Join Date
    01-10-2017
    Location
    Wellington, New Zealand
    MS-Off Ver
    2010
    Posts
    15

    Re: Extending Range in Code

    Sorry about this.

    I still need the code to keep a track of all the values that have been entered in column A but I need to output to start in column Z then AA then AB etc.

    I have a picture that might explain better.

    This code is what I need except I need it to not start recording in the column beside A (where data is input).

      Private Sub Worksheet_Change(ByVal Target As Range)
        Dim lCol As Long
          If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
              Application.EnableEvents = False
              'Get the last column for Row A. This will determine where the value needs to be copied
              lCol = Cells(Target.Row, Columns.Count).End(xlToLeft).Column
              'Copy to the first row of the next column found above
              Cells(Target.Row, lCol + 1).Value = Range("A" & Target.Row).Value
              Application.EnableEvents = True
          End If
    
    End Sub
    Capture.PNG

  9. #9
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: Extending Range in Code

    ahhhhhh, now I get you.

    Ok, so what you want is for the code that copies the date over to choose the next empty column to write to, after column Z, not overwrite the value in column Z.

    Do this:

    lCol = Cells(Target.Row, Columns.Count).End(xlToLeft).Column
    if lcol < 26 then lcol = 26
    What you're telling Excel is find the next available column number but if it's to the left of column Z make it column Z.

  10. #10
    Registered User
    Join Date
    01-10-2017
    Location
    Wellington, New Zealand
    MS-Off Ver
    2010
    Posts
    15

    Re: Extending Range in Code

    This worked like a charm!

    Thank you so much

+ 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. Extending Range for Code
    By trobb in forum Excel General
    Replies: 1
    Last Post: 01-19-2017, 09:50 AM
  2. [SOLVED] VBA Extending Conditional Formatting Code
    By Mulsiphix in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-31-2015, 12:17 PM
  3. Extending the range of the current code
    By coys1717 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-13-2014, 12:36 PM
  4. Replies: 3
    Last Post: 11-22-2012, 11:19 PM
  5. Help extending code into loop
    By beng404 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-18-2011, 11:27 AM
  6. Extending existing VBA code
    By TRJJK73 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-07-2008, 01:49 PM
  7. 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

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