I have multiple worksheets in a workbook. I have data in columns I and K starting at row 3. I want to say that if I is greater than K mark “TRUE” in column P. If I is less than K mark “FALSE” in column P.
I then want to say that if P3 is TRUE and P4 is FALSE, then mark a “1” in Q for that row. If P4 is FALSE and P4 is TRUE, then mark a “2” in Q for that row. If P3 is TRUE and P4 is TRUE, ignore it, and if P3 is FALSE, and P4 is FALSE, ignore it also. Then carry on through the worksheet, for P4 comparing to P5 etc.
Then, I want for any lines that have a 1 or a 2 in them, for those lines to be copied, and taken across to a worksheet called Analysis, and pasted in the next available row. I then want to loop through to the next worksheet and do it all again, until it has been done on all worksheets. I haven't written any code for this bit yet, as I can't get the first bit to work.
The code I have tried is this.
Dim lr As Long
Dim pr As Long
Dim i As Long
Dim cval As Variant
Dim pval As Variant
lr = ActiveSheet.Cells(Rows.Count, "K").End(xlUp).Row
ActiveSheet.Range("P3:P" & lr).ClearContents
pval = ""
ActiveSheet.Range("P3").Select
ActiveCell.FormulaR1C1 = "=RC[-7]>RC[-5]"
Range("P3").Select
Selection.Copy
Range("P4:P195").Select
ActiveSheet.Paste
For i = 3 To lr
'note that cval is current value - i.e. for us this would be P4,
' because previous value is P3 - it checks the current value
' against the previous value to tell if they are the same.
cval = ActiveSheet.Range("P" & i).Value
pval = ActiveSheet.Range("P" & i + 1).Value
If cval <> "" Then
If cval = "FALSE" And pval = "FALSE" Then ActiveSheet.Range("Q").Value = ""
ElseIf cval = "FALSE" And pval = "TRUE" Then ActiveSheet.Range("Q").Value = "1"
ElseIf cval = "TRUE" And pval = "TRUE" Then ActiveSheet.Range("Q").Value = ""
ElseIf cval = "TRUE" And pval = "FALSE" Then ActiveSheet.Range("Q").Value = "2"
End If
pval = cval
pr = i
End If
Next i
Any help would be greatly appreciated! Thanks!
question from:https://stackoverflow.com/questions/66056423/vba-cval-and-pval