Question occured to me while watching Pirates of the Caribbean in which they use both. Obviously that‘s not a reputable historical source but now I can‘t help wondering when we gave up on sword fighting completely.
This is a tricky question to answer, because we "gave up" on sword fighting at several different points in history, depending on where in the world you're talking about. My expertise mostly deals with European military history, so I'll focus on that.
First things first obviously, we have to determine what we mean by replacing swords with guns. Swords were very rarely, if ever, the primary military weapon of choice. Officers and nobles used them as marks of status, but the infantry that made up the bulk of military fighting units often used halberds, spears, or other similar weapons that can best be summarized as a long stick with a pointy bit on the end. They were easier to manufacture, learn, and use than swords.
Long pointy objects on sticks started to be replaced among the infantry by firearms in or around 1560-1660, in a period sometimes referred to as the European Military Revolution. This was a time of extremely rapid change in how wars were fought by European powers. To quote from an essay I wrote on the subject:
Under King Henry IV (1589-1610), France replaced the 1000-man regiment as their standard combat unit with the 400-man battalion. This innovation in French warfighting came after the lessons of the French Wars of Religion (1562-98). During the Wars of Religion, French protestants were forced to innovate military strategies. They lacked access to the royal treasury and had no standing military formations or traditions. As a result, an emphasis was placed on small-unit engagements, where the standard combat unit was a single regiment, broken into companies that stood no more than 12 ranks deep. These regiments that were pioneered by the French protestants were themselves a reduction in size from the French units that participated in the Italian Wars of 1494-1559, which regularly stood at 3,000-5,000 men. Shortly after 1600, formations were again reduced in size to be no more than 10 ranks deep, and in 1640 Marshall Turenne reduced French depth again to just 6 ranks and reduced the number of pikemen to just a third of the entire battalion.
However, France was among some of the earlier adopters of the firearm. The Spanish tercio formation remained overwhelmingly pointy-object-on-stick focused until the Battle of Rocroi in 1643 where they were soundly trounced by French combined arms (musketeers, heavy cavalry, field artillery), after which they begrudgingly began to switch to a firearm based formation.
Still though, infantry units continued to use pikemen until well after the conclusion of the 30-years war. Infantry units became wholely firearm based sometime around 1700, but that's hardly a hard and fast number. The American Revolution even saw some use of pikes, and during the Napoleonic Wars NCOs in the British army were sometimes issued Halberds to help in defense of the regimental colors. For most infantry however, the pointy-object-on-stick era came to a close with the proliferation of the socket bayonet. After all, why do you need a dedicated group of guys holding pikes or some such when you could have them shooting, and then have them put a pointy bit on their musket if they really need to.
But you wanted to know about swords! Not pikes. Swords continued to be issued to military units after the infantry abandoned pointy bits on sticks (with the exception of bayonets). The US Army in fact issued a saber to cavalry units up until 1934. Romanian cavalry had sabers as practical weapons until 1941!
This is long after swords went out of style for gentlemen to wear and duel with (the mid-1800s), and while swords are still part of an Officer's dress uniform in militaries around the world, they're dress swords, not practical killing weapons.
If I had to put a hard date on it, I would say that the western world gave up on swordsmanship around the turn of the 20th century. Dueling had long been outlawed, and while there was a half-hearted attempt to keep the tradition alive within cavalry units, they stopped being issued as practical weapons by any military I am familiar with in the early years of WW2.
So to recap:
Primarily bladed weapons such as pikes, halberds, etc, stopped being used by infantry en masse by the mid-18th century, and had really stopped being used by any serious military by the 19th.
Swords continued in various civilian circles until the mid-19th century, but were gone by the turn of the 20th.
Cavalry units continued to use swords until the Second World War.
Sources:
Evans, M. and Ryan, A. (2003). From Breitenfeld to Baghdad Perspectives on Combined Arms Warfare. Land Warfare Studies Center
Fowler, Jeffrey T., and Mike Chappell. (2001). Axis Cavalry in World War II. Osprey.
Loades, Mike, and Adam Des Forges. (2017). Swords and Swordsmen. Pen & Sword Military.
López, Ignacio Notario, and Iván Notario López. (2012). The Spanish Tercios 1536-1704. Oxford: Osprey.
Lynn, J. (1985). Tactical Evolution in the French Army, 1560-1660. French Historical Studies
Parrott, David. (1992). The Military Revolution in Early Modern Europe. History Today 21.
Roberts, M. (1967). Essays in Swedish History. Minneapolis: University of Minnesota Press.
Edit: formatting