Use a global to keep track of your created enemies.
Const maxsprites% = 40
Global TotalEnemies%
If TimeToSpawn = True Then
; Generate a random number of enemies
tmp = Rnd(1,5)
; Check to see if these enemies can be added (current number of enemies + the ones to be added must be lower than 40)
If (TotalEnemies + tmp) <= maxsprites Then
; Spawn the enemies
SpawnSprites(tmp)
; Add the new number of enemies to the current number of enemies
TotalEnemies = TotalEnemies + tmp
Else
; The generated number of enemies cannot be added (the total number would be bigger than 40)
If TotalEnemies < maxsprites Then
; Calculate the remaining number of enemies that can be spawned (until 40)
tmp = maxsprites - TotalEnemies
; Spawn them
SpawnSprites(tmp)
; Add them to the total number of enemies
TotalEnemies = TotalEnemies + tmp
EndIf
EndIf
EndIf
I haven't tested this, but it should work.
This code should spawn enemies until the maximum of 40 is reached.
Afterwards, when you kill an enemy, you should decrease TotalEnemies with 1, so the code could generate a new enemy to replace the one you just killed.