我有一个Userform,它将显示在E列中输入的最后一个值。
这很好,但是我希望userform显示最后一个值增加1,从而使用户不必自己更改值。
E.g。
" MSA 00-00-0001" < - 这是显示的,因为这是列中的最后一个值 但是,我希望userform显示MSA 00-00-0002
通常你会使用.Value + 1我假设,但由于连字符和值字段中的文字,这不会起作用。
答案 0 :(得分:3)
假设您将当前值加载到msa_string
,您可以使用:
newvalue="MSA " & format(1+val(replace(right(msa_string,10),"-","")),"00-00-0000")
如果它始终不是MSA_
,那么你可以这样做:
newvalue=left(msa_string,len(msa_string)-10) & format(1+val(replace(right(msa_string,10),"-","")),"00-00-0000")
使用您的方法,将msa_string声明为包含其他声明的字符串,然后:
msa_string=Worksheets("Temp").Range("E1").End(xlDown).Value
critCode.Value = left(msa_string,len(msa_string)-10) & format(1+val(replace(right(msa_string,10),"-","")),"00-00-0000")
答案 1 :(得分:0)
你可以"作弊"一点点,使用AutoFill
。
我们假设您在Cell B1中有"MSA 00-00-0001"
,因此您可以使用AutoFill
一个单元格,而单元格B2中的值将为"MSA 00-00-0002"
。现在您可以将TextBox1.value
设置为Cell B2,之后只需清除其中的内容。
<强>代码强>
Dim Rng As Range
Set Rng = Range("B1")
Rng.AutoFill Destination:=Range(Rng, Rng.Offset(1)) '<-- use AutoFill once cell down
UserForm1.TextBox1.value = Rng.Offset(1).value
Rng.Offset(1).ClearContents ' <-- clear the contents of the added cell