IF-THEN w /中的字符串

时间:2015-08-11 13:51:01

标签: excel vba if-statement for-loop

我试图通过用字符串读取值来检查两个单元格值是否相等。使用for循环逐步查找IF-THEN语句的匹配项。

当单步执行程序时它显示值在一点上相等,但IF-THEN比较只是继续运行我无法弄清楚为什么它们在它们相等时不会停止。

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

1 个答案:

答案 0 :(得分:3)

如何'相同'是两个字符串?一个是资本化而另一个不是吗?有一个领先/尾随零?是否有不可打印的字符?

尝试这个,在比较它们之前清除每个术语:

If AllCleanedUp(Check) = AllCleanedUp(Val) Then

...

其中涉及以下功能:

Function AllCleanedUp (DirtyString byVal) As String

    AllCleanedUp = Trim(Application.Clean(Lcase(DirtyString)))

End Function