VBA code for changes in cell and delete cell value

cooleomter

New Member
Joined
Sep 3, 2024
Messages
25
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
The COde works if i only delete one by one.. But if i Highlight many cells and delete all at once.. it debugs
Need help please,... here is my code:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextRow As Long
Set historyWks = ActiveSheet

'Check if the changed cells are in the specified range
If Not Intersect(Target, Me.Range("C8:C3000")) Is Nothing Then
' to check cell if empty. or i delete
If Target.Cells.Value = " " Or IsEmpty(Target) Then Exit Sub 'Check if target cell is empty

With historyWks
nextRow = .Cells(.Rows.Count, "C").End(xlUp).Offset(0, 1).Row
End With

With historyWks
With .Cells(nextRow, "G")
.Value = Date
.NumberFormat = "mm/dd/yyyy"
End With
With .Cells(nextRow, "F")
.Value = Time
.NumberFormat = "h:mm:ss AM/PM"
End With
With .Cells(nextRow, "D")
.Value = Range("F1").Value 'pACKING OPERATOR
End With
With .Cells(nextRow, "H")
.Value = Range("D2").Value ' MODEL
End With
With .Cells(nextRow, "I")
.Value = Range("F2").Value ' wEIGHING OPERATOR
End With

End With

End If


End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I cannot test this without your file, but it handles changes to multiple cells.

When you paste code into your post it improves readability to add CODE tags by selecting the code and clicking the VBA button in the edit controls.

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

   Dim nextRow As Long
   Set historyWks = ActiveSheet
   Dim Cell As Range
   
   For Each Cell In Target
   
      'Check if the changed cells are in the specified range
      If Not Intersect(Cell, Me.Range("C8:C3000")) Is Nothing Then
      
         ' to check cell if empty. or i delete
         If Cell.Value <> " " And Not IsEmpty(Cell) Then 'Check if Cell is empty
      
            With historyWks
               nextRow = .Cells(.Rows.Count, "C").End(xlUp).Offset(0, 1).Row
            End With
            
            With historyWks
            
               With .Cells(nextRow, "G")
                  .Value = Date
                  .NumberFormat = "mm/dd/yyyy"
               End With
               With .Cells(nextRow, "F")
                  .Value = Time
                  .NumberFormat = "h:mm:ss AM/PM"
               End With
               With .Cells(nextRow, "D")
                  .Value = Range("F1").Value 'pACKING OPERATOR
               End With
               With .Cells(nextRow, "H")
                  .Value = Range("D2").Value ' MODEL
               End With
               With .Cells(nextRow, "I")
                  .Value = Range("F2").Value ' wEIGHING OPERATOR
               End With
            
            End With
            
         End If
      
      End If
   
   Next Cell


End Sub
 
Upvote 0
try

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim nextRow As Long
Dim r As Range
Set historyWks = ActiveSheet


If Not Intersect(Target, Range("C8:C3000")) Is Nothing Then
    If Not Target.Cells.Value = "" Then
        With historyWks
            .Cells(Target.Row, "G").Value = Format(Date, "mm/dd/yyyy")
            .Cells(Target.Row, "F").Value = Format(Time, "h:mm:ss AM/PM")
            .Cells(Target.Row, "D").Value = .Range("F1").Value 'pACKING OPERATOR
            .Cells(Target.Row, "H").Value = .Range("D2").Value ' MODEL
            .Cells(Target.Row, "I").Value = .Range("F2").Value ' wEIGHING OPERATOR
        End With
            Else
        For Each r In Range("C8:C3000")
            If r.Value = "" Then
                With historyWks
                    .Cells(r.Row, "G").Value = ""
                    .Cells(r.Row, "F").Value = ""
                    .Cells(r.Row, "D").Value = ""
                    .Cells(r.Row, "H").Value = ""
                    .Cells(r.Row, "I").Value = ""
                End With
            End If
        Next r
    End If
End If


End Sub
 
Upvote 1
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, r As Range
    Set rng = Intersect(Target, [C8:C3000])
    If rng Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Each r In rng
        If Trim$(r) <> "" Then
            r(, 2) = [f1]
            With r(, 4).Resize(, 2)
                .Value = Array(Time, Date)
                .NumberFormat = Array("h:mm:ss AM/PM", "mm/dd/yyyy")
            End With
            r(, 6).Resize(, 2) = Array([D2], [F2])
        Else
            r.Range("b1,d1:g1").ClearContents
        End If
    Next
    Application.EnableEvents = True
End Sub
 
Upvote 1
Solution
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, r As Range
    Set rng = Intersect(Target, [C8:C3000])
    If rng Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Each r In rng
        If Trim$(r) <> "" Then
            r(, 2) = [f1]
            With r(, 4).Resize(, 2)
                .Value = Array(Time, Date)
                .NumberFormat = Array("h:mm:ss AM/PM", "mm/dd/yyyy")
            End With
            r(, 6).Resize(, 2) = Array([D2], [F2])
        Else
            r.Range("b1,d1:g1").ClearContents
        End If
    Next
    Application.EnableEvents = True
End Sub
Tha
try

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim nextRow As Long
Dim r As Range
Set historyWks = ActiveSheet


If Not Intersect(Target, Range("C8:C3000")) Is Nothing Then
    If Not Target.Cells.Value = "" Then
        With historyWks
            .Cells(Target.Row, "G").Value = Format(Date, "mm/dd/yyyy")
            .Cells(Target.Row, "F").Value = Format(Time, "h:mm:ss AM/PM")
            .Cells(Target.Row, "D").Value = .Range("F1").Value 'pACKING OPERATOR
            .Cells(Target.Row, "H").Value = .Range("D2").Value ' MODEL
            .Cells(Target.Row, "I").Value = .Range("F2").Value ' wEIGHING OPERATOR
        End With
            Else
        For Each r In Range("C8:C3000")
            If r.Value = "" Then
                With historyWks
                    .Cells(r.Row, "G").Value = ""
                    .Cells(r.Row, "F").Value = ""
                    .Cells(r.Row, "D").Value = ""
                    .Cells(r.Row, "H").Value = ""
                    .Cells(r.Row, "I").Value = ""
                End With
            End If
        Next r
    End If
End If


End Sub
Thank you for correcting my code. THank you so much.
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, r As Range
    Set rng = Intersect(Target, [C8:C3000])
    If rng Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Each r In rng
        If Trim$(r) <> "" Then
            r(, 2) = [f1]
            With r(, 4).Resize(, 2)
                .Value = Array(Time, Date)
                .NumberFormat = Array("h:mm:ss AM/PM", "mm/dd/yyyy")
            End With
            r(, 6).Resize(, 2) = Array([D2], [F2])
        Else
            r.Range("b1,d1:g1").ClearContents
        End If
    Next
    Application.EnableEvents = True
End Sub
Thank you for correcting my code. THank you so much.
 
Upvote 0
Thank you for correcting my code. THank you so much. and you make some additional features such as when i delete the input data on c column it also deletes the adjacent d,,f,g,h,and I column which makes me happy.. thank you
 
Upvote 0
I cannot test this without your file, but it handles changes to multiple cells.

When you paste code into your post it improves readability to add CODE tags by selecting the code and clicking the VBA button in the edit controls.

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

   Dim nextRow As Long
   Set historyWks = ActiveSheet
   Dim Cell As Range
  
   For Each Cell In Target
  
      'Check if the changed cells are in the specified range
      If Not Intersect(Cell, Me.Range("C8:C3000")) Is Nothing Then
     
         ' to check cell if empty. or i delete
         If Cell.Value <> " " And Not IsEmpty(Cell) Then 'Check if Cell is empty
     
            With historyWks
               nextRow = .Cells(.Rows.Count, "C").End(xlUp).Offset(0, 1).Row
            End With
           
            With historyWks
           
               With .Cells(nextRow, "G")
                  .Value = Date
                  .NumberFormat = "mm/dd/yyyy"
               End With
               With .Cells(nextRow, "F")
                  .Value = Time
                  .NumberFormat = "h:mm:ss AM/PM"
               End With
               With .Cells(nextRow, "D")
                  .Value = Range("F1").Value 'pACKING OPERATOR
               End With
               With .Cells(nextRow, "H")
                  .Value = Range("D2").Value ' MODEL
               End With
               With .Cells(nextRow, "I")
                  .Value = Range("F2").Value ' wEIGHING OPERATOR
               End With
           
            End With
           
         End If
     
      End If
  
   Next Cell


End Sub
Thank you so much for correcting my code.
here my work..
test4.xlsm
ABCDEFGHIJ
1Category:Packing OperatorShipment Plan Qty: 
2ModelWeighing OperatorFinished Good Qty.: 
3Shipment Date:Shipment FreightGood Weight 
4Start SerialEnd SerialNG Weight 
5Minimum Maximum Lacking 
6Excess 
7Box No. BIGBox No. SMALLWeightPacking OperatorSerial NumberTimeDateModelWeighing OperatorRemarks
8   
9   
10   
11   
12   
13   
14   
15   
MASTER COPY
Cell Formulas
RangeFormula
D5D5=IF((D2=""),"",INDEX(Validation!I2:I8,MATCH('MASTER COPY'!D2,Validation!F2:F8,0)))
F5F5=IF((D2=""),"",INDEX(Validation!J2:J8,MATCH('MASTER COPY'!D2,Validation!F2:F8,0)))
H1H1=IF(D4="","",(F4-D4+1))
H2H2=COUNT(C8:C100384)
H3H3=H2-H4
H4H4=((COUNTIFS(C8:C100000,"<"&D5)+(COUNTIFS(C8:C100000,">"&F5))))
H5H5=((COUNTIFS(C8:C10001,"<"&D5)))
H6H6=((COUNTIFS(L8:L10003,">"&O5)))
A8:A15A8=IFERROR(IFS($D$2="HDROP-14",INDEX(Serial!$E$2:$E$25426,MATCH('MASTER COPY'!E8,Serial!$A$2:$A$25426,0)),$D$2="IND-B",INDEX(Serial!$D$2:$D$25426,MATCH('MASTER COPY'!E8,Serial!$A$2:$A$25426,0)),OR($D$2="HDROP-FK1",$D$2="JD2100",$D$2="JD2000WH",$D$2="JD2000BK"),INDEX(Serial!$B$2:$B$25426,MATCH('MASTER COPY'!E8,Serial!$A$2:$A$25426,0)),$D$2="ZR-02",INDEX(Serial!$F$2:$F$25426,MATCH('MASTER COPY'!E8,Serial!$A$2:$A$25426,0))),"")
B8:B15B8=IFERROR(IFS(OR($D$2="HDROP-14",$D$2="ZR-02"),INDEX(Serial!$C$2:$C$25426,MATCH('MASTER COPY'!E8,Serial!$A$2:$A$25426,0))),"")
E7E7=IF(OR(D1="Option",D1="Harness"),"Item Count","Serial Number")
E8E8=IF(C8="","",D4)
E9:E15E9=IF(C9="","",E8+1)
Named Ranges
NameRefers ToCells
ACCList=Validation!$F$6:$F$8D5, F5
OPList=Validation!$F$3:$F$5D5, F5
SIBList=Validation!$F$2D5, F5
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C8:C1048576Cell Value<$D$5textNO
C8:C1048576Cell Value>$F$5textNO
Cells with Data Validation
CellAllowCriteria
D1List=ManuList
D2List=INDIRECT(VLOOKUP($D$1,ManuLookUp,2,0) &"List")
D3Any value
F1:F2List=OFFSET(Validation!$A$2:$A$999267,0,0,COUNTA(Validation!$A$2:$A$198)-0,1)
F3List=Validation!$C$10:$C$14
 
Upvote 0
try

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim nextRow As Long
Dim r As Range
Set historyWks = ActiveSheet


If Not Intersect(Target, Range("C8:C3000")) Is Nothing Then
    If Not Target.Cells.Value = "" Then
        With historyWks
            .Cells(Target.Row, "G").Value = Format(Date, "mm/dd/yyyy")
            .Cells(Target.Row, "F").Value = Format(Time, "h:mm:ss AM/PM")
            .Cells(Target.Row, "D").Value = .Range("F1").Value 'pACKING OPERATOR
            .Cells(Target.Row, "H").Value = .Range("D2").Value ' MODEL
            .Cells(Target.Row, "I").Value = .Range("F2").Value ' wEIGHING OPERATOR
        End With
            Else
        For Each r In Range("C8:C3000")
            If r.Value = "" Then
                With historyWks
                    .Cells(r.Row, "G").Value = ""
                    .Cells(r.Row, "F").Value = ""
                    .Cells(r.Row, "D").Value = ""
                    .Cells(r.Row, "H").Value = ""
                    .Cells(r.Row, "I").Value = ""
                End With
            End If
        Next r
    End If
End If


End Sub
Thank you for your help all of you help me alot
 
Upvote 0

Forum statistics

Threads
1,223,734
Messages
6,174,186
Members
452,550
Latest member
southernsquid2

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