+ Reply to Thread
Results 1 to 9 of 9

Help my code is running very slowly..

Hybrid View

  1. #1
    Registered User
    Join Date
    11-12-2009
    Location
    surrey
    MS-Off Ver
    Excel 2003
    Posts
    22

    Help my code is running very slowly..

    Thank you to everyone thats helped me get this far, but I need your assistance again. Currently the following code take about 15 minutes to run through 300 lines. so any help would be greatly appreciated

    Thank you in advance. John

    Dim MyLastRow As Long, x As Long, y As Long, w As Long, z As Long   ' Set Variables
    Dim sWBU As Worksheet, sWBUT As Worksheet    ' Set worksheet abbreviations
    Dim varConctnt As Variant 
    
    Sheets.Add.Name = "Forecast"                        ' Add new sheet called "FORCAST"
    Set sWBU = Worksheets("2014")                      ' Name of worksheets for data source
    Set sWBUT = Worksheets("Forecast")                ' Name of worksheets for data destination
    
    MyLastRow = sWBU.UsedRange.Rows.Count        ' Find Final Row in dataset
    varConctnt = "FR-25-UK-"                          ' Used to concatenate text cell value
    
    x = 2     ' Start row to copy to
    w = 1    ' Start Column to copy to
    z = 9     ' Start column to copy from
    For y = 30 To mylastrow                                  ' first row of the source data
    If Not IsEmpty(sWBU.Cells(y, 9)) Then
    sWBUT.Cells(x, w).Value = varConctnt & sWBU.Cells(y, z).Value & "-URF"    ' This concatenate the AA & nominal code
    sWBUT.Cells(x, 3).Value = sWBU.Cells(y, 11).Value     ' Project
    sWBUT.Cells(x, 2).Value = sWBU.Cells(y, 13).Value     ' Source
    sWBUT.Cells(x, 4).Value = sWBU.Cells(y, 42).Value     ' Jan of first year
    sWBUT.Cells(x, 5).Value = sWBU.Cells(y, 47).Value     ' Feb of first year
    sWBUT.Cells(x, 6).Value = sWBU.Cells(y, 52).Value     ' Mar of first year
    sWBUT.Cells(x, 7).Value = sWBU.Cells(y, 57).Value     ' Apr of first year
    sWBUT.Cells(x, 8).Value = sWBU.Cells(y, 62).Value     ' May of first year
    sWBUT.Cells(x, 9).Value = sWBU.Cells(y, 67).Value     ' Jun of first year
    sWBUT.Cells(x, 10).Value = sWBU.Cells(y, 72).Value    ' Jul of first year
    sWBUT.Cells(x, 11).Value = sWBU.Cells(y, 77).Value    ' Aug of first year
    sWBUT.Cells(x, 12).Value = sWBU.Cells(y, 82).Value    ' Sep of first year
    sWBUT.Cells(x, 13).Value = sWBU.Cells(y, 87).Value    ' Oct of first year
    sWBUT.Cells(x, 14).Value = sWBU.Cells(y, 92).Value    ' Nov of first year
    sWBUT.Cells(x, 15).Value = sWBU.Cells(y, 97).Value    ' Dec of first year
    sWBUT.Cells(x, 16).Value = sWBU.Cells(y, 102).Value   ' Jan of 2nd year
    sWBUT.Cells(x, 17).Value = sWBU.Cells(y, 107).Value   ' Feb of 2nd year
    sWBUT.Cells(x, 18).Value = sWBU.Cells(y, 112).Value   ' Mar of 2nd year
    sWBUT.Cells(x, 19).Value = sWBU.Cells(y, 117).Value   ' Apr of 2nd year
    sWBUT.Cells(x, 20).Value = sWBU.Cells(y, 122).Value   ' May of 2nd year
    sWBUT.Cells(x, 21).Value = sWBU.Cells(y, 127).Value   ' Jun of 2nd year
    sWBUT.Cells(x, 22).Value = sWBU.Cells(y, 132).Value   ' Jul of 2nd year
    sWBUT.Cells(x, 23).Value = sWBU.Cells(y, 137).Value   ' Aug of 2nd year
    sWBUT.Cells(x, 24).Value = sWBU.Cells(y, 142).Value   ' Sep of 2nd year
    sWBUT.Cells(x, 25).Value = sWBU.Cells(y, 147).Value   ' Oct of 2nd year
    sWBUT.Cells(x, 26).Value = sWBU.Cells(y, 152).Value   ' Nov of 2nd year
    sWBUT.Cells(x, 27).Value = sWBU.Cells(y, 157).Value   ' Dec of 2nd year
                 
            x = x + 1
            z = 9
            w = 1
        End If
    Next y
    varConctnt = ""

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Help my code is running very slowly..

    Difficult to comment with certainty without seeing the request in the context of its workbook but avoid a loop and consider using Autofilter to first filter the rows you're trying to update and then add the relevant formulae to the filtered rows before removing the autofilter.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Valued Forum Contributor Kamboj's Avatar
    Join Date
    09-25-2014
    Location
    India
    MS-Off Ver
    2003 - 2010
    Posts
    430

    Re: Help my code is running very slowly..

    use this command in the beginning

    Application.ScreenUpdating = False

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Help my code is running very slowly..

    There's nothing particularly obvious in your code that's slowing things down.

    Hard to find a good answer without seeing the workbook:

    Are there many complex / volatile formuale? I'd suggest switching calculation to manual before running your code, then switch back to automatic (if necessary) afterwards, see if that helps speed things up.

    Are there any workbook / worksheet change events being triggered by every value change? If so, try disabling application events.

    And as always, disabling screenupdating helps speed things up.


    To summarise:
    With Application
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    'your code
    
    With Application
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
        .ScreenUpdating = True
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Help my code is running very slowly..

    Reading and writing cell by cell won't be quick. You can speed it up using arrays like this:
        Dim MyLastRow As Long, x As Long, y As Long, w As Long, z As Long   ' Set Variables
        Dim sWBU As Worksheet, sWBUT As Worksheet    ' Set worksheet abbreviations
        Dim varConctnt            As Variant
        Dim vIn, vOut
    
        Sheets.Add.Name = "Forecast"                        ' Add new sheet called "FORCAST"
        Set sWBU = Worksheets("2014")                      ' Name of worksheets for data source
        Set sWBUT = Worksheets("Forecast")                ' Name of worksheets for data destination
    
        MyLastRow = sWBU.UsedRange.Rows.Count        ' Find Final Row in dataset
        varConctnt = "FR-25-UK-"                          ' Used to concatenate text cell value
    
        vIn = sWBU.Range(sWBU.Cells(30, 1), sWBU.Cells(MyLastRow, 157)).Value2
        ReDim vOut(1 To MyLastRow - 29, 1 To 27)
    
        x = 1     ' Start row to copy to
        w = 1    ' Start Column to copy to
        z = 9     ' Start column to copy from
    
        For y = 1 To MyLastRow - 29                                  ' first row of the source data
            If Not IsEmpty(vIn(y, 9)) Then
                vOut(x, w) = varConctnt & vIn(y, z) & "-URF"    ' This concatenate the AA & nominal code
                vOut(x, 3) = vIn(y, 11)     ' Project
                vOut(x, 2) = vIn(y, 13)     ' Source
                vOut(x, 4) = vIn(y, 42)     ' Jan of first year
                vOut(x, 5) = vIn(y, 47)     ' Feb of first year
                vOut(x, 6) = vIn(y, 52)     ' Mar of first year
                vOut(x, 7) = vIn(y, 57)     ' Apr of first year
                vOut(x, 8) = vIn(y, 62)     ' May of first year
                vOut(x, 9) = vIn(y, 67)     ' Jun of first year
                vOut(x, 10) = vIn(y, 72)    ' Jul of first year
                vOut(x, 11) = vIn(y, 77)    ' Aug of first year
                vOut(x, 12) = vIn(y, 82)    ' Sep of first year
                vOut(x, 13) = vIn(y, 87)    ' Oct of first year
                vOut(x, 14) = vIn(y, 92)    ' Nov of first year
                vOut(x, 15) = vIn(y, 97)    ' Dec of first year
                vOut(x, 16) = vIn(y, 102)   ' Jan of 2nd year
                vOut(x, 17) = vIn(y, 107)   ' Feb of 2nd year
                vOut(x, 18) = vIn(y, 112)   ' Mar of 2nd year
                vOut(x, 19) = vIn(y, 117)   ' Apr of 2nd year
                vOut(x, 20) = vIn(y, 122)   ' May of 2nd year
                vOut(x, 21) = vIn(y, 127)   ' Jun of 2nd year
                vOut(x, 22) = vIn(y, 132)   ' Jul of 2nd year
                vOut(x, 23) = vIn(y, 137)   ' Aug of 2nd year
                vOut(x, 24) = vIn(y, 142)   ' Sep of 2nd year
                vOut(x, 25) = vIn(y, 147)   ' Oct of 2nd year
                vOut(x, 26) = vIn(y, 152)   ' Nov of 2nd year
                vOut(x, 27) = vIn(y, 157)   ' Dec of 2nd year
    
                x = x + 1
            End If
        Next y
        sWBUT.Cells(2, 1).Resize(UBound(vOut, 1), UBound(vOut, 2)).Value2 = vOut
        varConctnt = ""
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Help my code is running very slowly..

    Hi.. Not tested.. but maybe the loop can be something like this..?

      For y = 1 To MyLastRow - 29
            If Not IsEmpty(vIn(y, 9)) Then
                For ii = 2 To 27
                    vOut(x, w) = varConctnt & vIn(y, z) & "-URF"
                    vOut(x, ii) = vIn(y, Choose(ii - 1, 13, 11, 42, 47, 52, 57, 62, 67, 72, 77, 82, 87, 92, 97, 102, 107, 112, 117, 122, 127, 132, 137, 142, 147, 152, 157))
                    x = x + 1
                Next ii
            End If
        Next y
    It probably isn't any quicker.. actually.. it may even be slower (as there is an extra loop there)..

    @Rory.. any advice on whether doing it like this is better/worse?

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Help my code is running very slowly..

    Quote Originally Posted by apo View Post
    @Rory.. any advice on whether doing it like this is better/worse?
    I doubt it will be significantly different - there may be an additional loop and function call in there but the same amount of data has to be processed either way.

  8. #8
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Help my code is running very slowly..

    Try this

    Sub Macro1()
    
    Dim MyLastRow As Long, x As Long, y As Long, w As Long, z As Long, T As Long   ' Set Variables
    Dim sWBU As Worksheet, sWBUT As Worksheet    ' Set worksheet abbreviations
    Dim varConctnt As Variant
    
    Sheets.Add.Name = "Forecast"                        ' Add new sheet called "FORCAST"
    Set sWBU = Worksheets("2014")                      ' Name of worksheets for data source
    Set sWBUT = Worksheets("Forecast")                ' Name of worksheets for data destination
    
    MyLastRow = sWBU.UsedRange.Rows.Count        ' Find Final Row in dataset
    varConctnt = "FR-25-UK-"                          ' Used to concatenate text cell value
    
    x = 2     ' Start row to copy to
    w = 1    ' Start Column to copy to
    z = 9     ' Start column to copy from
    For y = 30 To MyLastRow                                  ' first row of the source data
    If Not IsEmpty(sWBU.Cells(y, 9)) Then
    sWBUT.Cells(x, w).Value = varConctnt & sWBU.Cells(y, z).Value & "-URF"    ' This concatenate the AA & nominal code
                 
            x = x + 1
            z = 9
            w = 1
        End If
    Next y
    
    sWBUT.Range(Cells(2, 3), Cells(MyLastRow - 30 + 2, 3)).Value = sWBU.Range(Cells(30, 11), Cells(MyLastRow, 11)).Value
    sWBUT.Range(Cells(2, 2), Cells(MyLastRow - 30 + 2, 2)).Value = sWBU.Range(Cells(30, 13), Cells(MyLastRow, 13)).Value
    
    For T = 4 To 27
    sWBUT.Range(Cells(2, T), Cells(MyLastRow - 30 + 2, T)).Value = sWBU.Range(Cells(30, 42 + (T - 4) * 5), Cells(MyLastRow, 42 + (T - 4) * 5)).Value
    Next T
    
    varConctnt = ""
    
    End Sub
    Last edited by kvsrinivasamurthy; 10-11-2014 at 09:03 AM.

  9. #9
    Registered User
    Join Date
    11-12-2009
    Location
    surrey
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Help my code is running very slowly..

    Thank you!

    I'll check out all your suggestions and let know how I get on.

+ 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. VBA Code is running really slowly
    By behrensf84 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-07-2014, 06:06 PM
  2. Code running really slowly
    By The Phil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-03-2014, 08:42 PM
  3. [SOLVED] Code running slowly
    By Sweepin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-30-2013, 09:15 PM
  4. Code running slowly
    By rhudgins in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 02-01-2011, 11:08 AM
  5. Paste code running extremely slowly...
    By KR in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2005, 11:05 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