0

How would you swap two characters in a string in Excel, with string and position to swap specified by the user?

For example:

User inputs a sentence: "Hi there!"

User inputs an integer between 1 and half of the LEN(sentence): 2

Output: "He theri!"

I am able to replace the first instance, but cannot figure out a way to replace the second.

So far I have used the REPLACE and MID funciton to accomplish what I have.

Thanks!

Pells31
  • 5
  • 1
  • 2

1 Answers1

2

Let say your initial sentence in A1 and intereg number in B1. Than you could use:

=REPLACE(REPLACE(A1,LEN(A1)-B1+1,1,MID(A1,B1,1)),B1,1,MID(A1,LEN(A1)-B1+1,1))

enter image description here

Dmitry Pavliv
  • 35,333
  • 13
  • 79
  • 80