See the question and my original answer on StackOverflow

I had the same problem but I wanted to keep both Visual Studio 2017, Visual Studio 2019 and Visual Studio 2022 support for building the same project.

What I did is edited my .vcxproj, which is an MSBuild-format file, like this:

Before (toolset 1.42, only available with Visual Studio 2019, fails with Visual Studio 2017 and 2022):

...
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    ...
    <PlatformToolset>v142</PlatformToolset>
    ...
  </PropertyGroup>
...

After, each Visual Studio Version uses its own toolset version:

...
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
    ...
    <PlatformToolset Condition="'$(VisualStudioVersion)' == '15.0'">v141</PlatformToolset>
    <PlatformToolset Condition="'$(VisualStudioVersion)' == '16.0'">v142</PlatformToolset>
    <PlatformToolset Condition="'$(VisualStudioVersion)' == '17.0'">v143</PlatformToolset>
    ...
  </PropertyGroup>
...