Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Creating an ID number

The code below creates an ID number in column A of the worksheet when an
entry is made/copied into column B of the same row.

The first row is made up of column headings. It works fine when more than
one row of data is copied into B2 but if only one row is copied then it goes
pear shaped.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'ID number
Dim rng As Range, rng1 As Range
Dim cell As Range, val As Long
Dim rngB As Range
On Error GoTo errhandler
If Target.Column = 2 Then
Application.EnableEvents = False
Set rngB = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
Set rng = rngB.Offset(0, -1)
val = Application.Max(rng)
If Intersect(rng, Cells(1, 1)) Is Nothing Then
On Error Resume Next
Set rng1 = rng.SpecialCells(xlBlanks)
On Error GoTo errhandler
If Not rng1 Is Nothing Then
For Each cell In rng1
val = val + 1
cell.Formula = val
Next
End If
End If
End If
errhandler:
Application.EnableEvents = True
End Sub

I had help with the code from this group so don't even know how it works let
alone try to amend it.

Hope someone can help.

Thanks in advance.

Gareth


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,089
Default Creating an ID number

Gareth

Try this:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'ID number
Dim rng As Range, rng1 As Range
Dim cell As Range, val As Long
Dim rngB As Range
On Error GoTo errhandler
If Target.Column = 2 Then
Application.EnableEvents = False
Set rngB = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
Set rng = rngB.Offset(0, -1)
val = Application.Max(rng)
If Intersect(rng, Cells(1, 1)) Is Nothing Then
On Error Resume Next
Set rng1 = Intersect(rng.SpecialCells(xlBlanks), Range("A:A")) '
<<<<
On Error GoTo errhandler
If Not rng1 Is Nothing Then
For Each cell In rng1
val = val + 1
cell.Formula = val
Next
End If
End If
End If
errhandler:
Application.EnableEvents = True
End Sub

Regards

Trevor


"Gareth" wrote in message
...
The code below creates an ID number in column A of the worksheet when an
entry is made/copied into column B of the same row.

The first row is made up of column headings. It works fine when more than
one row of data is copied into B2 but if only one row is copied then it

goes
pear shaped.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'ID number
Dim rng As Range, rng1 As Range
Dim cell As Range, val As Long
Dim rngB As Range
On Error GoTo errhandler
If Target.Column = 2 Then
Application.EnableEvents = False
Set rngB = Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
Set rng = rngB.Offset(0, -1)
val = Application.Max(rng)
If Intersect(rng, Cells(1, 1)) Is Nothing Then
On Error Resume Next
Set rng1 = rng.SpecialCells(xlBlanks)
On Error GoTo errhandler
If Not rng1 Is Nothing Then
For Each cell In rng1
val = val + 1
cell.Formula = val
Next
End If
End If
End If
errhandler:
Application.EnableEvents = True
End Sub

I had help with the code from this group so don't even know how it works

let
alone try to amend it.

Hope someone can help.

Thanks in advance.

Gareth




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
Creating a number of Hyperlinks one after another. Brian L Excel Discussion (Misc queries) 1 December 1st 09 07:35 PM
Creating number from groups of numbers Jordan Excel Worksheet Functions 13 April 9th 07 09:20 PM
Creating a number from different fields. chzabel Excel Worksheet Functions 3 August 25th 06 02:12 PM
Creating zero's in front of number and after number dyukon Excel Discussion (Misc queries) 3 January 12th 06 03:46 PM
Creating a certain number of entries based on a number in a cell PPV Excel Worksheet Functions 4 June 16th 05 10:25 PM


All times are GMT +1. The time now is 11:30 AM.

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

About Us

"It's about Microsoft Excel"