+ Reply to Thread
Results 1 to 7 of 7

Need Code Critique. Is there a Better or Faster Way?

  1. #1
    Registered User
    Join Date
    04-16-2010
    Location
    Phx, Az
    MS-Off Ver
    Excel 2000
    Posts
    6

    Post Need Code Critique. Is there a Better or Faster Way?

    I am new to VB/Macros. I would like any experienced programmers to critique the below code. The code works fine, but is there a better (more proper) way to write it? Is there a different technique that would be faster?

    I have not used Case Select before, but read that it is a better approach for longer If...Else code. This code will grow to about 50 selections. Is the Select Case any quicker?

    This code basically searched column A of sheet 1 for a specific term. When term is identified, it copies the contents of the 5 cells to the right (Column B,C,D,E,F) one by one and pastes that data to specific cell on sheet2. The search is used on sheet1 because the record location changes regularly(imported data, no control over that).

    Please Login or Register  to view this content.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Need Code Critique. Is there a Better or Faster Way?

    Without knowing what you're doing, I'd do something more like this:
    Please Login or Register  to view this content.
    And, ...

    o Declare all variables appropriate to usage
    o Don't Activate or Select unless necessary

    There's a lengthy thread on this topic at http://www.excelforum.com/excel-prog...id-in-vba.html
    Last edited by shg; 04-23-2010 at 05:31 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Need Code Critique. Is there a Better or Faster Way?

    Does this work correctly?:

    Please Login or Register  to view this content.

    Should be more efficient with less selecting and activating of sheets and cells.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  4. #4
    Registered User
    Join Date
    04-16-2010
    Location
    Phx, Az
    MS-Off Ver
    Excel 2000
    Posts
    6

    Re: Need Code Critique. Is there a Better or Faster Way?

    Thank you shg. I will try your code suggestion, and read the thread link you provided. Like I said, I'm new to writing macros and know just enough to get myself lost. I know there are several ways to do the same thing, so I like to see and try the other ways people would do this, so I can pick it apart and learn from it. Thanks again for the quick response!

  5. #5
    Registered User
    Join Date
    04-16-2010
    Location
    Phx, Az
    MS-Off Ver
    Excel 2000
    Posts
    6

    Re: Need Code Critique. Is there a Better or Faster Way?

    Thank you davegugg. Your code is significantly faster than what I wrote. Like you and shg pointed out, I had way to many occurrences of activating sheets and cells. I had no idea that I could nest For...Next commands inside If's and other For...Next. I realize that I am writing step by step, activating everything every time I do something.

    I guess with time and a lot of writing I'll improve. Thanks for the quick response.

  6. #6
    Registered User
    Join Date
    04-16-2010
    Location
    Phx, Az
    MS-Off Ver
    Excel 2000
    Posts
    6

    Re: Need Code Critique. Is there a Better or Faster Way?

    shg,

    Quick question. What does the following do?
    Please Login or Register  to view this content.
    Your code works extremely fast as well, but copies sheet1 every other cell (Column B,D,F) instead of each of the next 5 cells (B,C,D,E,F). Again, I have not used Case Select, so I am trying to use this piece of code to understand it. Thanks in Advance...

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Need Code Critique. Is there a Better or Faster Way?

    Well ... you see what it does. As I said, I don't know what you're trying to do, and didn't want to take the time to reverse engineer your code to figure it out.

    If you have a specific question, please start a new thread with an appropriate title and post a workbook.

+ 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