+ Reply to Thread
Results 1 to 16 of 16

Looking for help with speeding up or rewrite on vba code.

  1. #1
    Registered User
    Join Date
    10-29-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    6

    Looking for help with speeding up or rewrite on vba code.

    Looking for help on this to see if i can find a faster way.
    What it does is takes the number that is entered in the "stock in" sheet and adds it to the corresponding cell in the "Master Stock Sheet Hidden" then moves on to the next cell until it gets to the end of the 3000 cells.
    But it takes for ever to enter the stock.

    What i would like it to do is if the "stock in" sheet cell contains stock input, then match the stock codes between both sheets and then add the stock to the "Master Stock Sheet Hidden" in the corresponding cell.

    Bellow is what i am currently using any help advice would be great.


    Sub StockIn()
    '
    ' StockIn Macro
    '
    With Application
    .Application.ScreenUpdating = False
    .Calculation = xlManual
    .EnableEvents = False
    End With
    Dim a As Long
    Dim b As Long
    Dim answer As Long
    Dim myrow As String
    Dim r As Integer
    For r = 6 To 3000
    myrow = "G" & r
    a = Sheets("Stock In").Range(myrow)
    b = Sheets("Master Stock Sheet Hidden").Range(myrow)
    answer = a + b
    Sheets("Master Stock Sheet Hidden").Range(myrow) = answer
    Sheets("Stock In").Range(myrow).ClearContents
    Next r
    ActiveWorkbook.Save
    MsgBox "Stock Added"
    With Application
    .Application.ScreenUpdating = True
    .Calculation = xlAutomatic
    .EnableEvents = False
    End With
    '
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Looking for help with speeding up or rewrite on vba code.

    Hi.

    This is not clear. Post the excel file with your explanation.
    Click *, if my suggestion helps you. Have a good day!!

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Looking for help with speeding up or rewrite on vba code.

    Hi,

    I think it's slow because you do so many things in the loop. Is this faster? (I also set .EnableEvent = True at the end, I think that's what you wanted. If no, feel free to change it back.)
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-29-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Looking for help with speeding up or rewrite on vba code.

    Removed Example
    Last edited by ukchris74; 01-16-2013 at 09:28 AM.

  5. #5
    Registered User
    Join Date
    10-29-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Looking for help with speeding up or rewrite on vba code.

    hi tried it but not sure where its pulling things from and putting them to i have included an example of my sheet thanks for your support.

  6. #6
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Looking for help with speeding up or rewrite on vba code.

    Unfortunately I can't test your file properly, because I have an earlier excel version (Excel 2003).

    Have you tried my macro? Just replace your code with my code.

    Also, make sure you make a copy of the original file before you try it.

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Looking for help with speeding up or rewrite on vba code.

    @RHCPgergo
    You have this backwards
    Please Login or Register  to view this content.
    should be Cells(row,column)

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  8. #8
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Looking for help with speeding up or rewrite on vba code.

    You're exactly right. So the correct code should be this.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    10-29-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Looking for help with speeding up or rewrite on vba code.

    yes notice that after it started writing across the row but now sorted thanks for all your help.

    seconds instead of minutes lol

  10. #10
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Looking for help with speeding up or rewrite on vba code.

    Anything looping that many times is going to be slow. See if this is any faster:

    Please Login or Register  to view this content.
    BTW, in future it's probably easier if you post workbooks where the sheets you'd like people to look at aren't hidden

  11. #11
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Looking for help with speeding up or rewrite on vba code.

    See if this is any faster

    Please Login or Register  to view this content.

  12. #12
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Looking for help with speeding up or rewrite on vba code.

    Great replies, thank you for showing more solutions guys.

  13. #13
    Registered User
    Join Date
    10-29-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Looking for help with speeding up or rewrite on vba code.

    Wow didnt think it could get quicker thanks for the replies.


    What do you have to change to subtract the stock as i cant see the plus calculation.
    Cheers
    Chris

  14. #14
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Looking for help with speeding up or rewrite on vba code.

    In my code you have to change this line:

    Please Login or Register  to view this content.
    To:

    Please Login or Register  to view this content.

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Looking for help with speeding up or rewrite on vba code.

    fyi you could also use copy and paste special values one data set then copy and paste special values with either add or subtract for the second data set
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  16. #16
    Registered User
    Join Date
    10-29-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Looking for help with speeding up or rewrite on vba code.

    Thanks to everyone that help me in solving this problem got the action down to under 5 sec's for imputing 3000 lines not to shabby.

    Cheers

    Chris

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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