VBA Macro to auto run on open and close with formatting

titoexcel

Board Regular
Joined
Mar 26, 2013
Messages
55
Hi,

I am trying to keep column G as text format. I would not like anyone to change the format as another program needs to read it as text.

so the format has to be for ex. 12/22/2014 or 12/01/2014 but text and users should not be allowed to change it.

What i had so far is not working, can anyone take a look see what I am doing wrong. What I did is basically run a macro when it opens and convert column G to text and when it closes run the macro convert column G to text. Is there a better way? Please help

Code:
Private Sub workbook_open()
Columns("G:G").Select
Selection.NumberFormat = "@"
End Sub


Private Sub workbook_beforeclose()
Columns("G:G").Select
Selection.NumberFormat = "@"
End Sub
 
I think the code needs to be placed on a sheet module not a regular module. That associates it with the change event for the sheet.

Just type a date in G10 and leave the cell. if you move back to the cell it will have changed from 12/12/2014 to '12/12/2014 (note the quote indicating text) If you typed '12/12/2014 it will do nothing because it is not formatted as a date.

I am doing something wrong as it does not do that. :(
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
When you open the code editor, you need to dbl click the sheet that you want the event to effect. That will open a window with "Bookname - Sheetname (Code)" in the title. The event code needs to go in there.

I just did a little experiment and if the cell is type text before you type in it, then the event does not need to do anything.
 
Upvote 0
can you describe what is happening? If you set a break point in the event procedure you should be able to see it fire off and see what it is doing.
 
Upvote 0

Forum statistics

Threads
1,215,503
Messages
6,125,175
Members
449,212
Latest member
kenmaldonado

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