Yes. Make a variable called loopCounter (or whatever) and set up logic like this.
Set loopCounter = number of berries to be added to stack (in this case 5)
Set up a While loop:
While loopCounter > 0:
- sub event: if berry stack in inventory = 10, create a new stack of 1, -1 from loopCounter.
- sub event: if berry stack in inventory is < 10, +1 to stack, -1 from loopCounter.
The script should loop through until it runs out of berries to try to add to the stack. But do note that this is off the top of my head and hasn't been tested.