Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 新代码阻止旧代码工作?_Excel_Vba - Fatal编程技术网

Excel 新代码阻止旧代码工作?

Excel 新代码阻止旧代码工作?,excel,vba,Excel,Vba,此Excel文件跟踪销售和生产部门的引擎状态。工作簿中的A-M列包含发动机状态所需的数据。列N-AS用于跟踪发动机状态,列顺序如下:销售、生产、第1天、状态。重复到第8天(即销售、生产、第8天、状态) 这8天代表一个月的最后8天,在此期间,数据每天更新到A-M列。然而,假设今天是第二天,虽然A-M列中的数据很可能会更新,但第1天的列(销售、生产、第1天、状态)中的数据保持不变。然后,我们继续记录第2天的状态 这是我的问题,我试图让宏执行:如果在AV列中“发货”,那么剩余的空天数将在销售和生产列中

此Excel文件跟踪销售和生产部门的引擎状态。工作簿中的A-M列包含发动机状态所需的数据。列N-AS用于跟踪发动机状态,列顺序如下:销售、生产、第1天、状态。重复到第8天(即销售、生产、第8天、状态)

这8天代表一个月的最后8天,在此期间,数据每天更新到A-M列。然而,假设今天是第二天,虽然A-M列中的数据很可能会更新,但第1天的列(销售、生产、第1天、状态)中的数据保持不变。然后,我们继续记录第2天的状态

这是我的问题,我试图让宏执行:如果在AV列中“发货”,那么剩余的空天数将在销售和生产列中都有“汇总”

您能否告知为什么在将以下行添加到主工作表后,宏不再返回日列中的值(根据模块中的IF语句),尽管它在添加这些代码之前返回了值

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
谢谢大家!!我很抱歉在这里放了很多代码,因为有人建议不要包含启用宏的链接

以下是我当前在主工作表选项卡中的内容:

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
这在模块上:

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
以下是我的电子表格中的内容:

| Title |发动机系列|细分市场|客户|发动机型号| S/N |建造规格|实际完工|销售订单|项目|承诺日期| EPS日期|目标|销售|生产|第1天|状态|销售|第2天|状态|销售|第3天|状态|生产|4 |状态|销售|生产|第5天|状态|销售|生产|第6天|状态|销售|生产|第8天|状态|状态|评论| MB51发货| FPS |工厂产权转让|
|--------|------------------|----------------|----------|--------------|-----|------------|-------------|-------------|-------|----------------|------------|-----------|-------|------------|--------|------------------|--------|------------|-------|------------------|--------|------------|-------|------------------|--------|------------|-------|------------------|--------|------------|-------|------------------|--------|------------|-------|------------------|--------|------------|-------|------------------|--------|------------|-------|------------------|------------------|----------|--------------|------|-------|----------------|
|汇总| PS | APU | HAC | T-62T-46C12 | 1 | BS1 | 0000-00-00 | 0 | 2019/12/31 | 2019/12/31 |汇总|绿色|黄色|生产|汇总|销售/生产|汇总|销售/生产|汇总|销售/生产|汇总|汇总|销售/生产|汇总|汇总|销售/生产|汇总|汇总|销售/生产|汇总|销售/生产|销售/生产|销售/生产|出货|124;|
|汇总| PS | APU | SA | S2300 | 2 | BS2 | 2019/06/25 | 1 | 380 | 2019/06/24 | 2019/06/25 |可用| | | | |销售/生产| | | | | | | | | |销售/生产|销售/生产| | | | | | | | | | | | | | | | | | | | | |销售/生产||

|黄色| PS | APU | AOG | PS3200 | 3 | BS3 | 0000-00-00 | 2 | 1 | 2019/12/16 | 2019/12/20 |黄色| | | | | |销售/生产| | | | | | | | | | | | | | | | | 124销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产销售/生产(我从你的评论中了解到)。这里有一些注释来描述做出更改的内容和原因

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

    ' Get last column based on first row
    '*** Need to set lastColumn outside of the Me.Cells(1, Target.Column).value = "MB51 Shipped" statement
    '*** so that the Intersect function does not fail (blow-up) if a cell in a column other than "MB51 Shipped" is modified .
    '*** Perhaps the Intersect call belongs within the If statement?
    lastColumn = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column

    ' 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
        '***In the posted code, The Intersect() function was never returning a non-null Range
        '***I think your intent was to find the intersection of the colSales1 column and the Target row
        'Set r = Intersect(Target, Cells(1, 1).CurrentRegion, Columns(colSales1).Resize(, 3))

        '*** This Intersect() call provides the range that I think you intended
        Set r = Intersect(Range(Cells(Target.Row, 1), Cells(Target.Row, lastColumn)), Cells(1, 1).CurrentRegion)

        If Not r Is Nothing Then Call DoCells(r)

    End Sub

    '*** DoCells was only attempting to operate on three columns, colSales1, colProduction1, and colDay1
    '*** And...each of the Case statements is sending the same range to MasterChange -> so, it was doing the same operation 3 times
    '*** I believe the intent was to call each Sales/Production/Days group and update...so, replacing the Select Case with
    '*** the following if statement updates all the Sales/Production/Days groups.
    Private Sub DoCells(r As Range)
        Dim r1 As Range
        For Each r1 In r.Cells
            With r1
                'Find each "Sales" column.  Call MasterChange only once for each group
                If Me.Cells(1, colSales1).value = r1.Offset(-1, 0).value Then
                    MasterChange .Resize(1, 3)
                End If
                '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

请不要包含指向启用宏的文件的链接。不太可能有人会下载它,因为这可能很危险。@braX嗨,谢谢你的建议。我已经用所有必要的代码编辑了这篇文章。再次感谢您可能希望创建一个mcve,它将使每个人都更容易找到问题(谁知道呢?在这个过程中,您自己可能会发现问题)@controlnetic.nomad嗨,我肯定可以创建一个mcve,但是,它仍然是一个启用宏的工作簿,不是吗?那么,这就是布拉克斯所反对的。对不起,我是新来的。请告知。谢谢,我的意思是在你的问题中只发布代码的相关部分,而不是200多行。嗨@BZngr谢谢你的详细解释和代码。
Shipped
现在运行得很好,但是,我的IF语句现在有一个问题。My Day列不会返回任何值,除非Sales和Production单元格具有Rollup和Rollup状态。当我输入其他单元格的销售和生产状态时,它们都是空白的。你知道为什么吗?非常感谢您到目前为止的帮助。@JaneH根据提供的信息,我认为这是我所能提供的最大帮助。此时,我建议您删除我的更改