VBA code to skip header row

ArmadaShari

New Member
Joined
Jun 23, 2017
Messages
4
Good morning - I'm a relative novice with coding for Excel. I know just enough to be dangerous, so please bear with me.

I'm using the following code to enable dates to be entered into certain columns using just 6 digits and it will automatically format it as a date with the slash characters.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Target.Column
    Case 4, 7, 8, 20, 21, 28, 30, 33, 34 ' columns numbers of dates
        TypedVal = Application.WorksheetFunction. _
            Text(Target.Value, "000000")
        NewValue = Left(TypedVal, 2) & "/" & _
            Mid(TypedVal, 3, 2) & "/" & _
            Right(TypedVal, 2)
    Case 22 ' Column U is a time column
        TypedVal = Application.WorksheetFunction. _
            Text(Target.Value, "0000")
        NewValue = Left(TypedVal, 2) & ":" & _
            Right(TypedVal, 2)
    End Select
    If NewValue > 0 Then
        Application.EnableEvents = False
        Target.Value = NewValue
        Application.EnableEvents = True
    End If
End Sub

How can I amend this so that it does NOT apply to the 1st (header) row?

Thank you,

Shari
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 Then Exit Sub
    Select Case Target.Column
    Case 4, 7, 8, 20, 21, 28, 30, 33, 34 ' columns numbers of dates
        TypedVal = Application.WorksheetFunction. _
            Text(Target.Value, "000000")
        NewValue = Left(TypedVal, 2) & "/" & _
            Mid(TypedVal, 3, 2) & "/" & _
            Right(TypedVal, 2)
    Case 22 ' Column U is a time column
        TypedVal = Application.WorksheetFunction. _
            Text(Target.Value, "0000")
        NewValue = Left(TypedVal, 2) & ":" & _
            Right(TypedVal, 2)
    End Select
    If NewValue > 0 Then
        Application.EnableEvents = False
        Target.Value = NewValue
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

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