VBA-如果单元格值更改,则移动行

时间:2018-06-28 10:45:31

标签: excel vba excel-vba

如果我更改特定行中的值,VBA是否有办法使特定范围内的行移动?

在图片中您可以看到我的桌子:

enter image description here

范围A4:B7中的日期为德语格式(DD.MM.YYYY),范围D3:O3中的标题标签仅为文本格式。

因此,当我将B5中的值更改为01.12.2019时,我希望该行中的单元格从E5:H5移至L5:O5。

这是我到目前为止所拥有的:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lastrow As Long

    lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1

    If Target.Address = "$B$3:" & lastrow Then
        Call Mymacro
    End If
End Sub

但是我真的不知道该怎么写宏。我希望有一个人可以帮助我。谢谢

编辑:

可以解决。感谢所有尝试提供帮助的人:)

这是我现在使用的代码。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim DelRng As Range
    Dim Rng As Range
    Dim rngyear As Range
    Dim rngmonth As Range

    Dim lastrow As Long

    Dim year As String
    Dim month As String

    Dim FAA As Integer
    Dim MFG As Integer
    Dim AGA As Integer

    lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
    Set KeyCells = Range("H11:H" & lastrow)

    year = Format(ActiveCell.Value, "yyyy")
    month = Format(ActiveCell.Value, "mmm")

    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        Set DelRng = Range("N" & Target.Row & ":BI" & Target.Row)
        DelRng.ClearContents

        Set rngyear = ActiveSheet.Range("N1:BI1").Find(What:=year, LookAt:=xlWhole, SearchDirection:=xlPrevious)
        If Not rngyear Is Nothing Then
            Set rngmonth = ActiveSheet.Range(Cells(Target.Row, rngyear.Column), Cells(2, rngyear.Column + 11)).Find(What:=month, LookAt:=xlWhole, SearchDirection:=xlPrevious)
            If Not rngmonth Is Nothing Then
                ActiveSheet.Cells(Target.Row, rngmonth.Column).Value = "DEL"
                If IsEmpty(ActiveSheet.Range("N" & Target.Row)) Then
                    FAA = ActiveSheet.Cells(Target.Row, rngmonth.Column).Find(What:="DEL", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column - 1
                    ActiveSheet.Cells(Target.Row, faa).Value = "FAA"
                End If
                If IsEmpty(ActiveSheet.Range("N" & Target.Row)) Then
                    MFG = ActiveSheet.Cells(Target.Row, rngmonth.Column).Find(What:="DEL", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column - 2
                    ActiveSheet.Cells(Target.Row, faa3).Value = "MFG"
                End If
                If IsEmpty(ActiveSheet.Range("N" & Target.Row)) Then
                    AGA = ActiveSheet.Cells(Target.Row, rngmonth.Column).Find(What:="DEL", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column - 3
                    ActiveSheet.Cells(Target.Row, faa2).Value = "AGA"
                End If
            End If
        End If
    End If
End Sub

0 个答案:

没有答案