HELP WITH EDIT VBA CODE

Jagat Pavasia

Active Member
Joined
Mar 9, 2015
Messages
359
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I have VBA code :

VBA Code:
Private Sub worksheet_change(ByVal Target As Range)

Dim rng As Range
 
  If Target.Count > 1 Then Exit Sub
  Application.EnableEvents = False
  ActiveSheet.Unprotect
  Set rng = Range("C:C")
  If Not Intersect(Target, rng) Is Nothing Then
    If Target.Value = "**" Then Target.Value = Format(Date, "mm/dd/yyyy")
  End If

  If Not Intersect(Target, Range("A4:K4")) Is Nothing Then
      If Target.Value = "" Then
          ActiveSheet.Range("A6:K6").AutoFilter Field:=Target.Column
      Else
          ActiveSheet.Range("A6:K6").AutoFilter Field:=Target.Column, Operator:=xlFilterValues, Criteria1:=CStr(Target.Value)
      End If
  End If
  ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFiltering:=True
  Application.EnableEvents = True
End Sub



I want to edit this VBA code that if I type "**" in C:C then it should be auto type "NIGHT",
and if I type "**" in D:D then it should be auto type "DAY"


please give me VBA with edited
 

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"
Hi Jagat Pavasia

Maybe like this
VBA Code:
Private Sub worksheet_change(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  Application.EnableEvents = False
  ActiveSheet.Unprotect
  
  If Not Intersect(Range("C:C"), Target) Is Nothing _
    And Target.Value = "**" Then
      Target.Value = "NIGHT"
      GoTo EndSub
  End If
  If Not Intersect(Range("D:D"), Target) Is Nothing _
    And Target.Value = "**" Then
      Target.Value = "DAY"
      GoTo EndSub
  End If
  ' Else
  If Not Intersect(Target, Range("A4:K4")) Is Nothing Then
      If Target.Value = "" Then
          ActiveSheet.Range("A6:K6").AutoFilter Field:=Target.Column
      Else
          ActiveSheet.Range("A6:K6").AutoFilter Field:=Target.Column, Operator:=xlFilterValues, Criteria1:=CStr(Target.Value)
      End If
  End If
  
EndSub:
  ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFiltering:=True
  Application.EnableEvents = True
End Sub

A+
 
Upvote 0
Solution
Hi Jagat Pavasia

Maybe like this
VBA Code:
Private Sub worksheet_change(ByVal Target As Range)
  If Target.Count > 1 Then Exit Sub
  Application.EnableEvents = False
  ActiveSheet.Unprotect
 
  If Not Intersect(Range("C:C"), Target) Is Nothing _
    And Target.Value = "**" Then
      Target.Value = "NIGHT"
      GoTo EndSub
  End If
  If Not Intersect(Range("D:D"), Target) Is Nothing _
    And Target.Value = "**" Then
      Target.Value = "DAY"
      GoTo EndSub
  End If
  ' Else
  If Not Intersect(Target, Range("A4:K4")) Is Nothing Then
      If Target.Value = "" Then
          ActiveSheet.Range("A6:K6").AutoFilter Field:=Target.Column
      Else
          ActiveSheet.Range("A6:K6").AutoFilter Field:=Target.Column, Operator:=xlFilterValues, Criteria1:=CStr(Target.Value)
      End If
  End If
 
EndSub:
  ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
        False, AllowFormattingCells:=True, AllowFiltering:=True
  Application.EnableEvents = True
End Sub

A+
thank you so much.......

BrianM45

 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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