Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default 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 ,
"Mikus" 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default 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 ,
"Mikus" 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default 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 ,
"Mikus" wrote:

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
3-Color Scale Vlookup for Current Month/Previous/Pre-Previous NeoFax Excel Discussion (Misc queries) 2 January 8th 10 07:04 PM
previous post Increment a filename. katagrga Excel Discussion (Misc queries) 0 July 23rd 09 03:46 PM
Inserting a new worksheet that is linked to a previous one Joanne Excel Worksheet Functions 0 December 8th 08 04:04 PM
Inserting new row and pasting formulas from previous row Redsphynx Excel Discussion (Misc queries) 4 May 8th 08 12:44 AM
Can I retain functions from a previous cell when inserting a new r Philobr Excel Discussion (Misc queries) 1 December 3rd 04 10:41 AM


All times are GMT +1. The time now is 12:28 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"