Record Macro vs Command button

HazelHanH

New Member
Joined
Dec 17, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi, I am new to VBA. This is probably a very simple mistake but I need some guidance.

I am trying to align textboxes in the excel sheet and wanted to create a shortcut command button which can disappear when I am printing the worksheet.
I did a Record Macro to get the script. It works when I run the Macro.
1639916098609.png


However, when I "Call" this code to link it into the Command button code, it does not work. Will be grateful if anyone can spot what I am missing here...

1639916407002.png


1639916436881.png
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Sorry, I missed out this - This is the code in the command button.

1639916750304.png
 
Upvote 0
Your code refers to "Selection" but you have not first selected the shapes .

Look at : SELECTING TEXT BOXES

VBA Code:
ActiveSheet.Shapes.Range(Array("TextBox1", "TextBox2")).Select
 
Upvote 0
Your code refers to "Selection" but you have not first selected the shapes .

Look at : SELECTING TEXT BOXES

VBA Code:
ActiveSheet.Shapes.Range(Array("TextBox1", "TextBox2")).Select
Thanks for your reply.
I did select the textbox if you see the screenshot background which I took with the error code.

Is there another code you will suggest I try?
 
Upvote 0
VBA Code:
Option Explicit

Sub SelAlignTexts()
    
    ActiveSheet.Shapes.Range(Array("TextBox1", "TextBox2", "TextBox3")).Select
    Selection.ShapeRange.Align msoAlignTops, False
    Selection.ShapeRange.Distribute msoDistributeHorizontally, False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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