Swapping text between 2 text boxes

glad_ir

Board Regular
Joined
Nov 22, 2020
Messages
143
Office Version
  1. 2010
Platform
  1. Windows
Hello,

Wondering if somebody could advise how to exchange text between 2 text boxes with VBA please. I have no idea where to start.

I have 2 boxes in my sheet - Text Box 1 contains the text "Series 1" and Text box 2 contains the text "Series 2". I need a macro that will convert this to Text Box 1 contains the text "Series1" and Text Box 2 contains the text "Series 2". I have another macro in my sheet that references the Text Box numbers so I think it is the text in the text boxes that needs to be switched rather than the text boxes names.

Any help with this would be much appreciated!

Thanks,
Iain
 
You can put your file on a free file sharing site (the only one I trust is DropBox though), mark it for sharing and then post the link they give you... that will allow us to download your file is look at what is going on.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
It's hard to do this in bits and pieces. Please show all your code.
 
Upvote 0
Hi, I think I've uploaded it.....can you access this?

macrobook1.xlsm

I've also included the macro that swaps the ranges coloured yellow and blue which I'm running off the double headed arrow. This works fine but the title in the text boxes doesn't change.
 
Upvote 0
Your button (the two-headed arrow) is calling a Sub in Module2 called Switch. The code I provided is in Module1 in a sub called swaptext that is never called anywhere. I think you probably want to take the code that is in swaptext and copy it into Switch...?
 
Upvote 0
Hi, great - you can access it....never used dropbox before!

I haven't associated it with the double headed arrow yet but the plan is to run both macros off it or combine swaptext and Switch into a single macro......however swaptext doesn't work me . Does it work for you?
 
Last edited:
Upvote 0
You actually have two problems with your code. The first was identified by 6StringJazzer, namely, you never call the swaptext macro. Add this line of code to the bottom of your Switch subroutine...
VBA Code:
Call swaptext

Now, your second problem is in the coding of swaptext... VBA thinks your TextBox1 and TextBox2 are local variables, not textbox objects. You have to reference the sheet the textboxes are on to get VBA to see them as objects. Change your swaptext code to this and add what I mentioned above and all should work as you wanted...
VBA Code:
Sub swaptext()
  Dim Temp As String
  With Sheets("Sheet1")
    Temp = .TextBox1
    .TextBox1 = .TextBox2
    .TextBox2 = Temp
  End With
End Sub
 
Upvote 0
Solution
Bingo!! Thank you both so much for your help with this and also for the explanation - I really appreciate your time and help. Thank you :):)
 
Upvote 0

Forum statistics

Threads
1,214,412
Messages
6,119,365
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top