Multiple worksheet change and Formula use

Noid67

New Member
Joined
Feb 18, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
So I have a two part question. I am struggling to understand how to incorporate multiple worksheet changes into one sheet. I have seen the examples for using IF NOT INTERSECT, but cannot correlate it to my needs. I also have a formula that I need to run in multiple rows and is the second part of the worksheet change. Below is the code I currently have for a worksheet change that works perfectly:

VBA Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim DestWH As String
Dim DWHRowNum As Long
Dim dt As Date

DWHRowNum = 2
dt = Format(Date, "mm/dd/yyy")

Application.EnableEvents = False

Do Until Cells(DWHRowNum, 2).Value = ""
    Select Case Cells(DWHRowNum, 6).Value
        Case Is = "ABQ1"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "BFI4"
            Cells(DWHRowNum, 7).Value = dt + 1
            Cells(DWHRowNum, 8).Value = "04:30"
        Case Is = "CLE2"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "DEN3"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "DEN4"
            Cells(DWHRowNum, 7).Value = dt + 1
            Cells(DWHRowNum, 8).Value = "04:30"
        Case Is = "GEG1"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "LIT1"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "ORD5"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "ORF3"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "PAE2"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "PCW1"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "PDX9"
            Cells(DWHRowNum, 7).Value = dt + 1
            Cells(DWHRowNum, 8).Value = "04:30"
        Case Is = "SLC1"
            Cells(DWHRowNum, 7).Value = dt
            Cells(DWHRowNum, 8).Value = "17:00"
        Case Is = "SMF1"
            Cells(DWHRowNum, 7).Value = dt + 1
            Cells(DWHRowNum, 8).Value = "04:30"
    End Select
    DWHRowNum = DWHRowNum + 1
Loop

Application.EnableEvents = True

End Sub

I want to include this formula that affects a different cell in the same worksheet as above, but this also needs to trigger based on a change in cell(s) N2:N28

VBA Code:
Sub VRID_Status()
'
' VRID_Status Macro
'

    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-10]="""","""",IF(AND(RC[-4]="""",RC[-3]>RC[-10]),""Future"",""Current""))"
    Range("Q18").Select
End Sub

Any assistance is greatly appreciated.
 
Ok, just saw a problem in the results. The new code has changed the date in column G to the year 2449. I cannot figure out how that is happening. But, when I put in a date ahead of that date into column N, the code is working as we hoped.
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Change
Code:
dt = Format(Date, "mm/dd/yyy")
to
Code:
dt = Format(Date, "mm/dd/yyyy")
 
Upvote 0
I added Application.EnableEvents = False to just above the screenupdating and a true at the end. Went back and reinput the data in column F and all worked well.
 
Upvote 0
so, after saving, closing, and reopening the workbook, the code no longer works. I have a feeling it has to do with the removal of the Do Until loop which used column B to find the end (Row 28). After entering data into any row of column F, the code is not triggered at all.
 
Upvote 0
When you set Application.Enableevents=False, if the macro errors out without completing the Application.Enableevents=True line of code, any event macros you are using will not be triggered. Close Excel and re-start it and re- open the file.
 
Upvote 0
When you set Application.Enableevents=False, if the macro errors out without completing the Application.Enableevents=True line of code, any event macros you are using will not be triggered. Close Excel and re-start it and re- open the file.
Is there a way around that?
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Dim dt As Date, lRow As Long
    lRow = Range("B" & Rows.Count).End(xlUp).Row
    If Intersect(Target, Range("F:F,N:N")) Is Nothing Then Exit Sub
    If Target.CountLarge > 1 Then Exit Sub
    Application.EnableEvents = False
    On Error GoTo errHandler
    dt = Format(Date, "mm/dd/yyyy")
    Select Case Target.Column
        Case Is = 6
            Select Case Target.Value
                Case Is = "ABQ1"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "BFI4"
                    Cells(Target.Row, 7).Value = dt + 1
                    Cells(Target.Row, 8).Value = "04:30"
                Case Is = "CLE2"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "DEN3"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "DEN4"
                    Cells(Target.Row, 7).Value = dt + 1
                    Cells(Target.Row, 8).Value = "04:30"
                Case Is = "GEG1"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "LIT1"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "ORD5"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "ORF3"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "PAE2"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "PCW1"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "PDX9"
                    Cells(Target.Row, 7).Value = dt + 1
                    Cells(Target.Row, 8).Value = "04:30"
                Case Is = "SLC1"
                    Cells(Target.Row, 7).Value = dt
                    Cells(Target.Row, 8).Value = "17:00"
                Case Is = "SMF1"
                    Cells(Target.Row, 7).Value = dt + 1
                    Cells(Target.Row, 8).Value = "04:30"
            End Select
        Case Is = 14
            If Range("G" & Target.Row) = "" Then
                Range("Q" & Target.Row) = ""
            ElseIf Range("M" & Target.Row) = "" And Range("N" & Target.Row) > Range("G" & Target.Row) Then
                Range("Q" & Target.Row) = "Future"
            Else
                Range("Q" & Target.Row) = "Current"
            End If
    End Select
Done:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Exit Sub
errHandler:
    MsgBox "The following error occurred: " & Err.Description
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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