End if without Block If

  • Thread starter Carrie_Loos via OfficeKB.com
  • Start date
C

Carrie_Loos via OfficeKB.com

I think I am trying to do too much in one line but I am not sure how to write
it correctly. I want the user to be able to click on cell P1 and then a
message box ask if they have completed the input - If yes, then save the file
name and date, if no then end sub. I am getting the error message above and
can't figure it out. Can anyone help?

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Dim ans As Long

If Target.Address = Range("p1").Address Then ans = MsgBox("Are you
finished inputing Daily Info?", vbYesNo)
If ans = vbNo Then ActiveWorkbook.SaveAs Filename:=Range("A1").Value &
Format(Worksheets("Daily").Range("ax1").Value, "yyyy-mm-dd") & ".xls"

End If
End Sub

Thanks
Carrie
 
D

Dave

Hi,
Try removing the End If at the end of the sub.
Your If Then statements are only one line of code each, so the End If is not
required.
Regards - Dave.
 
C

Carrie_Loos via OfficeKB.com

You know, I started off that way but then the macro just doesn't respond, I
can't even get the cursur to move with striking the enter key. So I thought
it was an End If problem but darn it I guess not. Any other suggestions?
Hi,
Try removing the End If at the end of the sub.
Your If Then statements are only one line of code each, so the End If is not
required.
Regards - Dave.
I think I am trying to do too much in one line but I am not sure how to write
it correctly. I want the user to be able to click on cell P1 and then a
[quoted text clipped - 15 lines]
Thanks
Carrie
 
D

Dave Peterson

I'd try:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Dim ans As Long

If Target.Address = me.Range("p1").Address Then
ans = MsgBox("Are you finished inputing Daily Info?", vbYesNo)
If ans = vbNo Then
me.parent.SaveAs Filename:=me.Range("A1").Value & _
Format(me.parent.Worksheets("Daily") _
.Range("ax1").Value, "yyyy-mm-dd") & ".xls"
End if
End If
End Sub

Personally, I avoid those if/then one liners. I like the block if/then/else
structures.

And you sure you want to check to see if the user hit the No button?

me is the worksheet with the code.
me.parent is the workbook that owns the worksheet with the code. I like that
better than using Activeworkbook.



Carrie_Loos via OfficeKB.com said:
You know, I started off that way but then the macro just doesn't respond, I
can't even get the cursur to move with striking the enter key. So I thought
it was an End If problem but darn it I guess not. Any other suggestions?
Hi,
Try removing the End If at the end of the sub.
Your If Then statements are only one line of code each, so the End If is not
required.
Regards - Dave.
I think I am trying to do too much in one line but I am not sure how to write
it correctly. I want the user to be able to click on cell P1 and then a
[quoted text clipped - 15 lines]
Thanks
Carrie
 
D

Dave

Hi
After removing the End If, put a breakpoint in at the first line of code.
Then when you change the value of P1, the VB window should appear, and from
there you can 'F8' through the code to see what's happening (or not
happening).
Dave

Carrie_Loos via OfficeKB.com said:
You know, I started off that way but then the macro just doesn't respond, I
can't even get the cursur to move with striking the enter key. So I thought
it was an End If problem but darn it I guess not. Any other suggestions?
Hi,
Try removing the End If at the end of the sub.
Your If Then statements are only one line of code each, so the End If is not
required.
Regards - Dave.
I think I am trying to do too much in one line but I am not sure how to write
it correctly. I want the user to be able to click on cell P1 and then a
[quoted text clipped - 15 lines]
Thanks
Carrie
 
G

Gord Dibben

Try this revision.

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Dim ans As String

If Target.Address = Range("P1").Address Then
ans = MsgBox("Are you finished inputing Daily Info?", vbYesNo)
If ans = vbYes Then
ActiveWorkbook.SaveAs Filename:=Worksheets("Daily").Range("A1").Value & _
Format(Worksheets("Daily").Range("AX1").Value, "yyyy-mm-dd") & ".xls"

End If
End If
End Sub


Gord Dibben MS Excel MVP
 
C

Carrie_Loos via OfficeKB.com

Thanks - This look like it will work. And to answer your question, the "No"
button is in fact backwards should be Yes.

Out of curiosity - why avoid the one liners? Too many issues? or just better
housekeeping? Maybe both?

Dave said:
I'd try:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Dim ans As Long

If Target.Address = me.Range("p1").Address Then
ans = MsgBox("Are you finished inputing Daily Info?", vbYesNo)
If ans = vbNo Then
me.parent.SaveAs Filename:=me.Range("A1").Value & _
Format(me.parent.Worksheets("Daily") _
.Range("ax1").Value, "yyyy-mm-dd") & ".xls"
End if
End If
End Sub

Personally, I avoid those if/then one liners. I like the block if/then/else
structures.

And you sure you want to check to see if the user hit the No button?

me is the worksheet with the code.
me.parent is the workbook that owns the worksheet with the code. I like that
better than using Activeworkbook.
You know, I started off that way but then the macro just doesn't respond, I
can't even get the cursur to move with striking the enter key. So I thought
[quoted text clipped - 15 lines]
 
D

Dave Peterson

The issues are all with me (with the one liners).

I find it easy to miss them when I'm reading existing code.

I guess it's more of a style choice.

Carrie_Loos via OfficeKB.com said:
Thanks - This look like it will work. And to answer your question, the "No"
button is in fact backwards should be Yes.

Out of curiosity - why avoid the one liners? Too many issues? or just better
housekeeping? Maybe both?

Dave said:
I'd try:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Dim ans As Long

If Target.Address = me.Range("p1").Address Then
ans = MsgBox("Are you finished inputing Daily Info?", vbYesNo)
If ans = vbNo Then
me.parent.SaveAs Filename:=me.Range("A1").Value & _
Format(me.parent.Worksheets("Daily") _
.Range("ax1").Value, "yyyy-mm-dd") & ".xls"
End if
End If
End Sub

Personally, I avoid those if/then one liners. I like the block if/then/else
structures.

And you sure you want to check to see if the user hit the No button?

me is the worksheet with the code.
me.parent is the workbook that owns the worksheet with the code. I like that
better than using Activeworkbook.
You know, I started off that way but then the macro just doesn't respond, I
can't even get the cursur to move with striking the enter key. So I thought
[quoted text clipped - 15 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top