+ Reply to Thread
Results 1 to 6 of 6

Run IfEmpty Macro Automatically

Hybrid View

rfernandes Run IfEmpty Macro... 01-25-2013, 04:07 PM
HaHoBe Re: Run IfEmpty Macro... 01-26-2013, 05:42 AM
rfernandes Re: Run IfEmpty Macro... 01-30-2013, 12:53 PM
HaHoBe Re: Run IfEmpty Macro... 01-30-2013, 02:36 PM
arlu1201 Re: Run IfEmpty Macro... 01-30-2013, 02:54 PM
rfernandes Re: Run IfEmpty Macro... 02-04-2013, 12:16 PM
  1. #1
    Registered User
    Join Date
    07-27-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    18

    Run IfEmpty Macro Automatically

    Hello,

    I'm trying to get the following macro to run automatically when the cell of interest (A12) is not empty:

    Sub DoStuffIfNotEmpty()
        If Not IsEmpty(Range("A12")) Then
            Sheets("CT2").Visible = True
            Else
            Sheets("CT2").Visible = False
        End If
    End Sub
    Right now, I have another macro that is successfully automatically enabled using: Application.EnableEvents = True

    I'm wondering, does the former code not count as an "event" per se, and if not, is there any way I can still auto-enable it?

    Thanks
    Last edited by arlu1201; 01-30-2013 at 02:53 PM. Reason: Use code tags

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Run IfEmpty Macro Automatically

    Hi, rfernandes,

    what kind of code do you have or use so far?

    Maybe just use the event behind the sheet with Range("A12") as a caller for the event (if the value is inserted manually which you didn´t mention):
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A12")) Is Nothing Then
      Sheets("CT2").Visible = Len(Range("A12")) > 0
    End If
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    07-27-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Run IfEmpty Macro Automatically

    Hi Holger,

    Sorry for the delayed response getting back; thanks for the help!

    I have another worksheet_change event so initially, I ran into compile errors. With a little googling, I was able to get these items working by turning them into Event procedures and my code looks like this:


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Application.ScreenUpdating = False
        Application.EnableEvents = False
         
        EventProc1 Target
        EventProc2 Target
         
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End Sub
    Private Sub EventProc1(ByVal Target As Excel.Range)
    Const WS_RANGE As String = "A1:R60"
    On Error GoTo ErrHandler
    Application.EnableEvents = False
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    Target.Formula = UCase(Target.Formula)
    End If
    ErrHandler:
    Application.EnableEvents = True
    End Sub
    Private Sub EventProc2(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A12")) Is Nothing Then
      Sheets("CT2").Visible = Len(Range("A12")) > 0
    End If
    End Sub
    This works, however, it requires that after I manually insert a value into "A12", I must select the A12 cell in order for the macro to run and sheet "CT2" to become visible. Is there any way for excel to notice I have entered in a value without having to select the cell post data entry?
    Last edited by arlu1201; 01-30-2013 at 02:54 PM. Reason: Use code tags

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: Run IfEmpty Macro Automatically

    Hi, rfernandes,

    Is there any way for excel to notice I have entered in a value without having to select the cell post data entry?
    That´s what the Worksheet_Change-event does. And I wonder why you post a lot of code that doesn´t do what you want it to do on it´s own but not what you have in your Change-event. You can check for any range by intersect in your second procedure but I would start it on thre event that to my knowledge was created for the purpose.

    Please, use Code-Tags as per Foum Rule #3 to display the code segments.

    Ciao,
    Holger

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,168

    Re: Run IfEmpty Macro Automatically

    rfernandes,

    I have added code tags to your posts 1 & 3. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. If you need more information on how to use them, check my signature below this post.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Registered User
    Join Date
    07-27-2012
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Run IfEmpty Macro Automatically

    Thank you both for the further direction. Will use those code tags from now on..

    Also I figured out where I was going wrong with regard to my last inquiry. My problem was that
    after I manually insert a value into "A12", I must select the A12 cell in order for the macro to run and sheet "CT2" to become visible
    My problem occured because I was using a Worksheet_SelectionChange event instead of your recommendation, a Worksheet_Change event.

    Thank you for all your help on this problem, as well as the guidance on forum rules. Here is the successful code; this thread is SOLVED!

    Private Sub Worksheet_Change(ByVal Target As Range)
        Application.ScreenUpdating = False
        Application.EnableEvents = False
         
        EventProc1 Target
        EventProc2 Target
         
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End Sub
    Private Sub EventProc1(ByVal Target As Excel.Range)
    Const WS_RANGE As String = "A1:R60"
    On Error GoTo ErrHandler
    Application.EnableEvents = False
    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    Target.Formula = UCase(Target.Formula)
    End If
    ErrHandler:
    Application.EnableEvents = True
    End Sub
    Private Sub EventProc2(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A12")) Is Nothing Then
      Sheets("CT2").Visible = Len(Range("A12")) > 0
    End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1