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
Related
I have a interactive workbook with 3 step-by-step buttons so the user can generate a report. I want 2nd macro to check if 1st macro was run and if not, warn the user with a Masgbox to run it first.
Is there a way to determine if a macro was run? Maybe putting a Call at the end of the first 2 macros where a public sub add +1 to a 'counter' variable?
For example:
Macro 2 check if counter = 1,
Macro 3 check if counter = 2
Thanks in advance.
Run Macros Sequentially
Public RunChecker As Long
Sub Macro1()
Select Case RunChecker
Case 1
MsgBox "You already ran Macro1. To continue, run Macro2.", vbExclamation
Exit Sub
Case 2
MsgBox "To continue, run Macro3.", vbCritical
Exit Sub
End Select
' Your code, e.g.:
MsgBox "Running1", vbInformation
RunChecker = 1
End Sub
Sub Macro2()
Select Case RunChecker
Case 0
MsgBox "You need to run Macro1 first.", vbCritical
Exit Sub
Case 2
MsgBox "You already ran Macro2. To continue, run Macro3.", vbExclamation
Exit Sub
End Select
' Your code, e.g.:
MsgBox "Running2", vbInformation
RunChecker = 2
End Sub
Sub Macro3()
Select Case RunChecker
Case 0
MsgBox "You already finished. To start again, run Macro1.", vbExclamation
Exit Sub
Case 1
MsgBox "You need to run Macro2 first.", vbCritical
Exit Sub
End Select
' Your code, e.g.:
MsgBox "Running3", vbInformation
RunChecker = 0
End Sub
I have encountered this problem. The first row is highlighters. How can I solve it.
Here is the code :
Sub DeleteSquare()
If (Selection.Name <> "Square") Then
MsgBox ("Not a Square!")
Exit Sub
End If
If (MsgBox("Are you sure?", vbYesNo, "Confirmation") = vbYes) Then
Selection.DeletenumSquares = numSquares - 1
Range("NoOfSquares").Value = numSquares
End If
End Sub
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
I cannot seem to get this code to work... I am trying to basically say, if the combo box changes and the range is not empty ask if the user really does want to clear the range and make the change. If they say no, then undo the combobox change back to what it was. My options for the Sub do not display a BeforeUpdate, only the Change and some others.
I tried to capture the value beforehand and set it back to that, but it doesnt work.
Any ideas welcome!!
Sub ComboBox_UPC_C18_Change()
Dim ComboBox_UPC_C18_Value As String
Dim Location As Range
Set Location = Range("Location_C18")
If Application.WorksheetFunction.CountA(Location) = 0 Then
Exit Sub
ElseIf MsgBox("Are you sure you want change the UPC (clearing the row's data)?", vbYesNo, "User Confirmation") = vbYes Then
Location.Value = ""
ElseIf MsgBox("Are you sure you want change the UPC (clearing the row's data)?", vbYesNo, "User Confirmation") = vbNo Then
ComboBox_UPC_C18.Value = ComboBox_UPC_C18_Value
MsgBox (ComboBox_UPC_C18_Value)
Exit Sub
End If
ComboBox_UPC_C18_Value = ComboBox_UPC_C18.Value
MsgBox (ComboBox_UPC_C18_Value)
End Sub
I wasn't able to test this but I think this is close to what you are looking for. Let me know if I am totally off base here:
Private Sub ComboBox_UPC_C18_Change()
Dim RngLocation As Range
Dim PrevLocation As String
Application.EnableEvents = False
PrevLocation = Range("Location_C18").Value
If PrevLocation = "" Then
Range("Location_C18").Value = ComboBox_UPC_C18.Value
Else
If MsgBox("Are you sure you want to change this?", vbYesNo) = vbYes Then
Range("Location_C18").Value = ComboBox_UPC_C18.Value
Else
ComboBox_UPC_C18.Value = PrevLocation
End If
End If
Application.EnableEvents = True
End Sub
I have the following portion of the code for a given active worksheet:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh As Worksheet
Dim myMessage
If ActiveSheet.Range("I5").Value > 0 Then
MsgBox "Some cells are empty" _
& Chr(13) & Chr(13) & "Please, fill in the empty cells to proceed", _
vbExclamation + vbOKOnly, "ERROR!"
Cancel = True
Else
ThisWorkbook.Unprotect ("123")
Worksheets("Instructions").Visible = True
Worksheets("Instructions").Activate
For Each sh In Worksheets
If sh.Name <> "Instructions" Then
sh.Visible = xlVeryHidden
End If
Next sh
ThisWorkbook.Protect ("123")
myMessage = MsgBox("Do you want to save and close this file?", vbQuestion + vbYesNo, "Workbook 1")
If myMessage = vbYes Then
Application.DisplayFormulaBar = True
ThisWorkbook.Close True
ElseIf myMessage = vbNo Then
Cancel = True
Else
'nothing
End If
End If
End Sub
In general, upon pressing the close button on the workbook itself, the idea is to check a given cell of a given active worksheet for a non-zero value. If it is non-zero, the user has to fill in certain empty cells first and the workbook is not closed. Alternatively, the "Instruction" worksheet is activated and the user receives a message box of what to do next (by doing this, instead of simply no msgbox and sticking with the usual system "SAVE/NO" prompt, is that I want to eliminate the possibility of a user pressing the "No" button, which will lead to no save being made). The only problem I have is the one-time loop I get when a user selects "Yes" answer: the same message box re-appears (apparently, because the procedure goes through the code line again!).
I would appreciate if you could suggest me the way I could avoid this loop.
If you are not going to allow the user not to save then why not force a save. You can replace the entire message box prompt section:
myMessage = MsgBox("Do you want to save and close this file?", vbQuestion + vbYesNo, "Workbook 1")
If myMessage = vbYes Then
Application.DisplayFormulaBar = True
ThisWorkbook.Close True
ElseIf myMessage = vbNo Then
Cancel = True
Else
'nothing
End If
with
ThisWorkbook.Save
This will force the save and close the workbook.