+ Reply to Thread
Results 1 to 4 of 4

Help with shortening code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-16-2004
    Posts
    125

    Help with shortening code

    I am new to Visual Basic and was hoping someone could help me shorten this code. The part that seems too long is the Copy and PasteSpecial portion (Bold). Can this be shortened any? As you can probably tell I recorded some of this macro and wrote the rest. This is only a small part of a very large marcro, which is why I want to shorten it. A variation of the code I posted repeats several times. Thanks for any help.


    Sub Finish_01()

    If Range("I7") = "" Then
    Range("I7").Formula = Range("E2") + Range("F2")
    Range("I7").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("B2:C2").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("G7").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("A1").Select
    ElseIf Range("L7") = "" Then ........

    ............ End Sub

  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
    Quote Originally Posted by CJ-22
    I am new to Visual Basic and was hoping someone could help me shorten this code. The part that seems too long is the Copy and PasteSpecial portion (Bold). Can this be shortened any? As you can probably tell I recorded some of this macro and wrote the rest. This is only a small part of a very large marcro, which is why I want to shorten it. A variation of the code I posted repeats several times. Thanks for any help.


    Sub Finish_01()

    If Range("I7") = "" Then
    Range("I7").Formula = Range("E2") + Range("F2")
    Range("I7").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("B2:C2").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("G7").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Range("A1").Select
    ElseIf Range("L7") = "" Then ........

    ............ End Sub
    Hi,

    For starters you could use
    Range("I7").Copy
    Range("I7").PasteSpecial(xlPasteValues)
    Range("B2:C2").Copy
    Range("G7").PasteSpecial(xlPasteValues)
    As always it's a trade off. If there are only two or three Copy..Paste operations, then it's probably not worth the overhead of having a loop to handle them. If however there are several then you do need to incorporate these either in a loop, or by calling a separate Copy Paste subroutine and passing the variables to it.

    So for instance at the position where you want to start a copy and paste, and using the example above, you'd introduce the following line:

    Call CopyPaste(Range("I7"),Range("I7")) '
    This would call the following Procedure..untested
    Sub CopyPaste(CopyCell As Range, DestinationCell As Range)
         CopyCell.Copy
         DestinationCell.PasteSpecial(xlPasteValues)
    
    End Sub
    If you want to use a loop then there'd need to be some algorithm for working out how the copy and pasted cells change. i.e. is there any relationship both between them, and also between each occurrence?

    If there is post back and we'll see what we can come up with. However I suspect the 'Call CopyPaste()' process is going to be the best and simplest way.

    Rgds

  3. #3
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    CJ-22


    Please read forum rules & wrap your VBA code

    A thread with the rules is available at the top of each forum or see links below

    If you do not understand the VBA code wrap instructions have a look at my last reply in this thread
    http://www.excelforum.com/showthread.php?t=583950
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  4. #4
    Forum Contributor
    Join Date
    12-16-2004
    Posts
    125
    When I get home from work today I will try some of these. If they don't work, I will post again. Thank you.

+ 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