+ Reply to Thread
Results 1 to 9 of 9

Why is this VBA code (that I wrote) executing so slow?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Why is this VBA code (that I wrote) executing so slow?

    I've created a user form for people to enter data into. All is working well in the workbook, except this form is slowing things down. It takes WAY too long to process the data that is entered (and thus allocated).
    Any suggestions on how to speed this up?

    Private Sub cboDept1_Change()
    Dim idx As Long
    Dim I As Long
    
    idx = cboDept1.ListIndex
    
    If idx <> -1 Then
    With Sheet3
    For I = 3 To 22
    Me.Controls("Label" & I + 12).Caption = .Range("C" & I).Offset(0, idx).Text
    Next I
    End With
    End If
    
    End Sub
    
    Private Sub cmdCancel_Click()
    Unload Me
    End Sub
    
    Private Sub cmdClearForm_Click()
    Call UserForm_Initialize
    End Sub
    
    Private Sub cmdOK4_Click()
    Application.ScreenUpdating = False
    Dim rng As Range, wd As Worksheet, r As Long
    Set wd = ActiveWorkbook.Sheets("IndicatorDB")
    r = wd.Range("A" & Rows.Count).End(xlUp).Row
    For I = 1 To 20
    If Me.Controls("TextBox" & I).Value <> "" Then
    
    r = r + 1: Set rng = wd.Range("A" & r)
    rng.Offset(-1, 0).EntireRow.Copy: rng.PasteSpecial xlPasteFormulas
    
    rng.Value = cboName1.Value
    rng.Offset(0, 1) = cboDept1.Value
    rng.Offset(0, 2).Value = Me.Controls("Label" & I + 14).Caption
    rng.Offset(0, 3).Value = DTPicker4.Value
    rng.Offset(0, 4).Value = Me.Controls("TextBox" & I).Value
    Set rng = rng.Offset(1)
    End If
    Next I
    
    
    Sheets("Forms").Select
    Unload Me
    UserForm2.Show
    Application.ScreenUpdating = True
    End Sub
    
    Private Sub UserForm_Initialize()
    With Worksheets("ADMIN")
    cboDept1.List = .Range("F4", .Range("F" & Rows.Count).End(xlUp)).Value
    End With
    
    With Worksheets("Employees")
    cboName1.List = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value
    End With
    
    End Sub
    
    Sub SlowWithFlicker()
    
    Dim rCell As Range
    
    For Each rCell In Range("A1:A15000")
    
    rCell.Select
    
    Next rCell
    
    End Sub
    
    
    
    Sub FastWithNoFlicker()
    
    Dim rCell As Range
    
    Application.ScreenUpdating = False
    
    For Each rCell In Range("A1:A15000")
    
    rCell.Select
    
    Next rCell
    
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by johnw993; 02-08-2015 at 02:20 AM. Reason: didn't clearly show message and code

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Why is this VBA code (that I wrote) executing so slow?

    Can you submit your workbook? I am a little curious about the interaction of cboDept1_Change() and the UserForm_Initialize processes and how they may be affecting each other. Also, this is beyond reason:

    Sub SlowWithFlicker()
    
    Dim rCell As Range
    
    For Each rCell In Range("A1:A15000")
    
    rCell.Select
    
    Next rCell
    
    End Sub
    
    
    
    Sub FastWithNoFlicker()
    
    Dim rCell As Range
    
    Application.ScreenUpdating = False
    
    For Each rCell In Range("A1:A15000")
    
    rCell.Select
    
    Next rCell
    
    Application.ScreenUpdating = True
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Why is this VBA code (that I wrote) executing so slow?

    stnkynts,

    Thanks for your reply. See attached my workbook (in zip file).

    The Workbook is 'Menu driven' and will only show the sheets you've opened. To see all the sheets in the workbook, from the Main Menu click on the Administration button, use Admin as user name and the letter a (small caps) as the password. Then click on "Show All Sheets" button in the upper right.
    The password to un-protect any or all sheets is the letter j (small caps).

    I'm no expert in Excel, and am trying to learn VBA as I go, so I'm sure you'll find some illogical processes in the workbook.

    Thank you for your willingness to take a look.

    John
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Why is this VBA code (that I wrote) executing so slow?

    Please remove from zip file and attach directly.

  5. #5
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Why is this VBA code (that I wrote) executing so slow?

    Please advise me how to. The file is too large (4.5 MB) to be accepted as an attachment.

  6. #6
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Why is this VBA code (that I wrote) executing so slow?

    I've been asked to show my workbook, which I'm glad to do, but the forum doesn't allow any workbook larger than 1K to be shared as an attachment. When I send the Workbook in a zip file, I'm being asked to please unzip and send it as a workbook. I'm confused. Please advise what I should do.
    This site has been a great learning experience and I really, really would like some feedback on the issues I'm trying to solve in my workbook.

    Thank you,

    John

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Why is this VBA code (that I wrote) executing so slow?

    Hello johnw993,

    Leave your workbook zipped. You were correct in following the forum's rule for posting. If a member is unable to open the workbook because the file is corrupted then you should repost. However, if a member does do not possess the needed software to open the file, that is not your problem.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  8. #8
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Why is this VBA code (that I wrote) executing so slow?

    Thank you for your reply Leith. I was starting to wonder what I was doing wrong.
    I guess if no-one else replies to my post/question, my question must not be enticing enough.
    It's a challenge to find a VBA expert who can look at my code and explain to me why my code is slowing things down (because I suspect I wrote it very poorly).
    I'll post my message/question again.

    Regards,

    John

  9. #9
    Forum Contributor
    Join Date
    11-30-2012
    Location
    Colorado, US
    MS-Off Ver
    MS Prof. 2010 and 2013 + 365
    Posts
    113

    Re: Why is this VBA code (that I wrote) executing so slow?

    Alan,

    My apologies for the mistake. Is the moderation request fulfilled correctly?

+ 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. The runtime for my code I wrote takes too long, is there a way tocan you condense?
    By dnice0123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2014, 03:33 AM
  2. Replies: 10
    Last Post: 11-13-2013, 09:01 PM
  3. VBA code that I wrote seems to be looping without a Do...Loop statement. Why?
    By carpe.cervisiam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2012, 12:08 AM
  4. QUERY &amp;amp; HELP: so slow executing VBA code... :S
    By KevinGPO in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2006, 11:50 AM
  5. [SOLVED] QUERY & HELP: so slow executing VBA code... :S
    By KevinGPO in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-13-2006, 11:50 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