Hexamethylenetetramine (HA) is widely used as a raw material in the medical, chemical, industrial, and military industries, and the fast and quantitative analysis of HA is important for manufacturing processes in these fields. Owing to its efficiency, low cost, nondestructive testing, and convenience, near infrared (NIR) spectroscopy is a powerful technique for quantitatively analyzing the HA concentration in HA–acetic acid (HAc) solutions, demonstrating application potential in the production of hexogen and octogen. A series of preprocessing algorithms and variable selection methods were studied to improve the detection accuracy of the NIR spectroscopic calibration. Forty-six different combinations of standard normal variation (SNV), multiplicative signal correction, first derivative, second derivative, and discrete wavelet transform (DWT) were screened. The effects of four variable selection methods (successive projection algorithm (SPA), uninformed variable elimination, competitive adaptive reweighted sampling, and multiverse optimization (MVO)) were compared. Finally, a model (SPXY-SNV-1stDer-DWT-MVO-RF) was developed by combining sample set portioning based on the joint x–y distance (SPXY) algorithm with the random forest (RF) calibration model, and MVO was combined with the NIR technique for the first time. The model achieved a coefficient of determination for the calibration set (R2), root mean square error of the calibration set (RMSEC), coefficient of determination for the prediction set (r2), and root mean square error of the prediction set (RMSEP) of 1.000, 0.04%, 0.999, and 0.05%, respectively. This study demonstrated the novelty and feasibility of HA quantitative detection by NIR spectroscopy and provided valuable insights for optimizing quantitative analysis models by optimizing algorithms, indicating the great application potential of NIR spectroscopy in related fields.