Constant-weight codes (CWCs) play an important role in coding theory. The problem of determining the sizes for optimal ternary CWCs with length n, weight 4, and minimum Hamming distance 5 ((n,5,4)3 code) has been settled for all positive integers n ≤ 10 or n >; 10 and n ≡ 1 mod 3 with n ∈ {13,52,58} undetermined. In this paper, we investigate the problem of constructing optimal (n,5,4)3 codes for all lengths n with the tool of group divisible codes. We determine the size of an optimal (n,5,4)3 code for each integer n ≥ 4 leaving the lengths n ∈ {12,13,21,27,33,39,45,52} unsolved.