VBA代码彼此冲突

时间:2020-03-27 15:33:36

标签: excel vba

请告知我如何解决以下代码,以免它们不再相互分类。抱歉,我不得不在这里写下很多代码,但我真的不知道其中哪些使它们相互冲突。

因此,如果在AU列(第47列)中“ Shipped”,则我有这部分返回所有“销售和生产”单元格的“ Rollup”。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim counter As Long
Dim lastcolumn As Long

lastColumn = Me.Cells(1, Me.Columns.Count).End(xlToLeft).Column

'Shipped without Title Transfer
      If Me.Cells(1, Target.Column).Value = "MB51 Shipped" Then
        For counter = 1 To lastColumn
          If (Me.Cells(1, counter).Value = "Sales" Or Me.Cells(1, counter).Value = "Production") _
          And IsEmpty(Me.Cells(Target.Row, counter).Value) Then
            Me.Cells(Target.Row, counter).Value = "Rollup"
          End If
        Next counter
      End If

然后,如果最后一个Sales列具有“ Title Transfer”,我将在AX列(第50列)中返回“ x”。

Dim r As Range, r1 As Range, counter As Long
Dim MaxCol As Variant, rg As Range, j As Long

  If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    Set r = Intersect(Target, Cells(1, 1).CurrentRegion, Columns(Target.Column).Resize(, 3))
    Call DoCells(r)
  End If

'Automatically put "x" if Title Transfer in any Sales columns
  If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    If Target.CountLarge > 1 Then Exit Sub
    Set rg = Range("N" & Target.Row & ":AP" & Target.Row)
    MaxCol = 0
    For j = Columns("AP").Column To Columns("N").Column Step -4
      If Cells(Target.Row, j) <> "" Then
        If j > MaxCol Then MaxCol = j
      End If
    Next
    If MaxCol Mod 4 = 2 Then
      If Cells(Target.Row, MaxCol).Value = "Title Transfer" Then
        Cells(Target.Row, 50).Value = "x"
      Else
        Cells(Target.Row, 50).Value = ""
      End If
    End If
  End If

'This I have 8 Sales Column, however, I only put 1 line down for demonstration
Set r = Intersect(Target, Cells(1, 1).CurrentRegion, Columns(colSales1).Resize(, 3))
    If Not r Is Nothing Then Call DoCells(r)

If Not Intersect(Target, Range("N:AP")) Is Nothing And Target.Column Mod 4 = 2 Then
    If Target.CountLarge > 1 Then Exit Sub
    Set rg = Range("N" & Target.Row & ":AP" & Target.Row)
    MaxCol = Evaluate("=MAX(IF(" & rg.Address & "<>"""",COLUMN(" & rg.Address & ")))")
    If MaxCol Mod 4 = 2 Then
      If Cells(Target.Row, MaxCol).Value = "Title Transfer" Then
        Cells(Target.Row, 50).Value = "x"
      Else
        Cells(Target.Row, 50).Value = ""
      End If
    End If
  End If

End Sub

这是其中一行调用的DoCells子项。

Private Sub DoCells(r As Range)
    Dim r1 As Range
    For Each r1 In r.Cells
        With r1
            Select Case .Column
                Case colSales1, colSales2, colSales3, colSales4, colSales5, colSales6, colSales7, colSales8
                    Call MasterChange(.Resize(1, 3))
                Case colProduction1, colProduction2, colProduction3, colProduction4, colProduction5, colProduction6, colProduction7, colProduction8
                    Call MasterChange(.Offset(0, -1).Resize(1, 3))
                Case colDay1, colDay2, colDay3, colDay4, colDay5, colDay6, colDay7, colDay8
                    Call MasterChange(.Offset(0, -2).Resize(1, 3))
            End Select
        End With
    Next
End Sub

这是我使用Markdown表的数据结构:

| Title  | Engine Family | Market Segment | Customer | Engine Model | S/N | Build Spec | ACTL.FINISH | Sales Order | Item | Committed Date | EPS Date   | Target | Sales | Production | Day 1 | Status | Sales          | Production | Day 2          | Status | Sales   | Production | Day 3   | Status           | Sales   | Production | Day 4   | Status           | Sales   | Production | Day 5   | Status           | Sales   | Production | Day 6   | Status           | Sales   | Production | Day 7   | Status           | Sales   | Production | Day 8   | Status           | Comments | MB51 Shipped | FPS? | Plant | Title Transfer |
|--------|---------------|----------------|----------|--------------|-----|------------|-------------|-------------|------|----------------|------------|--------|-------|------------|-------|--------|----------------|------------|----------------|--------|---------|------------|---------|------------------|---------|------------|---------|------------------|---------|------------|---------|------------------|---------|------------|---------|------------------|---------|------------|---------|------------------|---------|------------|---------|------------------|----------|--------------|------|-------|----------------|
| Rollup | PS            | APU            | ABC      | 46C12        | 1   | BS1        | 0000-00-00  | 101         | 450  | 2019-12-31     | 2019-12-31 | Rollup | Green | Rollup     | Green | Sales  | Title Transfer | Yellow     | Title Transfer | Sales  |         |            |         | Sales/Production |         |            |         | Sales/Production |         |            |         | Sales/Production |         |            |         | Sales/Production |         |            |         | Sales/Production |         |            |         | Sales/Production |          |              |      |       | x              |
| Yellow | PS            | FEP            | ADG      | PS3          | 3   | BS3        | 0000-00-00  | 103         | 180  | 2019-12-16     | 2019-12-20 | Yellow | Green | Rollup     | Green | Sales  | Title Transfer | Yellow     | Title Transfer | Sales  | Rollup  | Rollup     | Rollup  | Sales/Production | Rollup  | Rollup     | Rollup  | Sales/Production | Rollup  | Rollup     | Rollup  | Sales/Production | Rollup  | Rollup     | Rollup  | Sales/Production | Rollup  | Rollup     | Rollup  | Sales/Production | Rollup  | Rollup     | Rollup  | Sales/Production |          | Shipped      |      |       |                |
| Rollup | T6T           | OEM            | FEDS     | 67C          | 5   | BS5        | 0000-00-00  | 105         | 250  | 2019-12-23     | 2019-12-22 | Rollup | Green | Rollup     | Green | Sales  | Title Transfer | Yellow     | Title Transfer | Sales  | Shipped | Rollup     | Shipped | Sales/Production | Shipped | Rollup     | Shipped | Sales/Production | Shipped | Rollup     | Shipped | Sales/Production | Shipped | Rollup     | Shipped | Sales/Production | Shipped | Rollup     | Shipped | Sales/Production | Shipped | Rollup     | Shipped | Sales/Production |          | Shipped      |      |       | x              |

仅供参考,我总共有8天,每一天都是4列的组合,它们的顺序完全相同:销售,生产,日期,状态。范围是从N列到BS列(或实际Excel工作簿中的AS)。

从表中可以看到:

1)第一行完全符合我的要求。它确实正确评估了“销售/列R”(第2天)中的“标题转移”,使其成为最后一个具有“标题转移”的销售列,并在BX列(或我的excel文件中的AX列)中返回了“ x”

2)第二行,代码返回正确和错误的结果。 我已经在“销售”列中首先放置了“标题转移”,然后宏在BX列中返回了“ x”。没错。

但是,当我在BX列中首先放置“标题转移”之后,在BU列中放置了“已发货”时,“ x”被我上面发布的发货代码所代替。当我在BU列(excel文件中的第47列或AU列)中放入“ Shipped”时,它确实为所有空的Sales和Production单元格返回了“ Rollup”。但是表示标题转移的“ x”不见了。

因此,过去一周来,我一直在苦苦挣扎的问题来了。您能告诉我如何解决此问题吗? 3)如果BU列中的“ Shipped”和BX列中的“ x”(显然不起作用),第三行就是我想要我的代码执行的操作

简而言之,我的代码应该执行以下操作:

1)如果BU列中为“ Shipped”(实际Excel文件中为AU),而BX列中(“实际Excel文件”中为AX)中没有“ x”,则对于所有空的Sales和Production单元格返回“ Rollup” < / p>

2)如果最后一个Sales列中的“标题转移”,而BU列中没有“ Shipped”(实际Excel文件中为AU),则在BX列中返回“ x”(实际Excel文件中为AX)

3)如果最后一个“销售”列中的“标题转移”(必须首先发生),而在BU列中的“ Shipped”(以后发生)(在实际的Excel文件中为AU),则在BX列中返回“ x”,然后在“在所有空的“销售和生产”单元中“发货”

您能帮忙如何使其以这种方式工作吗?非常感谢,如果您需要更多信息,请告诉我。

Ps:这是MasterChange的内容

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"
'I have approximately 40 Ifs statements like those but above are just a few for demonstration
    End If
        Application.EnableEvents = True
    End Sub

0 个答案:

没有答案
相关问题