Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88087

New codes prevent old codes from working?

$
0
0

This Excel file tracks engine status from Sales and Production departments. Columns A - M in the workbook contain data necessary to deem the engine status. Columns N - AS are used to track engine status with the following column order: Sales, Production, Day 1, Status. That repeats till Day 8 (i.e. Sales, Production, Day 8, Status).

I was trying to get the Macro to do: If "Shipped" in column AV, then the empty remaining Days will have "Rollup" in both Sales and Production columns.

Can you please advise why after adding the following lines to Master Worksheet, the Macro no longer returns values in Day columns (per the IF statements from Module) though it did before adding those codes?

Dim lastColumn As Long
Dim counter As Long

Application.EnableEvents = False

' Check if header is "MB51 Shipped"
If Me.Cells(1, Target.Column).Value = "MB51 Shipped" Then

    ' Get last column based on first row
    lastColumn = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column

    ' Check all cells in row and find matches for Sales and Production
    For counter = 1 To lastColumn

        ' Check if header match and cell is not empty
        If (Me.Cells(1, counter).Value = "Sales" or Me.Cells(1, counter).Value = "Production") And Me.Cells(Target.Row, counter).Value = vbNullString Then

            Me.Cells(Target.Row, counter).Value = "Rollup"

        End If

    Next counter

End If

Application.EnableEvents = True

Thank you! And my apology for putting a lot of codes down here since someone has advised not to include a Macro enabled link.

Here is what I currently have in my Master Worksheet tab:

Option Explicit

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range, r1 As Range

        Dim lastColumn As Long
        Dim counter As Long

    Application.EnableEvents = False

    ' Check if header is "MB51 Shipped"
    If Me.Cells(1, Target.Column).Value = "MB51 Shipped" Then

        ' Get last column based on first row
        lastColumn = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column

        ' Check all cells in row and find matches for Sales and Production
        For counter = 1 To lastColumn

            ' Check if header match and cell is not empty
            If (Me.Cells(1, counter).Value = "Sales" Or Me.Cells(1, counter).Value = "Production") And Me.Cells(Target.Row, counter).Value = vbNullString Then

                Me.Cells(Target.Row, counter).Value = "Rollup"

            End If

        Next counter

    End If

    Application.EnableEvents = True

        Set r = Intersect(Target, Cells(1, 1).CurrentRegion, Columns(colSales1).Resize(, 3))
        If Not r Is Nothing Then Call DoCells(r)

    End Sub


    Private Sub DoCells(r As Range)
        Dim r1 As Range
        For Each r1 In r.Cells
            With r1
                Select Case .Column
                    Case colSales1
                        Call MasterChange(.Resize(1, 3))
                    Case colProduction1
                        Call MasterChange(.Offset(0, -1).Resize(1, 3))
                    Case colDay1
                        Call MasterChange(.Offset(0, -2).Resize(1, 3))
                End Select
            End With
        Next
    End Sub

And this is on the Module:

Option Explicit

Public Const colSales1 As Long = 14
Public Const colProduction1 As Long = 15
Public Const colDay1 As Long = 16
Public Const colStatus1 As Long = 17

Sub UpdateMaster()
    Dim r As Range
    Dim wsMaster As Worksheet, wsSAP As Worksheet

    If MsgBox("Do you want to update 'Master Worksheet' from 'SAP'?", vbYesNo + vbQuestion + vbDefaultButton2, "Update Master") = vbNo Then
        Exit Sub
    End If

    Set wsMaster = Worksheets("Master Worksheet")
    Set wsSAP = Worksheets("SAP")

    'IMPORTANT -- turn off events
    Application.EnableEvents = False

    'get rid of old data
    wsMaster.Cells.Clear

    'copy SAP
    wsSAP.Cells(1, 1).CurrentRegion.Copy wsMaster.Cells(1, 1)

    'add formulas - double "" inside string to get one
    Set r = wsMaster.Cells(1, 1).CurrentRegion.Columns(colStatus1)
    Set r = r.Cells(2, 1).Resize(r.Rows.Count - 1, r.Columns.Count)
    r.Formula = "=IF(O2=N2,""Sales/Production"",IF(P2=O2,""Production"",IF(P2=N2,""Sales"","""")))"'IMPORTANT -- turn on events
    Application.EnableEvents = True

End Sub

Sub ClearMaster()
    Dim ws As Worksheet
    Set ws = Workbooks("SampleReport03.xlsm").Sheets("Master Worksheet")
    ws.Rows("2:"& Rows.Count).Clear
End Sub

Sub ClearSAP()
    Dim ws As Worksheet
    Set ws = Workbooks("SampleReport.xlsm").Sheets("SAP")
    ws.Rows("2:"& Rows.Count).ClearContents
End Sub


Public Sub MasterChange(SPD As Range)
    Dim rSales As Range
    Dim rProduction As Range
    Dim rDay As Range

    Set rSales = SPD.Cells(1, 1)
    Set rProduction = SPD.Cells(1, 2)
    Set rDay = SPD.Cells(1, 3)

    Application.EnableEvents = False
    If rSales = "Rollup" And rProduction = "Rollup" Then
        rDay = "Rollup"
    ElseIf rSales = "Rollup" And rProduction = "Green" Then
        rDay = "Green"
    ElseIf rSales = "Rollup" And rProduction = "Yellow" Then
        rDay = "Yellow"
    ElseIf rSales = "Rollup" And rProduction = "Red" Then
        rDay = "Red"
    ElseIf rSales = "Rollup" And rProduction = "Overdue" Then
        rDay = "Overdue"
    ElseIf rSales = "" And rProduction = "" Then
        rDay.ClearContents
    End If
    Application.EnableEvents = True
End Sub

Viewing all articles
Browse latest Browse all 88087

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>