Delete Data from a Worksheet If Selection from Dropdown List is Changed

前端 未结 1 2021
情歌与酒
情歌与酒 2021-01-26 05:30

Follow up question to a previously answered question: Excel VBA - Run a macro based on a range of dropdown lists.

Current: This is for a personal expense spreadsheet an

1条回答
  •  花落未央
    2021-01-26 06:14

    Try this. You probably need to tweak it a bit, but it should get you going. I have added a global variable that you can store the previous value from the dropdown list.
    In the SelectionChange I have tried to create an error handling to take care of multiple cells selected. If just 1 cell selected then that value will be bound to the global variable. Then you can use that variable to find the sheet of the previous value in the dropdown list, loop through the sheet, and delete the value.

    First I have added this to your Gas, Power, etc. subs. to make them dynamic.

    Sub Power(c As Range)
    
        Dim rng As Range
    
        Set rng = Nothing
        Set rng = Range("A" & c.Row & ":F" & c.Row) '<< A1:F1 here is *relative to c.EntireRow*
    
        'copy the values
        With Worksheets("Power").Cells(Rows.Count, 1).End(xlUp)
            .Offset(1, 0).Resize(1, rng.Cells.Count).Value = rng.Value
    
            ' Copy formating from Master Sheet
            With Worksheets("Master")
                Range("A" & c.Row & ":F" & c.Row).Copy
            End With
            .Offset(1, 0).PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
    
        End With
    
    End Sub
    

    Under the Master sheet (not module), I have added this:

    ' Add this to the absolute top of the sheet, must be outside a procedure (sub)
    Option Explicit
    Public cbxOldVal As String
    Dim PrevVal As Variant
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Rows.Count > 1 Then Exit Sub
    If Target.Columns.Count > 1 Then Exit Sub
    
    cbxOldVal = Target.Value
    End Sub
    
    Private Sub Worksheet_Activate()
        If Selection.Rows.Count = 1 And Selection.Columns.Count = 1 Then
            PrevVal = Selection.Value
        Else
            PrevVal = Selection
        End If
    End Sub
    

    Add this to your Worksheet_Change event.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, c As Range
    Set rng = Intersect(Target, Range("G2:G1001"))
    
    If Not Intersect(Target, Columns("G")) Is Nothing Then
        If PrevVal <> "" Or cbxOldVal <> "" Then
            If cbxOldVal = Target.Value Then
                MsgBox "You have to click on another cell " & vbNewLine & "and then click back on " & Target.Address & " to change the value", vbExclamation, "Error"
                Cells(Target.Row, Target.Column) = PrevVal
                Exit Sub
            ElseIf Target.Value = "" Or Target.Value = PrevVal Then Exit Sub
            End If
        End If
    End If
    
    If Not rng Is Nothing Then
    ' Your loop
    

    Then I have added some code to your Worksheet_Change event. Add this to after the End Select.

        If cbxOldVal = "" Then
        ' do nothing
    
        Else
    
            With Worksheets(cbxOldVal)
    
                Dim i As Integer
                Dim strFindA As String, strFindB As String, strFindC As String
                Dim strFindD As String, strFindE As String, strFindF As String
                strFindA = Sheets("Master").Range("A" & c.Row)
                strFindB = Sheets("Master").Range("B" & c.Row)
                strFindC = Sheets("Master").Range("C" & c.Row)
                strFindD = Sheets("Master").Range("D" & c.Row)
                strFindE = Sheets("Master").Range("E" & c.Row)
                strFindF = Sheets("Master").Range("F" & c.Row)
    
                For i = 1 To 100    ' replace with lastrow
    
                If .Cells(i, 1).Value = strFindA _
                And .Cells(i, 2).Value = strFindB _
                And .Cells(i, 3).Value = strFindC _
                And .Cells(i, 4).Value = strFindD _
                And .Cells(i, 5).Value = strFindE _
                And .Cells(i, 6).Value = strFindF _
                Then
    
                .Rows(i).EntireRow.Delete
                MsgBox "deleted row " & i
                GoTo skip:
    
                End If
    
                Next i
    
    
            End With
        End If
    skip:
    

    0 讨论(0)
提交回复
热议问题