+ Reply to Thread
Results 1 to 8 of 8

Optimize my VBA code

Hybrid View

modytrane Optimize my VBA code 02-07-2019, 10:03 AM
MarvinP Re: Optimize my VBA code 02-07-2019, 10:10 AM
modytrane Re: Optimize my VBA code 02-07-2019, 10:11 AM
MarvinP Re: Optimize my VBA code 02-07-2019, 10:16 AM
modytrane Re: Optimize my VBA code 02-07-2019, 10:22 AM
MarvinP Re: Optimize my VBA code 02-07-2019, 10:37 AM
modytrane Re: Optimize my VBA code 02-07-2019, 10:32 AM
modytrane Re: Optimize my VBA code 02-07-2019, 11:25 AM
  1. #1
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Optimize my VBA code

    I have following code working fine but I am hoping it can be modified to make it run faster. Currently it takes almost five minutes to run.
    I have two worksheets. Data tab gets new update of currently open projects. Active tab needs to be updated.

    The basic idea is to update worksheet Active in two ways by looking at records in Data tab.
    1. If the record does not exist in Active tab [new project], that row [from Data tab] gets added at the bottom of Active tab.
    2. If the record exists than update columns B:X and Z:AG. (data in column Y needs to remain, it's a manual entry).

    I have learned a lot here and I would really appreciate any tips I can get to optimize this code.

    With Worksheets("Data")
        For x = 8 To lrd  ‘ lrd is last row in Data tab which is around 850.
            If Worksheets("Data").Range("A" & x).Value = "No" Then 'Cell "A" & x has formula that results in Yes or No to indicate if the record exists.
                Worksheets("Data").Range("B" & x & ":AG" & x).Copy _
                    Destination:=Worksheets("Active").Range("B" & lra + 1)
                    lra = lra + 1  ‘lra is last row used in worksheet Active
                Else
    a = .Range("AS" & x).Value  ‘this cell has formula that calculates which row in Active sheet holds 
                                                 ‘ matching data.
                Worksheets("Data").Range("B" & x & ":X" & x).Copy _
                    Destination:=Worksheets("Active").Range("B" & a)
                
                Worksheets("Data").Range("Z" & x & ":AG" & x).Copy _
                    Destination:=Worksheets("Active").Range("Z" & a)
         
            End If
        Next x
    End With
    1. Click on the * Add Reputation if you think this helped you
    2. Mark your thread as SOLVED when question is resolved

    Modytrane

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,229

    Re: Optimize my VBA code

    Hi modytrane,

    Instead of going through each row one by one why not try this....

    Take the entire table and append it on the bottom of the other table. Then do an Advanced Filter using "Unique Records Only" on the combined table. This should leave only one of the duplicates and what I think you want.

    Does this make sense? Do it a few times manually and then write the VBA to accomplish the Advanced Filter if you need to use VBA. Native Excel functions are faster than VBA.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Optimize my VBA code

    I have been playing around with this and just figured out something. Maybe it will help others as well.

    I turned of calculation [set it to manual] just before entering this code and turned it back on after exiting.
    Made the code much faster. Seems like it was re-calculating after every step of the loop.

    I still welcome any other tips I can get.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,229

    Re: Optimize my VBA code

    Here is a better site explaining what you need

    http://cpearson.com/excel/optimize.htm
    Last edited by MarvinP; 02-07-2019 at 10:19 AM.

  5. #5
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Optimize my VBA code

    Marvin
    Thanks for the tip.
    Here's the question I have.
    Once I combine the tables, old and new records will be in one table. Then when I filter for unique only, How do I ensure that only the newer record stays and older one gets deleted? There are no date or time stamp in the record to indicate which is newer. All I know is that Data tab has latest download.

    Thanks again.

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,229

    Re: Optimize my VBA code

    When the Advanced Filter does its thing, using Unique Only, the top/first one is kept. If you put the newest table on the bottom (with dups in it) only the above records will be kept.

    After reading your question again, you may want to put the newer table on top and then do the advanced filter on the combined table.

    Hope this helps.

  7. #7
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Optimize my VBA code

    Marvin,
    Thanks for the link. Nice tips there. I used one for calculation as stated above. I'll keep this for reference in the future.

  8. #8
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Optimize my VBA code

    It does help to know that Advanced filter keeps top or first unique value.

    Thanks

+ 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 to optimize the code
    By pm.patel189 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-05-2018, 01:06 PM
  2. SpeedUp and Optimize My Code
    By mwinning8 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-07-2017, 05:25 PM
  3. New to VBA programming and need help to Optimize VBA code
    By snuffnchess in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-10-2016, 07:44 AM
  4. Optimize a code
    By pezalmendra in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-06-2015, 05:31 PM
  5. Optimize code
    By DarkKnightLupo in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 02-19-2014, 08:58 AM
  6. Optimize code
    By miso.dca in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-08-2011, 03:35 PM
  7. Optimize VBA code
    By doodlebug in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-22-2007, 07:53 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