nested if statements

tortillachips

New Member
Joined
Aug 9, 2011
Messages
5
I want vba to search a sheet for a value. If the value exists, ask the user if they want to overwrite the values below it. If it does not exist, start a new set of values. The problem lies in my if statements.. If the user selects to overwrite the file. nothing happens. can anyone help?

Private Sub ButtonSaveValues_Click()
Dim irow As Long
Dim SaveFile As String
Dim x As Integer
Dim overwrite As Variant
Dim FoundValue As Range

Start:

SaveFile = InputBox("Enter the name you would like this to be saved under:", "Save File")

Sheets("Save").Activate

Range("A1").Activate



Set FoundValue = Cells.Find(what:=SaveFile, after:=ActiveCell, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False, _
searchformat:=False)


If Not FoundValue Is Nothing Then
overwrite = MsgBox("There is already a file under this name, would you like to overwrite it?", vbYesNo)
If overwrite = vbNo Then Call ButtonSaveValues_Click
ElseIf overwrite = vbYes Then
irow = ActiveCell.Row
Call Saver
End If

Else
irow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

Call Saver

End If
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
That module does not even compile. How can you be running it?
 
Upvote 0
As I stated, I needed help getting it to work. I figured it out... needed an On error resume next. Thanks for the response though.
 
Upvote 0
As I stated, I needed help getting it to work. I figured it out... needed an On error resume next. Thanks for the response though.
If you didn't change anything else in the code you showed us, thenI doubt if that solved your problem rather than just mask it.... you have a syntax error in the code you showed us.

Code:
  If overwrite = vbNo Then [COLOR=darkred]Call ButtonSaveValues_Click
[/COLOR]  ElseIf overwrite = vbYes Then
    iRow = ActiveCell.Row
    Call Saver
  End If
The part marked in red cannot be located where you have it... you cannot mix a single line If..Then statement with block If..Then..Else constructions. If you do it this way, the code should compile (without the need for the On Error statement)...

Code:
  If overwrite = vbNo Then
    Call ButtonSaveValues_Click
  Else
    iRow = ActiveCell.Row
    Call Saver
  End If
Note that I removed your ElseIf statement and replaced it with a simple Else... your MessageBox can only return vbNo and vbYes... if isn't vbNo as your first test checks for, then it can only be vbYes and there is no need to test directly for that.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
rootytrip

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