Problem with IF coding

neildex

New Member
Joined
Dec 7, 2014
Messages
5
I am having a problem when a macro runs. IF I don't want to delete order data, it should do nothing and return to the front sheet. IF I do want to delete data, it runs the correct process. At the end of my coding under ELSE, it will show a dialog box advising a user to re-enter a year, but it shows this if I choose the option not to delete data. Where have I gone wrong? I babe even tried placing END IF before ELSE, but this does not work and presents an error message.

Here is my code:

Dim Answer As String
Dim MyNote As String
Dim vbNo As String


'Place your text here
MyNote = "that you want to DELETE ALL orders logged for the year?"


'Display MessageBox
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Please confirm...")


If Answer = vbNo Then
'Code for No button Press
Sheets("Front").Select


Else
'Code for Yes button Press


Sheets("Jan").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Feb").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Mar").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Apr").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("May").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Jun").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Jul").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Aug").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Sep").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Oct").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Nov").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Dec").Select
Range("B11:H38").ClearContents
Range("J11:M38").ClearContents
Range("O11:Q28").ClearContents
Sheets("Front").Select
Range("L22:L33").ClearContents
Sheets("Front").Select
'Reset Budget
Range("Q22").Select
ActiveCell.Value = 0
Range("Q23").Select
ActiveCell.Value = 0
Range("Q24").Select
ActiveCell.Value = 0
Range("Q25").Select
ActiveCell.Value = 0
Range("Q26").Select
ActiveCell.Value = 0
Range("Q27").Select
ActiveCell.Value = 0
Range("Q28").Select
ActiveCell.Value = 0
Range("Q29").Select
ActiveCell.Value = 0
Range("Q30").Select
ActiveCell.Value = 0
Range("Q31").Select
ActiveCell.Value = 0
Range("Q32").Select
ActiveCell.Value = 0
Range("Q33").Select
ActiveCell.Value = 0


'Reset year
Range("B8:D8").Select
ActiveCell.FormulaR1C1 = ""
Range("B8:D8").Select
MsgBox "Please enter year again, in the top left of the worksheet."
End If




End Sub
 

mole999

Moderator
Joined
Oct 23, 2004
Messages
9,899
I have to wonder why you want the macro to run to then decide you don't want the action. How about you create only the clear sheet macro, and give yourself a second chance before you commit to the action.

Not elegant, but your clear routines could be


with Sheets("Jan")
.Range("B11:H38,J11:M38,O11:Q28").ClearContents
end with
 

neildex

New Member
Joined
Dec 7, 2014
Messages
5
It is not just me that uses this spreadsheet, others use it on their systems and I just wanted confirmation as to whether a user really did want to delete data, just in case they accidentally clicked on the button to action this.

Would the With coding bring up a dialog box confirming a user wanted to really delete data?
 
Last edited:

mole999

Moderator
Joined
Oct 23, 2004
Messages
9,899
ah I hadn't realised you were only going to call this from a button
 

Forum statistics

Threads
1,082,385
Messages
5,365,151
Members
400,825
Latest member
Sreekanth_21

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top