VBA and Macros 2013 book question

Nurzhan

Board Regular
Joined
Dec 13, 2017
Messages
60
Hallo,
Tried to use the below code from the book on page 136 but it doesn't work. I use excel 2010.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)Dim ThisColumn As Integer
Dim UserInput As String, NewInput As String
ThisColumn = Target.Column


If ThisColumn < 3 Then
  If Target.Count > 1 Then Exit Sub
  If Len(Target) = 1 Then Exit Sub


  UserInput = Target.Value
  If UserInput > 1 Then
     NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
     Application.EnableEvents = False
     Target = NewInput
     Application.EnableEvents = True
    
   End If
End If
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this on a blank sheet:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ThisColumn As Integer
Dim UserInput As String, NewInput As String
ThisColumn = Target.Column

  If Target.Count > 1 Or Len(Target) = 1 Then
  Exit Sub
  End If
  
  UserInput = Target.Value
  If UserInput > 1 Then
     NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
     Application.EnableEvents = False
     Target = NewInput
     Application.EnableEvents = True
    
   End If

End Sub

I don't have the book so not sure why it doesn't want columns A-B filled (data from other exercises are there?)
 
Last edited:
Upvote 0
As a newbie user myself a simple question is are you putting this code in a worksheet or a module
 
Upvote 0
Doesn't work either.
This Ex's task acc to the book:
C A S E S T U DY: QUICKLY ENTERING MILITARY TIME IN TO A CELL
You’re entering arrival and departure times and want the times to be formatted with a 24-hour clock, which is also known
as military time. You have tried formatting the cell, but no matter how you enter the times, they are displayed in the 0:00
hours and minutes format.
The only way to get the time to appear as military time such as 23:45, is to have the time entered in the cell in this manner.
Because typing the colon is time-consuming, it would be more efficient to enter the numbers and let Excel format it
for you. The solution is to use a Change event to take what is in the cell and insert the colon for you: then goes the code above.

An entry of 2345 will display as 23:45. Note that the code limits this format change to Columns A and B (If
ThisColumn < 3). Without this limitation, entering numbers anywhere on a sheet such as in a totals column would
force it to be reformatted.
 
Upvote 0
Thanks man! Good question that allowed me to find the error. I didn't know that I can put codes in Worksheets.
 
Upvote 0
Now I want to rewrite this code so that this event works in columns AU,AV, BA and BB. How can i do that?
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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