Closed Thread
Results 1 to 4 of 4

Inserting value with increment by 1 from previous value

  1. #1
    Mikus
    Guest

    Inserting value with increment by 1 from previous value

    I need unique ID in Row 1

    How do i modify this code to have value of ( 1+value in column A from 1 row
    above ) instead of static 1



    Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub

    On Error GoTo errHandler:

    If Target.Column = 2 And Target.Value <> "" Then
    Cells(Target.Row, 3).Value = "New"
    Cells(Target.Row, 1).Value = 1
    Cells(Target.Row, 5).Select
    End If

    errHandler:
    Application.EnableEvents = True

    End Sub

    I thought that i could search for max in column A and then add 1
    Or remember previous value and insert it in variable somehow

    how do i best acomplish this task ?

  2. #2
    JE McGimpsey
    Guest

    Re: Inserting value with increment by 1 from previous value

    One way:

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    With Target
    If .Count > 1 Then Exit Sub
    If .Column = 2 Then
    If Not IsEmpty(.Value) Then
    On Error GoTo errHandler
    Application.EnableEvents = False
    .Offset(0, 1).Value = "New"
    .Offset(0, -1).Value = Application.Max(Range("A:A")) + 1
    .Offset(0, 3).Select
    End If
    End If
    End With
    errHandler:
    Application.EnableEvents = True
    End Sub




    In article <[email protected]>,
    "Mikus" <[email protected]> wrote:

    > I need unique ID in Row 1
    >
    > How do i modify this code to have value of ( 1+value in column A from 1 row
    > above ) instead of static 1
    >
    >
    >
    > Sub Worksheet_Change(ByVal Target As Range)
    > If Target.Cells.Count > 1 Then Exit Sub
    >
    > On Error GoTo errHandler:
    >
    > If Target.Column = 2 And Target.Value <> "" Then
    > Cells(Target.Row, 3).Value = "New"
    > Cells(Target.Row, 1).Value = 1
    > Cells(Target.Row, 5).Select
    > End If
    >
    > errHandler:
    > Application.EnableEvents = True
    >
    > End Sub
    >
    > I thought that i could search for max in column A and then add 1
    > Or remember previous value and insert it in variable somehow
    >
    > how do i best acomplish this task ?


  3. #3
    Mikus
    Guest

    Re: Inserting value with increment by 1 from previous value

    I already thought up a way to fo this.... i did following:

    Do you think i have some serious flaws here ? Is your way better ?

    Sub Worksheet_Change(ByVal Target As Range)

    Dim iNextValue As Integer
    iNextValue = Cells(Rows.Count, "A").End(xlUp).Row

    If Target.Cells.Count > 1 Then Exit Sub

    On Error GoTo errHandler:

    If Target.Column = 2 And Target.Value <> "" Then
    Cells(Target.Row, 3).Value = "New"
    Cells(Target.Row, 1).Value = iNextValue
    Cells(Target.Row, 5).Select
    End If

    errHandler:
    Application.EnableEvents = True

    End Sub

    "JE McGimpsey" wrote:

    > One way:
    >
    > Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    > With Target
    > If .Count > 1 Then Exit Sub
    > If .Column = 2 Then
    > If Not IsEmpty(.Value) Then
    > On Error GoTo errHandler
    > Application.EnableEvents = False
    > .Offset(0, 1).Value = "New"
    > .Offset(0, -1).Value = Application.Max(Range("A:A")) + 1
    > .Offset(0, 3).Select
    > End If
    > End If
    > End With
    > errHandler:
    > Application.EnableEvents = True
    > End Sub
    >
    >
    >
    >
    > In article <[email protected]>,
    > "Mikus" <[email protected]> wrote:
    >
    > > I need unique ID in Row 1
    > >
    > > How do i modify this code to have value of ( 1+value in column A from 1 row
    > > above ) instead of static 1
    > >
    > >
    > >
    > > Sub Worksheet_Change(ByVal Target As Range)
    > > If Target.Cells.Count > 1 Then Exit Sub
    > >
    > > On Error GoTo errHandler:
    > >
    > > If Target.Column = 2 And Target.Value <> "" Then
    > > Cells(Target.Row, 3).Value = "New"
    > > Cells(Target.Row, 1).Value = 1
    > > Cells(Target.Row, 5).Select
    > > End If
    > >
    > > errHandler:
    > > Application.EnableEvents = True
    > >
    > > End Sub
    > >
    > > I thought that i could search for max in column A and then add 1
    > > Or remember previous value and insert it in variable somehow
    > >
    > > how do i best acomplish this task ?

    >


  4. #4
    JE McGimpsey
    Guest

    Re: Inserting value with increment by 1 from previous value

    As long as you have no blanks in your list, it's more a matter of style
    - I prefer to minimize the number of references by using With..End With,
    since it's significantly faster than accessing the referenced cell. But
    you probably won't be able to do much with the microseconds...

    If you have blanks, then making an entry that fills in the blank will
    generate a duplicate ID.



    In article <[email protected]>,
    "Mikus" <[email protected]> wrote:

    > Do you think i have some serious flaws here ? Is your way better ?


Closed Thread

Thread Information

Users Browsing this Thread

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

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