Private Sub Generate_Product_Code_Click()
'Sub UniqueCodes()
Dim LengthOfCode As Long, strCodeChars As String, uniquecode As String
Dim x As Long, r As Long
'*************** [START] Trying to Append another on this ***************
Dim sProductCode As String
Dim sProductName As String
Dim sRequestedBy As String
'Dim lngUnitStartRow As Long, lngUnitStartColumn As Long, lngUnitLastRow As Long
'Dim lngProductNameStartRow As Long, lngProductNameStartColumn As Long, lngProductNameLastRow As Long
Dim lngLastRow As Long, i As Long
'All inputs are mandatory. In case of missing inputs prompt user and exit.
If Product_Name.Text = "" Then
MsgBox "All fields are mandatory - Please enter Product Name."
Exit Sub
End If
If My_ID.Text = "" Then
MsgBox "All fields are mandatory - Please enter your ID."
Exit Sub
End If
'Determine last row with data in Product Code List database
lngLastRow = shProductCodeList.Cells(shProductCodeList.Rows.Count, "A").End(xlUp).Row
'Search if the Product Code generated and Product Name already exists. If it exists prompt user and exit. If it does not exist, add it to database.
Set rngProductCode = shProductCodeList.Range(shProductCodeList.Cells(2, 1), shProductCodeList.Cells(lngLastRow, 1)).Find(sProductCode)
If Not rngProductCode Is Nothing Then
Product_Code.Text = sProductCode
MsgBox "Product Code " & sProductCode & " already exists in database."
Exit Sub
Set rngProductName = shProductCodeList.Range(shProductCodeList.Cells(2, 2), shProductCodeList.Cells(lngLastRow, 1)).Find(sProductName)
If Not rngProductName Is Nothing Then
Product_Name.Text = sProductName
MsgBox "Product Name " & sProductName & " already exists in database."
Exit Sub
Else
Product_Code.Text = sProductCode
shProductCodeList.Cells(lngLastRow + 1, 1) = sProductCode
shProductCodeList.Cells(lngLastRow + 1, 2) = Product_Name.Text
shProductCodeList.Cells(lngLastRow + 1, 3) = _ID.Text
End If
'***************
LengthOfCode = 12
strCodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
With CreateObject("scripting.dictionary")
For r = 2 To r
Do
For x = 1 To LengthOfCode
uniquecode = uniquecode & Mid(strCodeChars, Application.RandBetween(1, 36), 1)
Next
If Not (.exists(uniquecode)) Then .Add uniquecode, 1: Cells(r, 3) = uniquecode: uniquecode = "":
Exit Do
Loop
Next
End With
'***************
If lngLastRow > 1 Then
'Format Product Code List Table.
shProductCodeList.Visible = xlSheetVisible
shProductCodeList.Select
shProductCodeList.Range(shProductCodeList.Cells(2, 1), shProductCodeList.Cells(lngLastRow + 1, 6)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 10
.TintAndShade = -0.249946592608417
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 10
.TintAndShade = -0.249946592608417
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 10
.TintAndShade = -0.249946592608417
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 10
.TintAndShade = -0.249946592608417
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.499984740745262
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.499984740745262
.Weight = xlHairline
End With
With Selection.Font
.Name = "Calibri Light"
.Size = 9
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
'Make alternate rows appear in different color for making it easier to read data
For i = 2 To lngLastRow + 1
If i Mod 2 = 1 Then
shProductCodeList.Range(shProductCodeList.Cells(i, 1), shProductCodeList.Cells(i, 6)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
End If
Next i
shProductCodeList.Visible = xlSheetVeryHidden
End If
'*************** [END] ***************
End If
MsgBox "Product Code " & UCase(sProductCode) & " successfully added to database."
End Sub