How do you make a macro dynamic?

justanotheruser

Board Regular
Joined
Aug 14, 2010
Messages
96
Hi guys,

I've got the following macro:

Code:
Sub UpdateDelivery()
Sheets("Del1").Shapes("TextBox 2").TextFrame.Characters.Text = Sheets("VAT").Shapes("TextBox 1").TextFrame.Characters.Text
Sheets("Del2").Shapes("TextBox 2").TextFrame.Characters.Text = Sheets("VAT & Disc").Shapes("TextBox 1").TextFrame.Characters.Text
Sheets("Del3").Shapes("TextBox 2").TextFrame.Characters.Text = Sheets("Zero VAT").Shapes("TextBox 1").TextFrame.Characters.Text
Sheets("Del4").Shapes("TextBox 2").TextFrame.Characters.Text = Sheets("Zero VAT & Disc").Shapes("TextBox 1").TextFrame.Characters.Text
End Sub


Which basically when executed, from the first line of code copies the contents of TextBox1 in the VAT sheet to TextBox 2 on the Del 1 sheet. This applies for the other 3 lines for the named sheets too. Currently, to run the macro this has to be done by executing it, but is it possible to make it work dynamically or perhaps not even need a macro at all if I can make TextBox 2 = TextBox 1 somehow? I've heard about something called Private Subs - but I don't know if that's what I need?

Thanks in advance! :)
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I clicked on TextBox1 in the VAT sheet and defined a name for it - called it VATTextBox and it refers to <CODE>="TextBox 1"</CODE>. However, if I go to TextBox 2 in the Del1 sheet and go to the formula bar and type =VATTextBox, I get "Reference is not valid" error - the scope of the VATTextBox is the entire workbook.
 
Upvote 0
If I put <CODE>=VATTextBox</CODE> into a cell, it prints out TextBox 1 - so how do I define the name to refer to the actual shape, instead of the name of the shape? Thanks again! :)
 
Upvote 0
I've done some googling, I can't find what formula you can use in a shape to make it equal another shape - so if anyone could help make the code in my first post work dynamically, I'd really appreciate it! :)
 
Upvote 0
If you use textboxes from the Control Toolbox (i.e. ActiveX textboxes), you can make TextBox1 on Sheet1 automatically change to the contents of TextBox1 on Sheet2 by putting the following code in the Sheet2 module:
Code:
Private Sub TextBox1_Change()
    Sheets("Sheet1").TextBox1.Text = TextBox1.Text
End Sub
See if you can adapt this for your situation.
 
Upvote 0
Thank you for that code John, works perfectly! Is there any way I can reshape the active x text box to make it a rounded edge rectangle with a black border?
 
Upvote 0
Yep, I've been doing that - unfortunately it seems to mean that an end user won't be able to change the font settings etc because they would need to edit the textbox properties and the sheet will be protected.

I tried to follow your Private Sub example, putting this in the code for VAT, but I have to run the code for it to work - do Private Subs only work for active X?

<CODE>Private Sub TextBox1_Change()
Sheets("Del1").Shapes("TextBox 2").TextFrame.Characters.Text = Sheets("VAT").Shapes("TextBox 1").TextFrame.Characters.Text
End Sub</CODE>
 
Upvote 0
I tried to change the line of code to:

<CODE> Sheets("Del1").Shapes("TextBox 2").TextFrame.Characters.Text = Shapes("TextBox 1").TextFrame.Characters.Text </CODE>

because it is in the actual sheet, but that didn't work either?
 
Upvote 0
Yep, I've been doing that - unfortunately it seems to mean that an end user won't be able to change the font settings etc because they would need to edit the textbox properties and the sheet will be protected.
I don't understand: why do you want an end user to be able to change the font settings, etc? You, as the designer/developer of the workbook, change the text box properties (font etc.) at design time by clicking the Design Mode button on the worksheet containing the text box, then right-click the text box, then click Properties and change font, border colour etc.
I tried to follow your Private Sub example, putting this in the code for VAT, but I have to run the code for it to work - do Private Subs only work for active X?

<CODE>Private Sub TextBox1_Change()
Sheets("Del1").Shapes("TextBox 2").TextFrame.Characters.Text = Sheets("VAT").Shapes("TextBox 1").TextFrame.Characters.Text
End Sub</CODE>
Why are you using the Shapes collection? And isn't TextFrame to do with text boxes from the Drawing Toolbar? My example uses text boxes from the Control Toolbox, i.e. ActiveX controls which have user-defined events like TextBox1_Change which allow you to respond to user actions, exactly as shown in my example.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,451
Members
452,915
Latest member
hannnahheileen

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