Help In updating my code, Employee Number Generator

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
Office Version
  1. 2016
Platform
  1. Windows
I have a code that generates employee numbers at random, I have noticed a few errors on it and can not fix them.

  1. It changes the whole row to red, when it should only be the employee number that is red.
  2. The code only works once, then it will only work if there are blank cells above a cell with a value. E.G. cell A1=value and cell A7=value Cells A2 to A6 are empty then the code will work as these cells are above A7 and empty, so it will place a value into them.

What I need is for it to check Column B and if it has a value then input a value in the cell/s in Column A, and only turn column A to Red.

Please see attached link
Download a demo sheet from this link
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Sorry Davesexcel

There has probably been a misunderstanding on my behalf. I took this bit of what you said

"I'm not sure how to test this, I know it works when I hard code the employee number. "

I then wanted to test the code for myself and added the duplicate values in Yellow and Blue in column A, I wanted to see if it would find them and replace them with new number, which it did not do.

If you are saying the code will not create duplicates when FIRST used, then, there is no need to look for duplicates as they can not be created.

Is this correct?
 

Attachments

  • Capture2.JPG
    Capture2.JPG
    86.7 KB · Views: 3
Upvote 0
Yes, I might have the r= in the wrong spot it should be in the 2nd loop

VBA Code:
Private Sub CommandButton1_Click()
    Dim lastRow As Long
    Dim thisRow As Long, r As String, x

    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
    For thisRow = 1 To lastRow
        x = 1
        If Cells(thisRow, "B").Value <> "" And Cells(thisRow, "A") = "" Then
          
            Do Until x = 0
             r = "EmpNo. " & Format$(WorksheetFunction.RandBetween(0, 99999999), "00000000")
                x = Application.WorksheetFunction.CountIf(Range("A:A"), r)
                If x = 0 Then
                    Cells(thisRow, "A").Value = r
                    Cells(thisRow, "A").Font.Color = vbRed
                End If
            Loop
        End If
    Next thisRow
End Sub
 
Upvote 0
Davesexcel

Firstly thanks for the update, Can you confirm the following?

Are you saying that the code WILL NOT create a duplicate? AS

I manually placed 3 duplicates and it did NOT find or REPLACE them, it created new numbers for records that did not have and numbers as shown in red, see image.

If the code CAN NOT create duplicates then there is NO need to look for duplicates.

However, if the codes job is also to LOOK for duplicates and then replace them it is NOT finding them. This is what I just ran.

  • Column C is for your REFF. ONLY
  • Numbers in Red in Column A are the new employee numbers created.
  • Column A has cells in YELLOW AND BLUE these are duplicates as shown in GREEN in column A.

If the code is to find and replace duplicates, then it has not done this and is not working.
If the code does not create duplicates then there is no need to search for duplicates as none will be created.

Could you please clarify

Thank you so much, for what you have done so far.
 

Attachments

  • Capture4.JPG
    Capture4.JPG
    73.4 KB · Views: 5
Upvote 0
The code will not add duplicate numbers, putting duplicate numbers in column A is redundant.
I dragged column B down 40k rows and ran the code, it did not punch out any duplicate numbers.(it took a minute or so to run for 40k rows)

Test it out
VBA Code:
Private Sub CommandButton1_Click()
    Dim lastRow As Long
    Dim thisRow As Long, r As String, x

    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
    For thisRow = 1 To lastRow
        x = 1
        If Cells(thisRow, "B").Value <> "" And Cells(thisRow, "A") = "" Then

            Do Until x = 0
                r = "EmpNo. " & Format$(WorksheetFunction.RandBetween(0, 99999999), "00000000")
                x = Application.WorksheetFunction.CountIf(Range("A:A"), r)
                If x = 0 Then
                    Cells(thisRow, "A").Value = r
                    Cells(thisRow, "A").Font.Color = vbRed
                End If
            Loop
        End If
    Next thisRow
    countDupes
End Sub
Sub countDupes()
    Dim sh As Worksheet, rng As Range
    Set sh = ActiveSheet
    With sh
        Set rng = .Range("C2:C" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        rng = "=COUNTIF(A:A,A2)"
        rng.Value = rng.Value
        .Range("D2") = "=COUNTIF(C:C,"">1"")"
    End With

End Sub
 
Upvote 0
Super, Thank you very much. I will use this now
 
Upvote 0

Forum statistics

Threads
1,214,537
Messages
6,120,096
Members
448,944
Latest member
SarahSomethingExcel100

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