+ Reply to Thread
Results 1 to 3 of 3

Application.ScreenUpdating = False

  1. #1
    Registered User
    Join Date
    04-15-2018
    Location
    London
    MS-Off Ver
    2013
    Posts
    5

    Application.ScreenUpdating = False

    Hi, I am using the below code which interacts a lot with excel. I heard of the Application.ScreenUpdating = False, would that help me reduce the execution time of the code? How does it work? Thank you!


    Public Function GetValue(EventDateRange As Range, _
    DateX As Date, _
    TheValue As Integer) As String

    Application.ScreenUpdating = False

    Dim i As Integer
    Dim startDate As Date
    Dim endDate As Date

    Dim sRange As Range
    Set sRange = Range(Range("B2"), Range("B2").End(xlDown))
    Dim SpecialEvent As String
    Dim SpecialEventRow As Integer
    Dim Probability As Double
    Dim sCell As Range
    Dim CalendarRange As Range
    Dim counter As Integer

    If (TheValue <= 5) Then 'return empty string for distance to the date less than or equal to five
    GetValue = ""
    Exit Function
    End If


    ' Loops through event column
    For i = 1 To EventDateRange.Count - 1
    startDate = EventDateRange(i)
    endDate = EventDateRange(i + 1)

    If EventDateRange(i) = DateX Or EventDateRange(i + 1) = DateX Then
    GetValue = ""
    Exit Function
    End If


    SpecialEvent = ""
    Probability = 0

    ' Column Special Event (B)
    For Each sCell In sRange
    If IsDateInDateRange(CDate(sCell), startDate, endDate) Then
    SpecialEvent = sCell.Value
    Probability = sCell.Offset(0, -1).Value

    ' Select range from Calendar based on start/end date (E)
    Set CalendarRange = GetRangeByDates(startDate, endDate)


    If Not CalendarRange Is Nothing Then

    SpecialEventRow = GetRowInRange(CDate(SpecialEvent), CalendarRange)
    If IsDateInRange(DateX, CalendarRange) Then

    ' Debug.Print DateX & _
    ' " - " & startDate & "-" & endDate & _
    ' " -> " & SpecialEvent & " - " & Probability & _
    ' " - " & SpecialEventRow & _
    ' " - " & CalendarRange.Rows(0).Row + 1 & _
    ' " - " & CalendarRange.Rows(CalendarRange.Rows.Count - 1).Row & _
    '" - " & TheValue

    Dim c As Range
    counter = 0
    ' counts the eligible rows
    For Each c In CalendarRange
    'Debug.Print c
    If Val(c.Offset(0, 1)) > 5 Then
    If CDate(c) <> startDate And CDate(c) <> endDate Then
    counter = counter + 1
    End If
    End If
    Next


    GetValue = (10 / counter) * Probability
    Exit Function

    End If
    End If

    End If
    Next

    Next

    ' GetValue = "0"

    End Function



    Public Function IsDateInDateRange(inputDate As Date, _
    startDate As Date, _
    endDate As Date) As Boolean

    IsDateInDateRange = inputDate >= startDate And inputDate <= endDate


    End Function

    ' Gets Range from Calendar/column E by Start/End dates
    Public Function GetRangeByDates(startDate As Date, endDate As Date) As Range

    Dim cRange As Range
    Set cRange = Range(Range("E2"), Range("E2").End(xlDown))
    Dim startCell As Range
    Dim endCell As Range

    ' Find StartDate and EndDate in column E (calendar)
    Dim cCell As Range
    For Each cCell In cRange
    If CDate(cCell) = startDate Then
    Set startCell = cCell
    End If
    If CDate(cCell) = endDate Then
    Set endCell = cCell
    End If
    Next

    If startCell Is Nothing Or endCell Is Nothing Then
    Set GetRangeByDates = Nothing
    Else
    Set GetRangeByDates = Range(startCell, endCell)
    End If

    End Function

    ' Checks if input date is in provided Range, returns true or false
    Function IsDateInRange(inputDate As Date, xlRange As Range) As Boolean

    Dim c As Range
    For Each c In xlRange
    If inputDate = CDate(c.Value) Then
    IsDateInRange = True
    Exit Function
    End If
    Next
    IsDateInRange = False

    End Function

    ' Gets row location of provided date in Range
    ' It's used to get Special Event row number in column E
    Function GetRowInRange(inputDate As Date, xlRange As Range) As Integer
    Dim c As Range
    For Each c In xlRange
    If CDate(c.Value) = inputDate Then
    GetRowInRange = c.Row
    Exit Function
    End If
    Next
    GetRowInRange = -1 ' if date not found in range then return -1
    End Function

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Application.ScreenUpdating = False

    Good morning, you're really enthusiastic duplicating your own post. I suggest you delete on, probably clicked once too many
    Depending on what your code is doing, I'm not reading it all
    Screen Updtaing set to false is time saving because the system does not have to refresh the screen every time a value is updated but you mus NOT forget to set it back to tru after the last iteration.
    If you've got worksheet events loaded then you should also disable ApplicationEvents

    I have these two macros that I invoke at the beginning of the actual loop and calculations and at the end

    Please Login or Register  to view this content.
    So your macro whatever it is

    Please Login or Register  to view this content.
    Hope this helps
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Application.ScreenUpdating = False

    @Keebellah - Setting the calculation mode to xlCalculationManual is not always helpful...

    @Gero92 - I also suggest you use code tags to format your code properly to make it more readable.
    Cheers!
    Deep Dave

+ 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. Application.ScreenUpdating = False
    By Gero92 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-30-2018, 03:27 AM
  2. Application.Screenupdating = False - not working
    By demarc in forum Excel General
    Replies: 3
    Last Post: 05-02-2015, 11:02 AM
  3. Application.Screenupdating = false not working
    By tanvi_kalra in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 01-30-2014, 09:55 AM
  4. Application.screenupdating = False is not working
    By gnome_core in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-15-2009, 03:32 PM
  5. How to set Application.ScreenUpdating = False for Gen use
    By David_Williams_PG () in forum Excel General
    Replies: 1
    Last Post: 08-15-2006, 07:10 AM
  6. What can cause application.screenupdating=false to not work?
    By Doh97 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-09-2005, 01:05 PM
  7. Using the Application.ScreenUpdating = False?
    By Susan Hayes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-29-2005, 12:06 AM

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