Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open Form if worksheet is empty

Posted on 2009-07-07
8
Medium Priority
?
201 Views
Last Modified: 2012-05-07
Hi there.  I have a form in Excel that I want to appear if the user opens up a worksheet in their workbook that is empty.  I tried putting the code under the Workbook_Activate.  When I F8 through this code it works, but it doesn't work when I'm in my Excel workbook and I click on a sheet to make it active.

Any ideas what I've done wrong? I've commented out my form, and just put a messagebox to appear but that doesn't work either.

Private Sub Workbook_Activate()
Call isWorkSheetEmpty
End Sub
 
Sub isWorkSheetEmpty()    
Dim result As Long
Application.DisplayAlerts = False
 
   result = Application.WorksheetFunction.CountA(ActiveWorkbook.ActiveSheet.Range("a:iv"))
   If result = 0 Then
       ' Call ShowHeaderForm
    MsgBox "empty"
   End If
 
Application.DisplayAlerts = True
 
End Sub

Open in new window

0
Comment
Question by:Fi69
  • 5
  • 2
8 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 24793745
Have you tried using the SheetActivate event?
0
 

Author Comment

by:Fi69
ID: 24793966
Yay that did something, but it errored on this line:

result = Application.WorksheetFunction.CountA(ActiveWorkbook.ActiveSheet.Range("a:iv"))

any ideas why?
0
 

Author Comment

by:Fi69
ID: 24794017
It's okay I worked it out by putting the below.  Thanks so much for your help!!!

Private Sub workbook_SheetActivate(ByVal Sh As Object)

If Sh.Type = xlWorksheet Then
    Call isWorkSheetEmpty
    Else
        'do nothing
    End If
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:Fi69
ID: 31600565
Thanks again!
0
 
LVL 3

Expert Comment

by:Devom
ID: 24794053
You shouldn't need the SheetActivate command for what you're using, the code works fine on my excel.  Does it matter where your code is? Does it have to work on different workbooks?  

Try putting the code in a worksheet and changing the line where you set result to be
result = Application.WorksheetFunction.CountA(Me.Range("a:iv"))
0
 

Author Comment

by:Fi69
ID: 24794104
I'm only a newbie. My Workbook is an XLT (template). When a user clicks on a sheet in the open workbook that has nothing in it I want my form to open up to prompt them to do something.  If I put it in a worksheet, how will this be activated, ie will it open up automatically when they click on an empty worksheet, or would they specifically have to run the code?
0
 
LVL 3

Expert Comment

by:Devom
ID: 24794281
I misread the problem here and didn't notice we were working with events :/.  The Workbook_SheetActivate event is exactly what you needed, sorry for the confusion.
0
 

Author Comment

by:Fi69
ID: 24794360
No problem. Thanks for helping.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question