I am having some troubles with an IF statement in my code, I noticed the code was not doing what it was ment to, and it is due to a buggy IF statement.
The code below checks if two values match, these two values are targetcolumnvalue and sourcecolumnvalue. If these two values match, excel will perform a copy/paste based on cell row and column positions using these values as the column and row identifiers.
When I debugged the code, the targetcolumnvalue was equal to 26, and the sourcecolumn value was equal to "", but the code was executing on the scenario that the if statement was being met. Am I not seeing something here?
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
Aucun commentaire:
Enregistrer un commentaire