Philip West
New Member
Hello Everyone,
I'm having a problem with some code that i'm trying to make work. I'll start by saying that this code is the product of a mornings frantic Googleing and I've never written anything in VBA before.
In my spreadsheet, each row in column W has a validation list, with 3 options, one of which is 'Review'. In Column T i have an ID that should be unique per row, but only generated when Review is selected from the list in W. What i thought i would be able to do is create a little bit of code that activates when one of the lists is changed, then checks if that rows ID is already there (so as not to overwrite anything), and if not, creates a new ID (max()+1).
Now, the code I've posted here works just fine if I write Review into a cell that's part of the W8:W500 range. I type review, a msg box pops up and then a new number is written on the same row, but in column T. The problem I am having is that when I select Review from the validation list I get the msg box popup with the correct information but then it doesn't add the number to column T.
I have played about with this most of today and i'm really struggling to work out what I've done wrong. So i'm hoping that someone can see something obvious that I've done wrong.
Thanks in advance.
Philip.
I'm having a problem with some code that i'm trying to make work. I'll start by saying that this code is the product of a mornings frantic Googleing and I've never written anything in VBA before.
In my spreadsheet, each row in column W has a validation list, with 3 options, one of which is 'Review'. In Column T i have an ID that should be unique per row, but only generated when Review is selected from the list in W. What i thought i would be able to do is create a little bit of code that activates when one of the lists is changed, then checks if that rows ID is already there (so as not to overwrite anything), and if not, creates a new ID (max()+1).
Now, the code I've posted here works just fine if I write Review into a cell that's part of the W8:W500 range. I type review, a msg box pops up and then a new number is written on the same row, but in column T. The problem I am having is that when I select Review from the validation list I get the msg box popup with the correct information but then it doesn't add the number to column T.
I have played about with this most of today and i'm really struggling to work out what I've done wrong. So i'm hoping that someone can see something obvious that I've done wrong.
Thanks in advance.
Philip.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Dim KeyCells As Range
Dim NextID As Integer
NextID = Application.WorksheetFunction.Max(Range("T8:W500")) + 1
Set KeyCells = Range("W8:W500")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
If Target.Value = "Review" Then
If IsEmpty(Range("T" & Target.Row)) Then
MsgBox "T" & Target.Row & " " & NextID
Range("T" & Target.Row).Value = NextID
End If
End If
End If
End Sub