Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Open Form if worksheet is empty

Posted on 2009-07-07
8
Medium Priority
?
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

664 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