Which way is faster? If elseif or select case

前端 未结 6 1276
陌清茗
陌清茗 2020-12-20 14:13

For the following code,

If Sheets(\"sheet1\").Range(\"A1\").Value = \"option_1\" Then
    Sheets(\"sheet1\").Range(\"A1\").Value = \"option_2\"
ElseIf Sheets         


        
6条回答
  •  时光取名叫无心
    2020-12-20 14:19

    A bit too late, but in the specific example the fastest should be to store the option as a number and just increment it when needed. The Custom Number Format of the cell can be changed to "option_"0;;; for the number to show as option_#.

    In almost all cases I expect the Select Case to be a just a tiny bit slower and to be compiled to something very similar to If Else statements.

    That is not the case in the two examples because they do slightly different things. In the first example, each of the If statements will look for Sheet "sheet1" and get the value of Range "A1", but the Select Case example gets that value only once in the beginning and then compares that value. This should cause the Select Case example to be few times faster when the cell value is not "option_1".

提交回复
热议问题