我在代码中遇到了一些IF语句的问题,我注意到代码没有按照它的意图执行,这是由于一个错误的IF语句。
下面的代码会检查两个值是否匹配,这两个值是 targetcolumnvalue 和 sourcecolumnvalue 。如果这两个值匹配,则excel将使用这些值作为列和行标识符,基于单元格行和列位置执行复制/粘贴。
当我调试代码时,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
答案 0 :(得分:0)
重新启动我的计算机修复此问题,似乎VBA没有正确编译。