Macro needs slight change

EE Pros,

I have a Macro application that I received great help from Fanpages and Martin Liss.  When button was selected, it should change the date/time format in two cells.  I added one date/time format to the macro and now it won't work.  Can you take a look?  

Much thanks in advance,

B.
Time-Interval-Frequency-calculationv51.x
Bright01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kanti PrasadCommented:
Hi

Can you please attach the xls file or the code you can and what you are trying ?
0
Bright01Author Commented:
0
Bright01Author Commented:
Option Explicit
Sub ChangeFormat()
    With Range("C4")
        Select Case .NumberFormat
            Case "m/d/yy h:mm;@" ' DATE/TIME
                .NumberFormat = "yyyy"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Yr."
               
'            Case "mm/ddd/yyyy" 'Date MO DAY YEAR"
            Case "yyyy" 'Date MO DAY YEAR"
                .NumberFormat = "mm/dd/yyyy"
'                .ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Day Yr."
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Day Yr."
               
'            Case "yyyy" ' DATE YR
            Case "mm/dd/yyyy" ' DATE YR
                .NumberFormat = "mm/yyyy"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Yr."
            Case "mm/yyyy" ' DATE MO YR
                .NumberFormat = "ddd"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Day"
            Case "ddd" ' DATE DAY
                .NumberFormat = "hh:mm"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Time: Hr./Min."
            Case "hh:mm"
                .NumberFormat = "hh:mm:ss"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Time: Hr./Min./Sec."
            Case "hh:mm:ss"
                .NumberFormat = "m/d/yy h:mm;@"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date/Time"
        End Select
        Range("C9").NumberFormat = .NumberFormat
    End With
End Sub
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

David Johnson, CD, MVPOwnerCommented:
it helps if you place code into a code block
also Experts Exchange currently has a 40 character limit on filenames so anything after 40 characters gets truncated .. your attached file was an .xlsm
Option Explicit
Sub ChangeFormat()
    With Range("C4")
        Select Case .NumberFormat
            Case "m/d/yy h:mm;@" ' DATE/TIME
                .NumberFormat = "yyyy"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Yr."
               
'            Case "mm/ddd/yyyy" 'Date MO DAY YEAR"
            Case "yyyy" 'Date MO DAY YEAR"
                .NumberFormat = "mm/dd/yyyy"
'                .ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Day Yr."
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Day Yr."
               
'            Case "yyyy" ' DATE YR
            Case "mm/dd/yyyy" ' DATE YR
                .NumberFormat = "mm/yyyy"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Yr."
            Case "mm/yyyy" ' DATE MO YR
                .NumberFormat = "ddd"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Day"
            Case "ddd" ' DATE DAY
                .NumberFormat = "hh:mm"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Time: Hr./Min."
            Case "hh:mm"
                .NumberFormat = "hh:mm:ss"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Time: Hr./Min./Sec."
            Case "hh:mm:ss"
                .NumberFormat = "m/d/yy h:mm;@"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date/Time"
        End Select
        Range("C9").NumberFormat = .NumberFormat
    End With
End Sub 

Open in new window

0
Bright01Author Commented:
Dave,

Thanks for the tips.  Do I use "Code" and then place the code in between the parameters?

B.
0
David Johnson, CD, MVPOwnerCommented:
nopoints: precisely.. I was in another area and there was a mention of the filename being truncated that is why I popped in here
Do not give me any points please
0
Martin LissOlder than dirtCommented:
What isn't working?
0
Martin LissOlder than dirtCommented:
It wasn't working beacuse the NumberFormat was "mm/d/yyyy" which wasn't one of the expected values. I added an Case Else (catch-all) case so that it will always change it to something.

Sub ChangeFormat()
    With Range("C4")
        Select Case .NumberFormat
            Case "m/d/yy h:mm;@" ' DATE/TIME
                .NumberFormat = "yyyy"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Yr."
                
'            Case "mm/ddd/yyyy" 'Date MO DAY YEAR"
            Case "yyyy" 'Date MO DAY YEAR"
                .NumberFormat = "mm/dd/yyyy"
'                .ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Day Yr."
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Day Yr."
                
'            Case "yyyy" ' DATE YR
            Case "mm/dd/yyyy" ' DATE YR
                .NumberFormat = "mm/yyyy"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Mo. Yr."
            Case "mm/yyyy" ' DATE MO YR
                .NumberFormat = "ddd"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date: Day"
            Case "ddd" ' DATE DAY
                .NumberFormat = "hh:mm"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Time: Hr./Min."
            Case "hh:mm"
                .NumberFormat = "hh:mm:ss"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Time: Hr./Min./Sec."
            Case "hh:mm:ss"
                .NumberFormat = "m/d/yy h:mm;@"
                ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date/Time"
            Case Else
                 ActiveSheet.Shapes("CycleFormat").TextFrame2.TextRange.Characters.Text = "Date/Time"
                .NumberFormat = "m/d/yy h:mm;@"
        End Select
        Range("C9").NumberFormat = .NumberFormat
    End With
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bright01Author Commented:
Martin,  PERFECT!  Now I get it.  You have to declare the format you may be using before using it.

Thanks again for the work and the lesson.

B.
0
Bright01Author Commented:
Martin,

I've got to issue another EE Post.  I found another bug in the original code that calculated the Time/Date.  Hope you will pick it up if you have the time.

B.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.