+ Reply to Thread
Results 1 to 14 of 14

VBA code not running automatically

Hybrid View

anwitha VBA code not running... 07-10-2021, 02:00 AM
BadlySpelledBuoy Re: VBA code not running... 07-10-2021, 02:03 AM
anwitha Re: VBA code not running... 07-10-2021, 02:10 AM
BadlySpelledBuoy Re: VBA code not running... 07-10-2021, 02:29 AM
anwitha Re: VBA code not running... 07-10-2021, 02:37 AM
AliGW Re: VBA code not running... 07-10-2021, 02:39 AM
anwitha Re: VBA code not running... 07-10-2021, 02:46 AM
AliGW Re: VBA code not running... 07-10-2021, 02:47 AM
BadlySpelledBuoy Re: VBA code not running... 07-10-2021, 02:48 AM
anwitha Re: VBA code not running... 07-10-2021, 02:54 AM
BadlySpelledBuoy Re: VBA code not running... 07-10-2021, 03:05 AM
anwitha Re: VBA code not running... 07-10-2021, 03:15 AM
BadlySpelledBuoy Re: VBA code not running... 07-10-2021, 03:42 AM
AliGW Re: VBA code not running... 07-10-2021, 03:19 AM
  1. #1
    Forum Contributor
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2019
    Posts
    288

    VBA code not running automatically

    Hi All,

    I have the below code in my vba project but every time when i changed some data in my sheet columns will not get hide automatically but when i go to VBA property window and press F5 changes will be done in my sheet. can you please help me how to make this work automatically without going and pressing F5 in manually everytime.

    Note - I have saved this file as Macro enabled worksheet and its enabled under File>option>trustCentre

    Option Explicit
    
    Sub HideColumns()
    
        Dim Rng As Range, Cell As Range
        
        Application.ScreenUpdating = False
      
        
        Set Rng = Sheets("Schedule").Range("D4:AH4")
        
        Cells.EntireColumn.Hidden = False
        
        For Each Cell In Rng
            If Cell.Value > Sheets("Schedule").Range("A1").Value Then
                Cell.EntireColumn.Hidden = True
       
           End If
        Next Cell
    
        Application.ScreenUpdating = True
        
    End Sub
    Last edited by anwitha; 07-10-2021 at 02:09 AM.

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,951

    Re: VBA code not running automatically

    I can tell you why it's not running automatically, but first you need to stick to the forum rules and wrap the code in your original post in code tags.

    BSB

  3. #3
    Forum Contributor
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2019
    Posts
    288

    Re: VBA code not running automatically

    sorry....this is done now.

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,951

    Re: VBA code not running automatically

    For your code to run automatically it needs a 'trigger'. The way you currently have it set up, that trigger is pressing F5.
    What you want to do is use a worksheet change event in the worksheet module. That way code can be triggered when a specified range is altered.

    If you need more help than that, attach a sample workbook and explain which range(s) are to trigger the code and we can help more from there.

    BSB

  5. #5
    Forum Contributor
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2019
    Posts
    288

    Re: VBA code not running automatically

    I am not able to attach the sample due to restriction in my laptop.

    I have a calendar dates from 1 to 31st which is there in the cell from D4 to AH4...there are some months where we have only 30 days or 28 day in case of february..in such cases this should not show the columns which is beyond the actual end date in a month.....

    but here in my sheet, even if the last working day is 30 its still showing the next column but when i pressed F5 then it will be hidden.

    In the code column A1 refers to last day of the month.
    Last edited by anwitha; 07-10-2021 at 02:43 AM.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    91,064

    Re: VBA code not running automatically

    What restriction in your laptop could possibly be preventing you from uploading a workbook? We are not asking to see real, sensitive data - just a realistic mock-up with a few rows of desensitised data.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Contributor
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2019
    Posts
    288

    Re: VBA code not running automatically

    This is my company provided laptop where in i cannot upload any kind of the documents

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    91,064

    Re: VBA code not running automatically

    That's a pain.

  9. #9
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,951

    Re: VBA code not running automatically

    From your code I can see that cell A1 holds the information on how many days in the month. Is this something you select manually or is it derived by a formula looking elsewhere? If so, where?

    Without a sample workbook there could be lots and lots of questions to get you the result you need. Can you not mock something up from another computer?

    BSB

  10. #10
    Forum Contributor
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2019
    Posts
    288

    Re: VBA code not running automatically

    Below is the formula avaible in cell A1

    =EDATE(DATE(C2,MONTH(1&B2),1),1)-1

    In C2 cell i will update the year manually

    In B2, cell is dropdown list where in i will select the months like January, February and based on this Cell A1 gives me the last date of the month.
    Last edited by anwitha; 07-10-2021 at 03:00 AM.

  11. #11
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,951

    Re: VBA code not running automatically

    It's difficult to know it will work without a sample workbook but we can try.
    Right click on the sheet tab for the "Schedule" sheet and select "View Code".
    Paste the code below in that module then see if it works when you change cells B2 or C2.
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Cells.Count = 1 Then
            If Not Intersect(Target, Range("B2:C2")) Is Nothing Then
                Select Case Day(Application.EoMonth(Range("A1"), 0))
                    Case 30
                        Columns("AF:AG").EntireColumn.Hidden = False
                        Columns("AH").EntireColumn.Hidden = True
                    Case 29
                        Columns("AF").EntireColumn.Hidden = False
                        Columns("AG:AH").EntireColumn.Hidden = True
                    Case 28
                        Columns("AF:AH").EntireColumn.Hidden = True
                    Case Else
                        Columns("AF:AH").EntireColumn.Hidden = False
                End Select
            End If
        End If
    End Sub
    BSB

  12. #12
    Forum Contributor
    Join Date
    10-16-2010
    Location
    bangalore
    MS-Off Ver
    Excel 2019
    Posts
    288

    Re: VBA code not running automatically

    This is working now....Thank you so much for your help

  13. #13
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,951

    Re: VBA code not running automatically

    Quote Originally Posted by anwitha View Post
    This is working now....Thank you so much for your help
    No problem. Happy to help
    Thanks for the feedback.

    Take care.

    BSB

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    91,064

    Re: VBA code not running automatically

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. code works running in visual basic editor but not when running from Excel macro
    By smporco in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-06-2016, 05:44 PM
  2. [SOLVED] How to prevent worksheet code from running while Module Macro is running:
    By Chad Bateman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2015, 09:26 AM
  3. [SOLVED] Excel VB code. Message pops up while code running asking question. Code must not wait.
    By Heinrich Venter in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-28-2014, 06:10 AM
  4. [SOLVED] Automatically returning a date value + 7 days after running some code
    By Skiddedoda in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2013, 05:37 AM
  5. Excel 2007 SP2 Objects Resize automatically when running VBA code
    By trent_19 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-14-2011, 05:32 AM
  6. Date doesn't appear automatically when running date code
    By James12 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-21-2009, 05:20 PM
  7. [SOLVED] Automatically running code
    By Richard in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-07-2006, 05:40 PM

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