Private Sub CommandButton1_Click()
With ComboBox1.Value
Dim a, b As String
Dim i, j, k, l, x, q, m, temp As Long
a = ComboBox1.Text
i = Application.WorksheetFunction.Match(a, Range("A1:A1000"), 0)
j = Cells(i, 2)
l = j * 1000
For q = 2 To 100
For m = 2 To 100
If Cells(q, m).Value < 0 Then
k = m
End If
Next
Next
x = l
If Cells(i, GC).Value = temp Then
Click = Click + 1
Else
Click = 0
End If
Cells(i, GC) = x + Click
TextBox1.Text = x + Click
temp = Cells(i, GC).Value
End With
GC = GC + 1
这是一个解决即将出现的问题的环节,
更多描述
上面的屏幕截图简要介绍了这个问题。我们想要实现的是,当用户在“A”列中选择一个值时,逻辑会提供一个值,该值包含从“B”列派生的预定义值和从“000”开始的系统生成的序列号。
比如说 - 当用户从“A”列中选择 - PP 时,系统将从“B”列 - 210 获取相应的值,并且还应生成一个从“000”开始的序列号,并将该值连接起来并为用户提供“210000”。
当用户从“A”列中选择“KK”时,系统将从“B”列“103”中获取相应的值,并且还应生成一个从“000”开始的序列号,并将该值连接起来并为用户提供“103000”。
当用户再次选择“A”列 - PP 时,系统将从“B”列 - 210 获取相应值,并应生成一个从“001”开始的序列号,并将该值连接起来并为用户提供“210001”。因为序列号“000”已被使用。
答案1
将其放置在 UserForm1 模块中
Option Explicit
Private ur As Range
Private Sub CommandButton1_Click()
Dim c As Range, co As Range
Set c = getLastID
Set co = c.Offset(, -1)
If Len(co) > 0 Then c = co + 1 Else c = ur.Cells(c.Row, 2) * 1000
Me.TextBox1.Text = c
End Sub
Private Sub ComboBox1_Change()
Me.TextBox1.Text = getLastID.Offset(, -1)
End Sub
Private Function getLastID() As Range
Dim sel As String, lc As Long, cr As Long
sel = Me.ComboBox1.Text
If Len(sel) = 0 Then sel = ur.Cells(ur.Row + ur.Rows.Count, 1)
cr = Application.WorksheetFunction.Match(sel, ur.Columns(1), 0)
lc = ur.Cells(cr, ur.Column + ur.Columns.Count + 1).End(xlToLeft).Column
If lc < 5 Then lc = lc + 2
Set getLastID = ur.Cells(cr, lc + 1)
End Function
Private Sub UserForm_Initialize()
Set ur = Worksheets(1).UsedRange
End Sub
生成从 B 列 ID * 1,000 开始的序列号,或增加最后一个 ID