SKU随机字母数字值生成器

时间:2014-11-15 16:14:57

标签: excel-vba vba excel

我有一个代码

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _ 
Cancel As Boolean) 
Dim ltr, rNum, AlphaLtrs 
AlphaLtrs = "ABCDEFGHIGKLMNOPQRSTUVWXYZ" 
selLtr = Application.RoundUp(Rnd() * 26, 0) 
ltr = Mid(AlphaLtrs, selLtr, 1) 
rNum = Application.RoundUp(Rnd() * 999999, 0) 
ActiveCell.Value = ltr & rNum 
Target.Offset(0, 1).Select 
End Sub  

我需要的是,将双击功能更改为回车

并且如果可能的话,我可以将列A的值添加到生成的代码中,如Jeans-A545145

2 个答案:

答案 0 :(得分:1)

只要选择了一个单元格,就会为在B列中选择的任何单元格添加一个值。将其粘贴到该工作表的代码模块中:

编辑:不确定这是否是您想要的,但现在只有在目标单元格为空时才会这样做:

编辑2:Woops,在IF中发现了一个bug。如果您选择多个单元格,则IF Target.Value = ""部分会出错。我把它分成了第二个IF:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ltr, rNum, AlphaLtrs, selLtr

If Not Intersect(Target, Me.Columns(2)) Is Nothing And _
   Target.Cells.Count = 1 Then
    If Target.Value = "" Then

        AlphaLtrs = "ABCDEFGHIGKLMNOPQRSTUVWXYZ"
        selLtr = Application.RoundUp(Rnd() * 26, 0)
        ltr = Mid(AlphaLtrs, selLtr, 1)
        rNum = Application.RoundUp(Rnd() * 999999, 0)
        Target.Value = Me.Range("A" & Target.Row) & "-" & ltr & rNum
    End If
End If
End Sub

答案 1 :(得分:0)

也许:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
    Dim ltr, rNum, AlphaLtrs
    AlphaLtrs = "ABCDEFGHIGKLMNOPQRSTUVWXYZ"
    selLtr = Application.RoundUp(Rnd() * 26, 0)
    ltr = Mid(AlphaLtrs, selLtr, 1)
    rNum = Application.RoundUp(Rnd() * 999999, 0)
    ActiveCell.Value = ltr & rNum
    Target.Offset(0, 1).Select
Application.EnableEvents = True
End Sub