Ignoring support so losers using cannons and such, when did your average infantry soldier get switched from a sword to a musket?
tl; dr - There is no clean, easy date but let's say "battles post-1650".
The following answer is provided focusing on European/Western militaries.
The introduction of gunpowder happened gradually in conjunction with the professionalisation of armies, the codification of making war, and the development of metallurgy and firearm manufacture. Introduced to Europe in the 13th Century by either/both Mongols fighting in Hungary and Arabs fighting in Iberia, these new weapons grew in popularity. The Battle of Crecy, 1341, was the first time two European armies used (primitive) cannons in a field battle. At this point, gunpowder weapons were used most successfully as siege weapons rather than handheld, personal weapons.
Handcannons were initially used in conjunction with close combat formations and bows/crossbows. Metallurgy developed during the 14th and 15th Centuries to the point that it became more economical to arm more and more soldiers with gunpowder weapons. The use of early firearms was more of a shotgun approach, one that was devastingly effective at breaking up enemy formations but yet required scant training. Still, the handcannon was cumbersome weapon and significant support was needed.
The Spanish "tercio" was introduced in the opening decades of the 16th Century to not only challenge the dominance of the French armoured cavalry but also to establish a structured, professional army based around combined arms. The tercio used a mishmash of pikes and firearms in discrete units; these units could at once be self-sustaining and mutually supportive, able to fight off both cavalry and unsupported foot soldiers. The tercio, in a variety of formats and evolving during the period, dominated infantry combat for the remainder of the 16th Century and was not truly challenged until a newer style - the "linear style" - was developed in the Netherlands and Sweden. The tercio faded in popularity throughout the 17th Century, the Thirty Years' War showed the tercio to be outdated if still remarkably effective a century after its inception.
During the War of Spanish Succession (1701-1714), the Spanish first line tercios were reorginised into regiments of linear infantry and dispensed with the pike. The tercio's success was down to the use of the pike as much as the matchlock gun. Able to keep cavalry at bay and presenting an unattractive option for direct infatry assault, they were formidable opponents. They were, however, big square targets for artillery. With improvements in metal casting and a more widespread understanding of the mathematics of ballistics, the cannon became increasingly effective as a deadly anti-tercio field gun. Linear infantry, being set up in a much thinner straight line, were harder to hit with artillery and took much less damage when they were hit.
The bayonet, basically a small stabbing sword attached to the front of a gun, was introduced into French armies during the 17th Century. It spread quickly. Cavalry was at the same time discarding most of their heavy arnour, in preference for being faster and harder to hit with a gun, making the anti-cavalry pike less important. Manufacture of firearms and gunpowder had become state monopolised industries with huge amounts of effort being put into making weapons for increasingly large armies. Logistics, previously a secondary consideration for armies, grew in importance, facilitating larger quantities of arms and ammunition being transported to armies in the field. The flintlock musket gave the average infantryman greater reliability and rate of fire. The pike was not only redundant in 1700, it was a liability.
Yet still, the infantry were expected to exploit tactical advantages with a charge of "cold steel". Melee combat did not stop in the 18th Century. Formations of infantry would march up to each other, fire their guns and attempt to rout the enemy, or kill them as they stood, with hand-to-hand combat. More often than not, the stress of being shot at (albeit very inaccurately) would demand a discretionary retreat rather than the charge itself. Training in hand-to-hand combat persisted, especially in reaction to increasingly aggressive cavalry tactics. It was a point of pride for some armies that they were so well-trained and disciplined as to make The Charge an effective tactic. Grenadiers were chosen as the largest, strongest, and most imposing fighting men and were expected to meet the enemy face-to-face.
Technology and training moved on once again. The Minie Ball solved a key issue with rifle tactics - rifles are very slow to muzzle load due to the tight fit of bullet and barrel - and increasingly accurate firearms became standard issue in most European armies. The rifle moved from being the domain of sharpshooters and specialist troops to being the mainstay of armies previously equipped with muskets. The American Civil War is notable in many regards, not least the growth in highly effective short range weapons (the revolver pistol) and the famous bayonet charge at Gettysburg. Despite firearms being more accurate, more deadly, and more rapidly firing, close combat was not unheard of.
The First World War was fought using mostly indirect weapons. Gas, artillery, bombs, and machineguns killed millions of men typically without combatants ever seeing each other. When men "went over the top" and attempted to wrest control of a trench, it was a very different matter. Whilst grenades and pistols had their uses (and shotguns too later in the war), the most effective weapon for attacking and defending a trench was a short spade. Issued as entrenching tools, the spade was small enough, strong enough, and sharp enough to completely outclass the unweildy bayonet and the impractical long rifle as a close combat weapon.
The truth is that regular soldiers are still armed with bayonets and knives today - the sword might have changed format but it's never gone away. Where you want to draw the line is up to you. Most people would probably opt for the end of the Thirty Years' War as being the death of the dedicated standard issue melee weapon - there were so many developments in such a short period of time that it becomes "tidy" to draw the line there.
As the knife and bayonet continued (and arguably continues) to be an important weapon, I'm going to assume you mean to ask when all combat soldiers began using a ranged weapon as the primary weapon in combat with close-quarter weapon as support.
In that case please narrow down your geographic area.