Excel VBA如果不满足条件后执行

我在代码中遇到了一些IF语句的问题,我注意到代码并没有按照它的意思去做,而是由于错误的IF语句。

下面的代码检查两个值是否匹配,这两个值是targetcolumnvaluesourcecolumnvalue 。 如果这两个值匹配,excel将使用这些值作为列和行标识符,根据单元格行和列位置执行复制/粘贴。

当我debugging代码时,targetcolumnvalue等于26,并且sourcecolumn值等于“”,但是代码在if语句被满足的情况下执行。 我在这里没有看到什么吗?

Option Compare Text Option Explicit Sub BuggyIf() Dim SourceColumnValue As String, sourcerow As String, targetrow As String, targetcolumnvalue As String, sourcecolumnnumber As String Dim F as long, O As Long, P As Long, TargetValue As Long, actualsourcerow As Long, actualtargetrow As Long, actualtargetcolumn As Long, sourcedateposition As Long, actualsourcecolumn As Long, targetdateposition As Long, actualtargetforecastrow As Long Dim Copysource As Range, pastetarget As Range TargetValue = dumpsheet.Cells(rows.Count, 1).End(xlUp).row sourcedateposition = dumpsheet.Cells(rows.Count, 5).End(xlUp).row targetdateposition = dumpsheet.Cells(rows.Count, 7).End(xlUp).row For F = 1 To sourcedateposition SourceColumnValue = dumpsheet.Cells(F, 5).Value 'Get Target Column Match to Source For P = 1 To targetdateposition targetcolumnvalue = dumpsheet.Cells(P, 7).Value ' BUGGY IF STATEMENT If targetcolumnvalue = SourceColumnValue Then TargetColumnRange.Value = SourceColumnValue targetcolumnvalue = dumpsheet.Cells(P, 8).Value sourcecolumnnumber = dumpsheet.Cells(F, 6).Value For O = 1 To dumpsheet.Cells(rows.Count, "a").End(xlUp).row If O > 1 Then Sourcename = dumpsheet.Cells(O, 1).Value sourcerow = dumpsheet.Cells(O, 2).Value targetrow = dumpsheet.Cells(O, 3).Value dailyrate = dumpsheet.Cells(O, 4).Value 'Set Integers actualsourcerow = CInt(sourcerow) actualtargetrow = CInt(targetrow) actualtargetcolumn = CInt(targetcolumnvalue) actualsourcecolumn = CInt(sourcecolumnnumber) actualtargetforecastrow = actualtargetrow - 521 dailyfte = CInt(dailyrate) 'Copy and Paste Set Copysource = SourceSheet.Cells(actualsourcerow, actualsourcecolumn) Set pastetarget = TargetSheet.Cells(actualtargetrow, actualtargetcolumn) If pastetarget.Cells.Interior.Color <> 1 Then Copysource.Copy pastetarget.PasteSpecial (xlPasteValues) Set Copysource = TargetSheet.Cells(actualtargetrow, actualtargetcolumn) Set pastetarget = TargetSheet.Cells(actualtargetforecastrow, actualtargetcolumn) Copysource.Copy pastetarget.PasteSpecial (xlPasteValues) fte = TargetSheet.Cells(actualtargetforecastrow, actualtargetcolumn).Value / dailyfte TargetSheet.Cells(actualtargetforecastrow, actualtargetcolumn).Value = fte End If End If Next O End If Next P Next F .... End Sub 

重新启动我的电脑解决了这个问题,好像VBA没有正确编译。