Code and more code...

Andrew_Rossington

Board Regular
Joined
Feb 12, 2007
Messages
121
Hello.

My question is two-fold.

One: I have a table on a worksheet - simple stuff. I'm looking for some code to launch Data > Form (y'know, for adding new records and such). I tried the excel help, but it's not very helpful. Would anyone be able to point me in the right direction?

Two: Based on the above need, does anyone know how to automatically populate a column with a consecutive number for each new record added? (Kind of like a unique ID for each record, whereby if the latest record is number 5, I add a new one and this column has number 6 automatically entered.)

I'd be very grateful for any help with this.

Thanks.
 
Are you sure you want this to run every time you change the selection or every time you make a change to a cell?

At any rate

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Ce As Range, LstRw As Long

LstRw = Cells(Rows.Count, "B").End(xlUp).Row
For Each Ce In Range("B4:B" & LstRw)
    If Len(Ce.Value) = 0 Then
        Range("A" & Ce.Row).Value = vbNullString
    Else
        Range("A" & Ce.Row).Formula = "=Row() - 3"
    End If
Next Ce

End Sub

Hope this helps!

Just when I thought it was working!

For some reason, this code seems to be working fairly erratically.

I've changed parts of the code to match my needs, however now it doesn't seem to work 100% of the time. If I enter a value in B4 and then B5, then delete both of them; the formula stays in A5 even though B5 has nothing in the cell.

Does anyone know why this happens?
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
There is an issue there of the deletion Perhaps something like. I also took care of another problem that I happened to notice. Hope this helps you out.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Ce As Range, LstRw As Long

Application.Screenupdating = False
LstRw = Cells(Rows.Count, "B").End(xlUp).Row
LstRw2 = Cells(Rows.Count, "A").End(xlUp).Row
If LstRw < LstRw2 Then LstRw = LstRw2
If LstRw < 4 Then Exit Sub
For Each Ce In Range("B4:B" & LstRw)
    If Len(Ce.Value) = 0 Then
        Range("A" & Ce.Row).Value = vbNullString
    Else
        Range("A" & Ce.Row).Formula = "=Row() - 3"
    End If
Next Ce
Application.Screenupdating = True

End Sub
 
Upvote 0
There is an issue there of the deletion Perhaps something like. I also took care of another problem that I happened to notice. Hope this helps you out.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Ce As Range, LstRw As Long

Application.Screenupdating = False
LstRw = Cells(Rows.Count, "B").End(xlUp).Row
LstRw2 = Cells(Rows.Count, "A").End(xlUp).Row
If LstRw < LstRw2 Then LstRw = LstRw2
If LstRw < 4 Then Exit Sub
For Each Ce In Range("B4:B" & LstRw)
    If Len(Ce.Value) = 0 Then
        Range("A" & Ce.Row).Value = vbNullString
    Else
        Range("A" & Ce.Row).Formula = "=Row() - 3"
    End If
Next Ce
Application.Screenupdating = True

End Sub

Thank you very much for this. You'll be happy to know it's working perfectly.
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,428
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top