Script divide number into equals
I'm helping a friend to make some labels and I'm having trouble.
I need to divide an amount of items into groups of 100. In the example, we have the amount 350 divided into 4 tags
100 + 100 + 100 + 50
The quantity of labels is calculated with this single line code:
Format.Objects("QTY").Value/Format.Objects("nGroups").Value
I used a script I found here on the Forum to divide the number, but it didn't solve my problem:
vResult = Format.Objects("nGroups").Value
IF Format.Objects("QTY").Value MOD Format.Objects("nGroups").Value = 0 THEN
Value = Format.Objects("QTY").Value / vResult
ELSE
IF CInt(Format.Objects("nGroups").Value) < CInt(vResult) THEN
Value =(Format.Objects("QTY").Value / (vResult - 1))
Else
Value = Format.Objects("QTY").Value - ((vResult - 1) * Int(Format.Objects("QTY").Value / (vResult - 1)))
End If
End If
The example file is here:
https://we.tl/t-DurrFYGvln
Thanks for reading and any help would be really appreciate.
-
Peter Thane
★ BarTender Hero ★
There is probably a better way to achieve the number of labels to print but the code below works. You need to make the Copies field a VB Script>Event Controlled Script and edit the OnIdenticalCopies to include the code below.
I called my field, that has the total number in it, "TheNumber" so I could reference it in the VB.
Value = Format.NamedSubStrings("TheNumber").Value
Value1 = Value/100
Value2 = Right(Value,2)
If Value2 > 0 then
Value2 = 1
else Value2 = 0
end if
Value1 = Int(Value1)
Value = Value1 + Value2
1 -
Jose R
★ BarTender Hero ★
Thanks for the input, Peter!
I still couldn't divide the amount into the 4 labels. 350 = 100 + 100 + 100 + 50. My code returns 53 in all of them.
I created a regular text object called nResult and I place my code the "OnSerialize" event in the Event List, is it correct or is there some other way?
Thanks in advance!
0 -
Peter Thane
★ BarTender Hero ★
Sorry I thought you were just after printing the correct number of labels. For the fields:
- Add a sequential number field that resets every print job and give this field a name (I used "CounterUp"). This field can be dragged off the side of the label so it is not in the printable area.
- On the File Print Screen amend the Number of Identical Copies to use the Print Dialog option
- For the Serial Numbers add the VB script used above into this field instead but make set it to run OnSerialise and not OnIndenticalCopies. Name this Data Source too, I called this LblQty
- Next amend your 100/50 field and make this a VBScript > EventControlled > OnIdenticalCopies field and add the VB below into this.
Value = Format.NamedSubStrings("LblQty").Value
Value1 = Format.NamedSubStrings("CounterUp").Value
Value2 = Format.NamedSubStrings("TheNumber").Value
If Value = 1 thenValue = 100
elseif Value = Value1 then
Value = Right(Value2,2)
else Value = 100
end if
Value = Value
This should then give you something like this:
1 -
Jose R
★ BarTender Hero ★
Good morning, Peter!
Thanks a lot for the help. That's perfect. Just what I needed.
0
Please sign in to leave a comment.
Comments
4 comments