Worksheet_Change

JOEE1979

Active Member
Joined
Dec 18, 2022
Messages
250
Office Version
  1. 365
Platform
  1. Windows
Im not sure where I screwed up, I need help please

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

Dim f As Range
Dim resp As VbMsgBoxResult
Dim i As Long


For Each f In Range("F475:F554")
    If Cells(f.Row, 6).Value = "Completed" Then
        If Cells(f.Row, 5).Value = "Running Repair" Then
     resp = MsgBox("Is unit returned to service?", _
        vbYesNo + vbQuestion)
      If resp = vbYes Then
        Set f = Range("I:J").Find("RETURNED TO SERVICE", , xlValues, xlPart, , , False)
        If Not f Is Nothing Then
          i = f.Row + 2
          Set f = Range("I:J").Find(Range(f.Row, 1).Value, , xlValues, xlWhole, , , False)
          If Not f Is Nothing Then
            MsgBox "This unit already exists in the section."
            Exit Sub
          End If
          Do While True
            If Range("I" & i).Value = "" Then
              Range("I" & i).Value = Range(f.Row, 1).Value
              Exit Do
            End If
            i = i + 1
          Loop
        End If
      End If
    End If
  End If
Next
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Im not sure where I screwed up, I need help please
Then it would help if you explained
  • what the code is supposed to do
  • in what way it is not doing that (error messages, wrong actions etc)
  • some details of what your data is like and how it is laid out. Consider XL2BB
 
Upvote 0
If "Running Repair" shows up in column E & "Completed" in column F, then I wanted a MsgBox to ask a question. If yes, then the number in column A would appear in Range (I:J).

Shift Update - Copy.xlsm
ABCDEFGHIJKLM
474Unit #Assigned toW/O #ActivityW/O TypeStatus
4751235Joe56456465BoostRunning RepairCompleted
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493MTO StickeredRETURNED TO SERVICE RepairsRETURNED TO SERVICE Sign In'sWaiting for Paperwork
494
4951235
496
497
498
Shift Update
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K466:L491Expression=MOD(ROW(),2)=0textNO
D476:D554Expression=MOD(ROW(),2)=0textNO
K495:L511Expression=MOD(ROW(),2)=0textNO
H487:J491,M487:M491Expression=MOD(ROW(),2)=0textNO
A475:F475,A476:C486,E476:F486Expression=MOD(ROW(),2)=0textNO
M495:M502Expression=MOD(ROW(),2)=0textNO
I495:J511Expression=MOD(ROW(),2)=0textNO
H495:H502Expression=MOD(ROW(),2)=0textNO
H468,J468,H469:J486,M470:M486Expression=MOD(ROW(),2)=0textNO
A487:C506,E487:F506Expression=MOD(ROW(),2)=0textNO
Cells with Data Validation
CellAllowCriteria
B475:B498List=Employee
M474:M491ListYes,No
D475:D554List=Activity
E475:E554List=WO_Type
F475:F554List=Status
 
Upvote 0
As best as I can make out what you might want ..
Test with a copy of your workbook.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range, f As Range
  Dim resp As VbMsgBoxResult
  Dim lr As Long
  
  Set Changed = Intersect(Target, Range("F475:F554"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      If c.Value = "Completed" Then
        If c.Offset(, -1).Value = "Running Repair" Then
          resp = MsgBox("Is unit returned to service?", vbYesNo + vbQuestion)
          If resp = vbYes Then
            lr = Range("I:J").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If lr > 492 Then
              If lr = 493 Then lr = 494
              Set f = Range("I:J").Find(Range("A" & c.Row).Value, , xlValues, xlWhole, , , False)
              If f Is Nothing Then
                Range("I" & lr + 1).Value = Range("A" & c.Row).Value
              Else
                MsgBox "This unit already exists in the section."
              End If
            End If
          End If
        End If
      End If
    Next c
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
For some reason its putting the number from ("A" & c.Row") into cell("I 540")
I need it to start in cell (I495)
 
Upvote 0
Sounds like there might be data further down the worksheet not shown in the post 3 mini sheet.
Try this version. (It also allows for multiple cells in column F being changed at once.)

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range, f As Range
  Dim resp As VbMsgBoxResult
  Dim nr As Long
  Dim UnitNum As String
  
  Set Changed = Intersect(Target, Range("F475:F554"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      If c.Value = "Completed" Then
        If c.Offset(, -1).Value = "Running Repair" Then
          UnitNum = Range("A" & c.Row).Value
          resp = MsgBox("Is unit " & UnitNum & " returned to service?", vbYesNo + vbQuestion)
          If resp = vbYes Then
            If IsEmpty(Range("I495").Value) Then
              nr = 495
            ElseIf IsEmpty(Range("I496").Value) Then
              nr = 496
            Else
              nr = Range("I495").End(xlDown).Row + 1
            End If
            Set f = Range("I:J").Find(UnitNum, , xlValues, xlWhole, , , False)
            If f Is Nothing Then
              Range("I" & nr).Value = UnitNum
            Else
              MsgBox "Unit " & UnitNum & " already exists in the section."
            End If
          End If
        End If
      End If
    Next c
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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