合并两个Private Sub Worksheet_Changes

时间:2016-11-08 18:41:50

标签: excel-vba vba excel

我正在尝试合并以下2个代码。任何帮助将不胜感激

Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
  If Intersect(Target, Me.Range("D:D")) Is Nothing Then Exit Sub
  For Each C In Intersect(Target, Me.Range("D:D")).Cells
    If C.Text = "y" Then
      C.EntireRow.Copy Worksheets("Shipped").Cells(Rows.Count, "D").End(xlUp).Offset(1).EntireRow
      C.EntireRow.Delete
    End If
  Next
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Column <> 8 Or Target.Cells.Count > 1 Then Exit Sub
    Dim SortRange As Range
    Set SortRange = Range(("A1"), Cells(Rows.Count, 8).End(xlUp))
           SortRange.Sort Key1:=Range("H2"), Order1:=xlAscending, Header:=xlYes
End Sub

1 个答案:

答案 0 :(得分:1)

Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range, SortRange as Range
  If Not Intersect(Target, Me.Range("D:D")) Is Nothing Then
      For Each C In Intersect(Target, Me.Range("D:D")).Cells
        If C.Text = "y" Then
          C.EntireRow.Copy Worksheets("Shipped").Cells(Rows.Count, "D").End(xlUp).Offset(1).EntireRow
          C.EntireRow.Delete
        End If
      Next
  End If
'End Sub

'Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Column = 8 And Target.Cells.Count <= 1 Then
    Set SortRange = Range(("A1"), Cells(Rows.Count, 8).End(xlUp))
    SortRange.Sort Key1:=Range("H2"), Order1:=xlAscending, Header:=xlYes
  End If
End Sub

这些代码只会在原始Sub的反面为真时运行,而不是If StatementsIf Statements一起存在。我还评论了第一个Sub的{​​{1}}声明声明和第一个Sub的{​​{1}}声明。这是你想要的吗?

相关问题