Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 411
Default How to: if a range is not empty don't allow close?

Hi microsoft.public.excel.programming group,

I have a command button the copies cells to a text file.

Sometimes the data has not been saved to the text file.

I suspect the users have been closing the workbook before pressing the
command button.

So I tried to this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

_____________________________________
With Worksheets("CompleteQuote")
Dim rng As Range
Set rng = Range("test_range")
If rng.Value 0 Then
Cancel = True
MsgBox ("You still have data that has not been exported. Please
cancel.")
Else: Cancel = False
End If
End With
End Sub
_____________________________________


This returns runtime error 13. Type Mismatch on
the following line:
If rng.Value 0 Then

The range will contain some cells with data and some cells will have
no data.

Please offer any suggestions or questions.

Thanks,

Dan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default How to: if a range is not empty don't allow close?

Not sure what type data you have in the range, but if it is all numeric, then
this should do the trick:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

_____________________________________
With Worksheets("CompleteQuote")
Dim rng As Range
Set rng = Range("test_range")
If Application.WorksheetFunction.Sum(rng) 0 Then
Cancel = True
MsgBox ("You still have data that has not been exported. Please
cancel.")
Else: Cancel = False
End If
End With
End Sub



"dan dungan" wrote:

Hi microsoft.public.excel.programming group,

I have a command button the copies cells to a text file.

Sometimes the data has not been saved to the text file.

I suspect the users have been closing the workbook before pressing the
command button.

So I tried to this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

_____________________________________
With Worksheets("CompleteQuote")
Dim rng As Range
Set rng = Range("test_range")
If rng.Value 0 Then
Cancel = True
MsgBox ("You still have data that has not been exported. Please
cancel.")
Else: Cancel = False
End If
End With
End Sub
_____________________________________


This returns runtime error 13. Type Mismatch on
the following line:
If rng.Value 0 Then

The range will contain some cells with data and some cells will have
no data.

Please offer any suggestions or questions.

Thanks,

Dan

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 411
Default How to: if a range is not empty don't allow close?

Thanks JLG Whiz.

I'll look into this tomorrow.

Dan
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default How to: if a range is not empty don't allow close?

Try this

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim rng As Range
With Worksheets("CompleteQuote")
Set rng = Nothing
On Error Resume Next
Set rng = .Range("test_range")
On Error GoTo 0
If Not rng Is Nothing Then
Cancel = True
MsgBox ("You still have data that has not been exported. Please
cancel.")
Else
Cancel = False
End If
End With
End Sub


--
HTH,
Barb Reinhardt



"dan dungan" wrote:

Hi microsoft.public.excel.programming group,

I have a command button the copies cells to a text file.

Sometimes the data has not been saved to the text file.

I suspect the users have been closing the workbook before pressing the
command button.

So I tried to this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

_____________________________________
With Worksheets("CompleteQuote")
Dim rng As Range
Set rng = Range("test_range")
If rng.Value 0 Then
Cancel = True
MsgBox ("You still have data that has not been exported. Please
cancel.")
Else: Cancel = False
End If
End With
End Sub
_____________________________________


This returns runtime error 13. Type Mismatch on
the following line:
If rng.Value 0 Then

The range will contain some cells with data and some cells will have
no data.

Please offer any suggestions or questions.

Thanks,

Dan

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 411
Default How to: if a range is not empty don't allow close?

Thanks Barb,

I'll look into this tomorrow.

Dan


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,355
Default How to: if a range is not empty don't allow close?

I think I'd change the if statement to
If not isempty(rng) then
--
HTH,
Barb Reinhardt



"dan dungan" wrote:

Thanks Barb,

I'll look into this tomorrow.

Dan

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 411
Default How to: if a range is not empty don't allow close?

Hi Barbara,

I agree. The procedure as previously written returned the message box
message every time I tried to close the workbook.

Thanks again to both you and JLGWhiz.

Dan
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Emailing range in Outlook- so close! just need a little help Keith Excel Programming 5 December 21st 06 05:13 AM
Determine if a value in a range is close to the value of another c Raven Excel Worksheet Functions 0 June 7th 06 07:47 PM
Determine if a value in a range is close to the value of another c Marcelo Excel Worksheet Functions 0 June 7th 06 07:46 PM
Range ... how to know if it is empty [email protected] Excel Programming 3 January 30th 06 03:23 PM
Finding next empty empty cell in a range of columns UncleBun Excel Programming 1 January 13th 06 11:22 PM


All times are GMT +1. The time now is 02:00 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"