我是VBA的新手。我有一个使用Excel的条形码扫描器填充的文本框。表单上有不同的值,例如:
608001F
608001
001IN
我需要检查代码是否包含'F'然后执行某个操作,否则执行其他操作。我在Textbox_Change()
函数上执行此操作,这会导致首先触发608001
代码。如何让它首先触发代码中的'F'?
我的代码看起来像这样(为简洁而编辑,所以如果有任何语法错误,请原谅):
Private Sub TextBox1_Change()
Value = TextBox1.Value
If Value <> "" Then
If (Len(Value) = 7 And Right(Value, 1) = "F") Then
ActiveCell.Value = Value
ActiveSheet.Cells(ActiveCell.Row + 1, 1).Select
TextBox1.Value = ""
TextBox1.Activate
ElseIf (Len(Value) = 6 Or (Len(Value) = 5 And (Right(Value, 2) = "IN" Or Right(Value, 2) = "EM"))) Then
ActiveCell.Value = Value
Selection.Offset(0, 1).Select
TextBox1.Value = ""
TextBox1.Activate
Else
'Do nothing
End If
End Sub
修改
我决定更清楚地说明发生了什么。当使用_Change()函数时,首先满足字符6到达时的条件,这意味着代码被视为608001而不是608001F。
答案 0 :(得分:0)
您可以使用<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.mohamedreda.note.Delete">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
</RelativeLayout>
函数查找字符串中是否存在字符,如果字符不存在,则函数返回0。然后只需检查返回值是否大于0.
这个简单的例子可以给你一个线索。
InStr()
答案 1 :(得分:0)
Private m_previousBarcodeString As String
Private m_isFirstBarcodeEntered As Boolean
Private Sub UserForm_Initialize()
m_isFirstBarcodeEntered = True
' Some test data only. You don't need to use the rest of this
' method code below as you have a barcode scanner.
Dim barcodesArray(2) As String
barcodesArray(0) = "608001F"
barcodesArray(1) = "608001"
barcodesArray(2) = "001IN"
Dim barcodeIndex As Integer
Dim barcodeCount As Long
Dim charIndex As Integer
Dim charCount As Integer
barcodeCount = UBound(barcodesArray)
For barcodeIndex = 0 To barcodeCount
charCount = Len(barcodesArray(barcodeIndex))
For charIndex = 1 To charCount
TextBox1.Text = TextBox1.Text & Mid(barcodesArray(barcodeIndex), charIndex, 1)
Next charIndex
TextBox1.Text = ""
Next barcodeIndex
End Sub
Private Sub TextBox1_Change()
If (m_isFirstBarcodeEntered = True) Then
m_isFirstBarcodeEntered = False
Else
If Len(TextBox1.Text) = 0 Then
MsgBox "Was '" & m_previousBarcodeString & "' ending in 'F'?: " & _
UCase(isValidBarcode(m_previousBarcodeString))
Else
m_previousBarcodeString = TextBox1.Text
End If
End If
End Sub
Private Function isValidBarcode(ByVal singleBarcodesString As String) As Boolean
isValidBarcode = False
If Len(singleBarcodesString) = 7 Then
If UCase(Right(singleBarcodesString, 1)) = "F" Then
isValidBarcode = True
End If
End If
End Function
答案 2 :(得分:0)
如果我理解了您的问题,那么您似乎不希望Change
事件被触发,直到您没有给出某种确认来说明&#34;我完成插入值&#34; 。
在这种情况下,我可能建议使用" "
分隔符编写代码(即,在您不按SpaceBar
确认插入成功之前,没有任何反应)。为了实现这一目标,您只需添加两行新代码(参见下面的编辑):
Private Sub TextBox1_Change()
Value = TextBox1.Value
If Value <> "" Then
If Right(Value,1) = " " Then '<-- new line
If (Len(Value) = 7 And Right(Value, 1) = "F") Then
ActiveCell.Value = Value
ActiveSheet.Cells(ActiveCell.Row + 1, 1).Select
TextBox1.Value = ""
TextBox1.Activate
ElseIf (Len(Value) = 6 Or (Len(Value) = 5 And (Right(Value, 2) = "IN" Or Right(Value, 2) = "EM"))) Then
ActiveCell.Value = Value
Selection.Offset(0, 1).Select
TextBox1.Value = ""
TextBox1.Activate
Else
'Do nothing
End If
End If '<-- new line
End If
End Sub
就像这样,您的代码将如下工作:
1)你写608001
...
之前: Len() = 6
条件已触发,因此您没有时间最终输入F
;
现在:等待用户确认
2)你写608001F
...
之前: Len() = 7
会被触发,但您没有时间输入最后一个字符F
;
现在:等待用户确认
3)按SpaceBar
- &gt;代码开始运行,您不会再将6 char
字符串与7 char
字符串混淆。请注意,按SpaceBar
表示&#34;我提交条形码&#34;。这意味着,您可以将SpaceBar字符替换为任何其他Chr()
集合(Enter
,数字,字母等)。