IF-THEN w / VBA中的字符串 [英] IF-THEN w/ strings in VBA
问题描述
我试图通过读取字符串值来检查两个单元格值是否相等。使用for循环逐步查找IF-THEN语句的匹配。
当步过程序时,它显示值在一点上相等,但IF-THEN比较只是持续运行我无法弄清楚为什么它不会停止他们是平等的
Dim ws As Worksheet
Dim lcol As Long
Dim Val As String
Dim Check作为字符串
'读取价值审计继续
Val = ContTextBox.Value
'使主表活动
工作表( Master)激活
设置ws = ThisWorkbook.Sheets(Master)
'查找下一个空列
使用ws
lcol = .Cells(11,.Columns.Count).End(xlToLeft).Column + 1
End with
'我需要设置为最小限制
'开始搜索循环
对于j = 1 To lcol
检查=单元格(11,j).Value
如果Check = Val然后
卸载我
Assetlookup.Show
结束如果
下一步j
卸载这两个字符串的相同是怎样的?一个是资本化的,另一个不是?有一个前导/尾随零?有一个不可打印的字符?
尝试这个,在比较它们之前清理每个术语:
如果AllCleanedUp(Check)= AllCleanedUp(Val)然后
这是指以下功能:
功能AllCleanedUp DirtyString byVal)As String
AllCleanedUp = Trim(Application.Clean(Lcase(DirtyString)))
结束函数
pre>
I am trying to check if two cells values are equal to each other by reading in the value with strings. Using a for loop to step through to find the match of the IF-THEN statement.
When stepping through the program It shows that the values are equal at one point but the IF-THEN comparison just keeps running I can not figure out why it will not stop when they are equal.
Dim ws As Worksheet
Dim lcol As Long
Dim Val As String
Dim Check As String
'Read in value for audit to continue
Val = ContTextBox.Value
'Make Master Sheet Active
Worksheets("Master").Activate
Set ws = ThisWorkbook.Sheets("Master")
'Finds next empty column
With ws
lcol = .Cells(11, .Columns.Count).End(xlToLeft).Column + 1
End With
'i needs to be set to minimum limit
'Begin loop of search
For j = 1 To lcol
Check = Cells(11, j).Value
If Check = Val Then
Unload Me
Assetlookup.Show
End If
Next j
Unload Me
解决方案 How 'identical' are the two strings? Is one capitalized and the other not? Does one have leading/trailing zeros? Does one have non-printable characters?
Try this, which cleans each term before comparing them:
If AllCleanedUp(Check) = AllCleanedUp(Val) Then
...
Which refers to the following function:
Function AllCleanedUp (DirtyString byVal) As String
AllCleanedUp = Trim(Application.Clean(Lcase(DirtyString)))
End Function
这篇关于IF-THEN w / VBA中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!