mardi 29 décembre 2020

IF-Statements: Making VBAs codes more efficient

I'm working on a code to compare two long (+40,000 logs) worksheets with info. I need to use VBA, sadly can't use python, SQL or any other language.

I'm going over the sheets with 2 for loops as I think is the most efficient way... (Do you perhaps have a more efficient suggestion?) And inside these loops, I'm using one IF-Statement with many criteria (multiple ANDs).

For making my code faster, would you recommend keeping this statement as it is or using multiple ones with one condition each?

If ID = N_ID_IN And Cells(j, DCol) > 0 And Cells(j, Int_DCol) > Min And IsInArray(Routes, Cells(j, DCol), Num_R - 1) And (ActiveWorkbook.Sheets(DSheetName).Cells(j, Int_DCol) * (ActiveWorkbook.Sheets(DSheetName).Cells(j, DCol) - ActiveWorkbook.Sheets(ISheetName).Cells(i, ICol)) / 100) >= Min_S Then

The "IsInArray()" is a function I created to simulate the df.isin() function of Python. Do you consider is best to use this function outside the IF-Statement and store its value in a variable?

Thanks!!

Aucun commentaire:

Enregistrer un commentaire