How do I add a "do you wish to continue" warning message and bring blanks to bottom of table when creating a clear button for certain cell values?

cheesypoofs

New Member
Joined
Mar 7, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I am making a funnel management tool and I am trying to use .AutoFilter to clear contents in a row on Table 1 if it states "installed", "scheduled", or "cancelled in the "stage" column. I do not want to delete the rows entirely because I want to keep the number of rows in the table as 100. I also want to add a warning message when the button is clicked to confirm that the user wants to run this macro since running the macro a second time with if there are no rows to clear ends up making a mess of the table and would confuse users.

This is the code I am currently trying to build on
VBA Code:
Sub ClearSold()

Dim dRng As Range
With ActiveSheet.ListObjects("Table1")
    .Range.AutoFilter Field:=7, Criteria1:=Array("Cancelled", "Installed", "Scheduled"), Operator:=xlFilterValues
    If WorksheetFunction.Subtotal(2, .DataBodyRange) > 0 Then
        Set dRng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
        .Range.AutoFilter
        dRng.ClearContents
    End If
    
End With

End Sub
 

Attachments

  • Funnel Management Example.JPG
    Funnel Management Example.JPG
    160.4 KB · Views: 12

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
since running the macro a second time with if there are no rows to clear ends up making a mess of the table and would confuse users
The way to avoid that is to avoid the the clear operation unless there is something to clear (not tested):
VBA Code:
Sub ClearSold()

    Dim dRng As Range
    With ActiveSheet.ListObjects("Table1")
        .Range.AutoFilter Field:=7, Criteria1:=Array("Cancelled", "Installed", "Scheduled"), Operator:=xlFilterValues
        If WorksheetFunction.Subtotal(2, .DataBodyRange) > 0 Then
            On Error Resume Next
            Set dRng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not dRng Is Nothing Then
                .Range.AutoFilter
                dRng.ClearContents
            End If
        End If
    End With

End Sub
 
Upvote 0
This still gives the same issues once clicked twice and it does not move blanks to the below the other rows that are still there sadly... Do you know how I would remedy this?
 

Attachments

  • Double Click Clear Sold.JPG
    Double Click Clear Sold.JPG
    150.1 KB · Views: 8
  • Blank Click Clear Sold.JPG
    Blank Click Clear Sold.JPG
    211.9 KB · Views: 6
Upvote 0
The way to avoid that is to avoid the the clear operation unless there is something to clear (not tested):
VBA Code:
Sub ClearSold()

    Dim dRng As Range
    With ActiveSheet.ListObjects("Table1")
        .Range.AutoFilter Field:=7, Criteria1:=Array("Cancelled", "Installed", "Scheduled"), Operator:=xlFilterValues
        If WorksheetFunction.Subtotal(2, .DataBodyRange) > 0 Then
            On Error Resume Next
            Set dRng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            If Not dRng Is Nothing Then
                .Range.AutoFilter
                dRng.ClearContents
            End If
        End If
    End With

End Sub
This still gives the same issues once clicked twice and it does not move blanks to the below the other rows that are still there sadly... Do you know how I would remedy this?
 

Attachments

  • Double Click Clear Sold.JPG
    Double Click Clear Sold.JPG
    150.1 KB · Views: 8
  • Blank Click Clear Sold.JPG
    Blank Click Clear Sold.JPG
    211.9 KB · Views: 8
Upvote 0
Perhaps this.
VBA Code:
Sub ClearSold()
    Dim LO As ListObject
    Dim dRng As Range
    Dim CA As Variant, TA As Variant, N As Variant
    Dim I As Long, J As Long, RRow As Long, RCol As Long
    
    Set LO = ActiveSheet.ListObjects("Table1")
    
    With LO
        .Range.AutoFilter Field:=7, Criteria1:=Array("Cancelled", "Installed", "Scheduled"), Operator:=xlFilterValues
        If WorksheetFunction.Subtotal(2, .DataBodyRange) > 0 Then
            On Error Resume Next
            Set dRng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            .Range.AutoFilter
            If Not dRng Is Nothing Then
                dRng.ClearContents
            End If
        End If
        .Range.AutoFilter
    End With
    
    CA = LO.DataBodyRange.Value
    TA = CA
    I = 0
    For RRow = LBound(CA, 1) To UBound(CA, 1)
        J = 0
        N = Trim(CA(RRow, LBound(CA, 2)))
        If N <> "" Then
            I = I + 1
        End If
        For RCol = LBound(CA, 2) To UBound(CA, 2)
            TA(RRow, RCol) = vbNullString
            N = Trim(CA(RRow, RCol))
            If N <> "" Then
                J = J + 1
                TA(I, J) = CA(RRow, RCol)
            End If
        Next RCol
    Next RRow
    LO.DataBodyRange.Value = TA
End Sub
 
Upvote 0
Solution
Perhaps this.
VBA Code:
Sub ClearSold()
    Dim LO As ListObject
    Dim dRng As Range
    Dim CA As Variant, TA As Variant, N As Variant
    Dim I As Long, J As Long, RRow As Long, RCol As Long
   
    Set LO = ActiveSheet.ListObjects("Table1")
   
    With LO
        .Range.AutoFilter Field:=7, Criteria1:=Array("Cancelled", "Installed", "Scheduled"), Operator:=xlFilterValues
        If WorksheetFunction.Subtotal(2, .DataBodyRange) > 0 Then
            On Error Resume Next
            Set dRng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
            On Error GoTo 0
            .Range.AutoFilter
            If Not dRng Is Nothing Then
                dRng.ClearContents
            End If
        End If
        .Range.AutoFilter
    End With
   
    CA = LO.DataBodyRange.Value
    TA = CA
    I = 0
    For RRow = LBound(CA, 1) To UBound(CA, 1)
        J = 0
        N = Trim(CA(RRow, LBound(CA, 2)))
        If N <> "" Then
            I = I + 1
        End If
        For RCol = LBound(CA, 2) To UBound(CA, 2)
            TA(RRow, RCol) = vbNullString
            N = Trim(CA(RRow, RCol))
            If N <> "" Then
                J = J + 1
                TA(I, J) = CA(RRow, RCol)
            End If
        Next RCol
    Next RRow
    LO.DataBodyRange.Value = TA
End Sub
This worked! Thank you!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top