excel vba中的StrComp函数给出了错误的结果。

时间:2017-06-11 18:46:06

标签: excel-vba vba excel

我是根据某些条件将数据复制到单元格。

If (StrComp(Trim(sh333.Cells(i333 + 1, jHistory).Value), Trim(sh333.Cells(i333 + 2, jHistory).Value), vbTextCompare)) = 0 Then
            MsgBox "history Comments and Last comments are Same / Equal"
            sh44.Cells(counterFinalHistory, 7).Value = Trim(sh333.Cells(i333 + 1, jHistory).Value) '& vbNewLine & sh333.Cells(i333 + 2, 3).Value & ":" & sh333.Cells(i333 + 2, jHistory).Value & vbNewLine & sh333.Cells(i333 + 5, jHistory).Value & ":" & sh333.Cells(i333 + 4, jHistory).Value 
            sh44.Cells(counterFinalHistory, 8).Value = Trim(sh333.Cells(i333 + 2, jHistory).Value & vbNewLine & sh333.Cells(i333 + 5, jHistory).Value & ":" & sh333.Cells(i333 + 4, jHistory).Value)  
        Else
            MsgBox "history Comments and Last comments are Not Same / Different"
            sh44.Cells(counterFinalHistory, 7).Value = Trim(sh333.Cells(i333 + 1, jHistory).Value & vbNewLine & sh333.Cells(i333 + 2, 3).Value & ":" & sh333.Cells(i333 + 2, jHistory).Value & vbNewLine & sh333.Cells(i333 + 5, jHistory).Value & ":" & sh333.Cells(i333 + 4, jHistory).Value) 
            sh44.Cells(counterFinalHistory, 8).Value = Trim(sh333.Cells(i333 + 2, 3).Value & ":" & sh333.Cells(i333 + 2, jHistory).Value & vbNewLine & sh333.Cells(i333 + 5, jHistory).Value & ":" & sh333.Cells(i333 + 4, jHistory).Value)  
        End If

这里有两个令人不安的趋势:

1)2个单元格中的字符串sh333.Cells(i333 + 1,jHistory).Value和sh333.Cells(i333 + 2,jHistory).Value完全相同仍然StrComp在Else部分进行并且说“历史评论和最后评论不相同/不同“

2)当我将sh333.Cells(i333 + 1,jHistory)的内容复制到sh333.Cells(i333 + 2,jHistory)时,StrComp表现正确并说“历史评论和最后评论相同/等于“

请让我知道VBA的StrComp功能的这种不稳定行为的原因。我已经打破了我的头脑,但没有发现任何纠正行为。请帮忙并建议。

0 个答案:

没有答案