1
votes

What it's for: I have a part list that changes per job.

What I am trying to accomplish: If selected cell is not in column A then msgbox prompts user to "select part #" Then only run delete macro if selected cell is in range A9:lastrow

Why only column A: If I can force them to select the cell in column A then I can use ActiveCell.Offset to add info to the msgbox asking them if they are sure that is what they want to delete and include other cells info about the part # they selected.

Above Row 8 is header so that has to be locked out.

Each of my part #'s has 3 rows that's why I run the "EntireRow.Delete" 3 times.

This is what I have now.

Sub DeleteRow()
    If MsgBox("Are you sure you want to delete this part?" & vbNewLine & " " & vbNewLine & ActiveCell.Value & vbNewLine & ActiveCell.Offset(0, 1).Value & vbNewLine & "QTY: " & ActiveCell.Offset(0, 12).Value, vbYesNo) = vbNo Then Exit Sub
    If ActiveCell.Row > 8 Then
    Rows(ActiveCell.Row).EntireRow.Delete
    Rows(ActiveCell.Row).EntireRow.Delete
    Rows(ActiveCell.Row).EntireRow.Delete
    End If
End Sub
3
Wouldn't it be more user friendly to ask the question after deciding that the ActiveCell row was > 8 (and column was =1)? Otherwise they are going to say "Yes" and then you are going to ignore them anyway.YowE3K

3 Answers

1
votes

Following @YowE3K comment, and maybe I am missing something in your post, but I think it should be something short like the code below.

Insead of Exit Sub if the user selects "No", just delete the Rows if he selects "Yes".

Sub DeleteRow()

    If ActiveCell.Row > 8 Then ' <-- first check if not above row 8 (headers)
       If MsgBox("Are you sure you want to delete this part?" & vbNewLine & " " & _
            vbNewLine & ActiveCell.value & vbNewLine & ActiveCell.Offset(0, 1).value _
            & vbNewLine & "QTY: " & ActiveCell.Offset(0, 12).value, vbYesNo) = vbYes Then

            ActiveCell.Resize(3, 1).EntireRow.Delete
        End If           
    End If

End Sub
1
votes

There is no reason for the active cell to be in column A in order to present the information you wish to in the MsgBox so, if that is the only reason you are trying to limit the location of the ActiveCell, you should be able to change your code to something like the following:

Sub DeleteRow()
    If ActiveCell.Row <= 8 Then
        MsgBox "This macro should only be invoked if you are in a non-header row"
    ElseIf MsgBox("Are you sure you want to delete this part?" & vbNewLine & _
                  vbNewLine & _
                  ActiveCell.EntireRow.Cells(1, "A").Value & vbNewLine & _
                  ActiveCell.EntireRow.Cells(1, "B").Value & vbNewLine & _
                  "QTY: " & ActiveCell.EntireRow.Cells(1, "M").Value, _
                  vbYesNo) = vbYes Then
        ActiveCell.EntireRow.Delete
        ActiveCell.EntireRow.Delete
        ActiveCell.EntireRow.Delete
    End If
End Sub
0
votes

Without knowing exactly what your issue is (which by the way you should state what the problem you are having is in your questions), I can only assume you are wanting the user to select a part in your code , and then do some kind of chekc after wards. Check this out:

Dim rNg As Range
Set rNg = Application.InputBox("Select something", "Obtain Range Object", Type:=8)
If target.address "something" then
end if

this should get you going.

For the people who are specially picky about answers without more info - i will delete if it turns out my answer is irrelevant