VBA Delete Cells with MsgBox? - excel

I am trying to delete cells in a table.
The worksheet line is marked as debug.
but i can't find my mistake...
Code:
Sub clear_click()
If MsgBox("Del?", vbOKCancel) = vbCancel Then
Exit Sub
Else
Worksheets("calc").Range("G9:G12").ClearContents
End If
End Sub

Related

VBA: Workbookfunction if Dropdown Changes Call Sub with different sheets

I have a worksheet function that automatically calls CopyValues once dropdown in E4 changes on Sheet Debt Detail. This macro only runs if the sheet name is "Debt Detail" otherwise it is going to Exit Sub. This macro worked well until now. I added another sheet called "Borrower Statement", which is supposed to call BorrowerStatementCall if E4 changes in Sheet Borrower Statement.
I need to modify the existing Workbook function to accomplish this.
Below is the existing code. Any help and suggestions on how to accomplish this would be appreciated:
Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
Application.ScreenUpdating = False
If sh.Name <> "Debt Detail" Then Exit Sub
If Target.Address = Range("$E$4").Address Then
Call CopyValues
Range("A1").ClearOutline
Range("d2").Select
End If
Application.ScreenUpdating = True
End Sub
Some suggestions on your code:
Indent your code
Use at least some error handling if you're turning off screenupdating and other stuff
No need for the Call statement
Code:
Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
On Error GoTo CleanFail
' Exit if modified cell is not E4
If Target.Address <> "$E$4" Then Exit Sub
' Turn off stuff to speed up process (only if modified cell is E4)
Application.ScreenUpdating = False
' Check the sheet name and call procedure accordingly
Select Case sh.Name
Case "Debt Detail"
' Do stuff if it's the target sheet
CopyValues ' Calls the sub CopyValues
sh.Range("A1").ClearOutline
sh.Range("D2").Select
Case "Borrower Statement"
' Do stuff if it's the target sheet
BorrowerStatementCall ' calls the sub BorrowerStatementCall
End Select
CleanExit:
' Turn on stuff again
Application.ScreenUpdating = True
Exit Sub
CleanFail:
MsgBox "An error occurred:" & Err.Description
GoTo CleanExit
End Sub
Let me know if it works

if range is Nothing Exit sub, userform, specialcells

Goal: Search range for cells containing errors, if found a modeless useform shows and allows you to change the sell to "yes", "no", or "Review later". If no cells with errors are found in range, msgbox appears to let you know, hide userform and exit sub.
Problem: I can not get the If range is nothing hide userform and exit sub to work properly. Whenever I reach the point where all the cell errors are dealt with I get a 1004 error on the range "no cells were found".
Sub UserformYes_no_review()
Dim Custchk As CustomListCheck
Set Custchk = VBA.UserForms.Add(CustomListCheck.Name)
With New CustomListCheck
Set CheckRange = Sheets("Sheet1").Range("A1:N2000").SpecialCells(xlCellTypeFormulas, xlErrors)
If CheckRange Is Nothing Then
MsgBox "All items have been accounted for"
CustomListCheck.Hide
Exit Sub
Else
For Each Cell In CheckRange
Cell.Select
If VarType(ActiveCell.Value) = vbError Then
Custchk.Show vbModeless
End If
Next Cell
End If
End With
End Sub
Private Sub CommandButton1_Click()
ActiveCell.Value = "Yes"
Call UserformYes_no
End Sub
Private Sub CommandButton2_Click()
ActiveCell.Value = "No"
Call UserformYes_no
End Sub
Private Sub CommandButton3_Click()
ActiveCell.Value = "Review Later"
Call UserformYes_no
End Sub
I have looked through a wealth of Stackoverflow pages and tried all the solutions that i could find and nothing is working.
As a side note, I used a userform over a msgbox as I needed this to be modeless.
When ever you are working with SpecialCells, use error handling.
Change
Set CheckRange = Sheets("Sheet1").Range("A1:N2000").SpecialCells(xlCellTypeFormulas, xlErrors)
to
On Error Resume Next
Set CheckRange = Sheets("Sheet1").Range("A1:N2000").SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
This solved the problem. There is still an issue where the userform will not hide, but I will post another question for this.
Sub UserformYes_no_review()
Dim Custchk As CustomListCheck
Set Custchk = VBA.UserForms.Add(CustomListCheck.Name)
Set CheckRange = Nothing
With New CustomListCheck
On Error Resume Next
Set CheckRange = Sheets("Sheet1").Range("A1:N2000").SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If CheckRange Is Nothing Then
MsgBox "All items have been accounted for"
CustomListCheck.Hide
Exit Sub
Else
For Each Cell In CheckRange
Cell.Select
If VarType(ActiveCell.Value) = vbError Then
Custchk.Show vbModeless
End If
Next Cell
End If
End With
End Sub

How to proceed when object is not found?

I am searching for a blank cell in a table. Want to have a msg or run a command when there is no blank cell. I tried below versions but none of them worked
Sub Macro1()
ActiveSheet.ListObjects("Tabel1").DataBodyRange.Select
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo Line1
Line1:
MsgBox "no blank cell is found"
End Sub
and also this one
Sub Macro1()
ActiveSheet.ListObjects("Tabel1").DataBodyRange.Select
Selection.SpecialCells(xlCellTypeBlanks).Select
If Selection = "" Then
MsgBox "no blank cell is found"
End If
End Sub
I suggest to catch the error and check if BlankCells is Nothing.
Sub Macro1()
Dim BlankCells As Range
On Error Resume Next 'supress all error messages until …Goto 0
Set BlankCells = ActiveSheet.ListObjects("Tabel1").DataBodyRange.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0 'never forget to re-activate error reporting immedeately!
If BlankCells Is Nothing Then
MsgBox "no blank cell is found"
Else
MsgBox BlankCells.Cells.Count & " blank cell(s) found"
End If
End Sub
You might benefit from reading …
How to avoid using Select in Excel VBA.
VBA Error Handling – A Complete Guide
No need for selects and selection, you could try:
Sub try()
On Error GoTo noblanks
MsgBox ActiveSheet.ListObjects("Tabel1").DataBodyRange.SpecialCells(xlCellTypeBlanks).Count & " blank cells are found"
Exit Sub
noblanks:
MsgBox "no blank cell is found"
End Sub

.range(ContRow&":"&ContRow).entirerow.Delete

This is the line I'm having trouble with:
.range(ContRow&":"&ContRow).entirerow.Delete
This is the macro:
Sub Cont_Delete()
With Sheet1
If MsgBox("Are you sure you want to delete this record?", vbYesNo, "Delete
Record") = vbNo Then Exit Sub
If .Range("B3").Value = Empty Then Exit Sub
ContRow = .Range("B3").Value.Range(ContRow&":"&ContRow).EntireRow.Delete.Range("D18").Select
End With
End Sub
Error message:
Syntax error and compile error expected: list separator or )
you are concatenating code lines
you most probably need this:
Sub Cont_Delete()
With Sheet1
If IsEmpty(.Range("B3")) Then Exit Sub
If MsgBox("Are you sure you want to delete this record?", vbYesNo, "Delete Record ") = vbNo Then Exit Sub
.Rows(.Range("B3").Value).EntireRow.Delete
.Range("D18").Select
End With
End Sub

excel vba - protetcted sheet delete row

I learned that I need to write a macro if I want users to delete rows on a protected sheet.
This is the code I got by googling around:
Sub delete_row()
ActiveSheet.Unprotect Password:="justme"
ActiveCell.EntireRow.Delete
ActiveSheet.Protect Password:="justme"
End Sub
Where exactly should I place this code? Will it work if multiple rows are deleted etc.?
MrExcel is down today, so limited options.
Paste this in a module
Option Explicit
Sub DeleteMe()
Dim Ret As Range, Cl As Range
On Error Resume Next
Set Ret = Application.InputBox("Please select the Cells", "Delete Rows", Type:=8)
On Error GoTo 0
ActiveSheet.Unprotect Password:="justme"
If Not Ret Is Nothing Then Ret.EntireRow.Delete
ActiveSheet.Protect Password:="justme"
End Sub
When you run the above macro it will ask you to select the cell(s). Whatever cells you select, the entire row will get deleted.

Resources