jeudi 5 août 2021

What is the best way to use the IF function in VBA?

I have a spreadsheet that is 166K rows and i need to write an IF function to fill in a new column. I've tried 2 ways, one doesn't work and the other does but it takes forever to run. Any tips?

First attempt that didnt work:

lngLastRow = Cells(Rows.Count, "E").End(xlUp).Row

Range("S2").Formula = "=IF(Q2> DateValue("7/31/2021") then S2 = R2+30)"
Range("S2").Copy Range("S3:S" & lngLastRow)

Second attempt that worked but took forever to run:

For i = 2 To report.UsedRange.Rows.Coun

 If report.Cells(i, 17) > "7/31/2020" Then
  report.Cells(i, 19) = report.Cells(i, 18) + 30
 Else
  report.Cells(i, 19) = report.Cells(i, 18) 
 End If

 Next i

Lastly it actually looks like its not adding 30 to column 18 which is a date. I would like it to add 30 days to the date if the statement is true.

Aucun commentaire:

Enregistrer un commentaire