+ Reply to Thread
Results 1 to 9 of 9

VBA Efficiency

  1. #1
    Registered User
    Join Date
    03-19-2022
    Location
    Columbus, Ohio
    MS-Off Ver
    2011 For Mac
    Posts
    2

    VBA Efficiency

    Howdy all! Running 2011 Excel on my 2012 MacBook Pro. Just learning how to use excel passed inputting numbers in someone else's sheet. In making my own sheet, to keep it clean, I hid blank rows, but wanted another blank row to appear, when needed. After asking a few people who don't know VBA, who said there isn't a way, I wrote my own way. I understand it may 100% just be my computer(yes I know I could buy a $50 PC that would be better), but this code takes about 6 seconds to work. Just wondering if there would have been a more efficient way of writing this out? Perhaps making a range within the column, instead of the entire column?
    TIA!
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 Then
    Application.EnableEvents = True
    Hiderows
    StartRow = 7
    EndRow = 43
    ColNum = 2
    For i = StartRow To EndRow
    If Cells(i, ColNum).Value = "" Then
    Cells(i + 1, ColNum).EntireRow.Hidden = True
    Else
    Cells(i + 1, ColNum).EntireRow.Hidden = False
    End If
    Next i
    End If
    End Sub
    Last edited by Homesteader82; 03-19-2022 at 06:06 PM. Reason: Typo

  2. #2
    Valued Forum Contributor
    Join Date
    07-13-2021
    Location
    California, USA
    MS-Off Ver
    2010
    Posts
    513

    Re: VBA Efficiency

    At a minimum, execute Application.ScreenUpdating = False some time before the loop.

    I believe it is set to True automagically when you exit back to Excel. But it would be prudent to do that explicitly before exiting.

    Also, it is probably better to write For i = EndRow to StartRow Step -1.

    PS.... You should execute Application.EnableEvents = False at the beginning. But don't forget to set it to True again before exiting.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: VBA Efficiency

    Also, it is probably better to write For i = EndRow to StartRow Step -1.
    That would be the case if you were deleting rows but, for hiding rows, it would make no difference. If you delete rows you can end up skipping rows because the rows have moved up but the index continues unabated. If you hide rows, they remain in position, just their visibility changes.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,220

    Re: VBA Efficiency

    Try this modified code:
    Please Login or Register  to view this content.
    curiouscat408, the descending loop is important for deleting rows/columns. When hiding, the direction of the loop does not matter (TMS has already pointed this out, and I wrote this post for too long ).

    Artik

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: VBA Efficiency

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,220

    Re: VBA Efficiency

    The long operation time of the original macro (ca. 6 s) on a relatively small range of rows (only 36) suggests to me that formulas may also be recalculated (some formulas react to hiding/unhiding rows/columns). Therefore, I added in my proposal a temporary exclusion of the calculation.

    Artik

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: VBA Efficiency

    Therefore, I added in my proposal a temporary exclusion of the calculation.
    Good thought. However, you should store and restore the current calculation state rather than make the assumption it is set to automatic.

  8. #8
    Registered User
    Join Date
    03-19-2022
    Location
    Columbus, Ohio
    MS-Off Ver
    2011 For Mac
    Posts
    2

    Re: VBA Efficiency

    Thanks, everyone! You’ve given me some great options to try!

  9. #9
    Forum Contributor GWteB's Avatar
    Join Date
    12-13-2021
    Location
    GMT +1
    MS-Off Ver
    2013
    Posts
    136

    Re: VBA Efficiency

    My submission would look like:
    Please Login or Register  to view this content.
    Did this help? Say thanks by clicking the ★

+ 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. Need help improving Job efficiency
    By popovgor in forum Excel General
    Replies: 5
    Last Post: 03-07-2014, 01:11 PM
  2. Efficiency Cumullative
    By Anuru in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-10-2013, 10:30 AM
  3. Efficiency Tracker
    By villivonka in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-21-2013, 05:32 AM
  4. Macro Efficiency?
    By wz4np1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-14-2010, 07:15 PM
  5. Code efficiency
    By abousetta in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-27-2010, 06:56 PM
  6. Efficiency
    By gtmeloney in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-06-2009, 11:10 AM
  7. Efficiency… Efficiency… Efficiency…
    By rakeshplb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-30-2009, 08:37 AM
  8. Efficiency using IF and OR
    By davegugg in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-30-2009, 08:27 PM

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