vba code doesn't run through all the way? It takes 2 clicks of a command button to execute??

Xceller

Active Member
Joined
Aug 24, 2009
Messages
265
vba Gurus:

Thanks in advance for your help. I attached the code below to a command button, but it takes 2 clicks to execute the code. Any ideas?? Could this be accomplished in a single click of the button.

Sub CnvtTextDate1()

Dim rng As Variant
For Each rng In Selection
rng.Value = Trim(rng)
rng.NumberFormat = "mm/dd/yy;@"
Next rng

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,217
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
You need to change the name of your sub to the name of the command button
Something like Sub CommandButton1Click()
 

Xceller

Active Member
Joined
Aug 24, 2009
Messages
265
You need to change the name of your sub to the name of the command button
Something like Sub CommandButton1Click()

Hi Michael - Thanks for your reply. It is a Form button not a command button. I right click the Form button and attached the code to the button.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,217
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
I attached the code below to a command button

You did say a Command button
OK, Right click on the form button and select view code.
Now paste the code you have in between the start and end sub lines
 

Xceller

Active Member
Joined
Aug 24, 2009
Messages
265
You did say a Command button
OK, Right click on the form button and select view code.
Now paste the code you have in between the start and end sub lines

I did that, but it takes 2 clicks of the button to execute the code completely.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
Xceller

What do you mean exactly by it doesn't execute the code completely?

Does it not execute at all the first time you click the button?

Or when you click the button the first time the code is executed but doesn't run the code on the entire range you've selected?
 

Watch MrExcel Video

Forum statistics

Threads
1,130,447
Messages
5,642,209
Members
417,262
Latest member
andrewd1

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
Top