+ Reply to Thread
Results 1 to 3 of 3

Improve Code

  1. #1
    Registered User
    Join Date
    01-23-2022
    Location
    uk
    MS-Off Ver
    MS Office 365
    Posts
    27

    Improve Code

    Hi,

    Thanks for looking, i have created this macro which works but a bit clunky, im sure it could be made better.

    What we have is a export dump that gets updated regularly, we cannot change how this is. This provides data from yesterday for the next 20 days, unfortunately we need to keep the data for the week, but be able to update. i have set a macro to copy this data in (not show), then split into days, if the data has passed 24 hours then keep the current data, but update the rest of the days. So on Sat it would update Sat to Fri, Sun would still update Sat to Fri as the data is still there, however on Mon it would ignore Sat leaving the data there but update Sun to Fri and so forth till the end of the week. Which we will then start again with a new sheet for the week.

    As i have said this works fine just clunky, perfdatex = each date for the week, this could actually be changed to start date + 1 etc, note that perfdate2 is the startdate and continue in order from here, the only exception is perfdate1 which is the day before.

    Any help would be much apreciated.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,962

    Re: Improve Code

    Honestly, there is no such thing as too clunky of a macro. If it works - and doesn't take all day - then don't worry about it.

    Could it be improved? Probably. Is it worth improving? Probably not.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,750

    Re: Improve Code

    There are a number of problems with this code, and opportunities for improvement.

    It appears that you did not show all your code, see blue text below.

    You do not show any variable declarations. I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

    You assign strings to the PerfDaten and Now2 variables, but want to use them as dates. Just make them dates. And use the built-in function Date to give the current date rather than using Now() and then manipulating it to give just the date.

    It is not necessary to use ThisWorkbook to qualify range references. This is the default.

    Your code for each day is identical except for the day and sheets. This can be factored out, greatly improving maintainability.

    Do not Select sheets or other objects to manipulate them. Just reference them directly. There are exceptions to this but they don't apply to your code.

    You do not need to unhide then hide these sheets to do any of this work, once you eliminate Select and Selection.

    Variable MyFile is undefined, used in Workbooks(MyFile). I have no idea what you are trying to refer to here.

    In the merging part you are doing a PasteValues and then a PasteFormats. This is essentially just a Paste, not sure why you are doing it this way.

    Here is the rewritten code, but I cannot test it without having your file. If you attach a file with sample data I'd be happy to test it.
    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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. Can someone help me Improve my code, please?
    By Energy48 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2023, 05:56 PM
  2. How to improve my code ?
    By ozstrik3r69 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-23-2017, 08:51 AM
  3. Improve my code: If, or, then
    By Alex532 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-01-2013, 07:19 PM
  4. [SOLVED] To improve Efficiency of code, code running too long
    By andywsw in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-19-2012, 05:54 PM
  5. [SOLVED] Anyone help to improve my code?
    By Andrew in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-05-2006, 10:15 AM
  6. Re: Improve code
    By rjamison in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-13-2005, 08:05 PM
  7. [SOLVED] Improve code
    By Gareth in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-20-2005, 11:06 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