+ Reply to Thread
Results 1 to 8 of 8

How can avoid .copy and .paste in this loop?

  1. #1
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Question How can avoid .copy and .paste in this loop?

    I wrote a code that creates two (2) lines of calculations and a delimiter between both sections.
    The first iteration looks like this:
    1.1
    delim
    1.2
    The second iteration looks like this:
    1.1
    2.1
    delim
    1.2
    2.2
    and so forth.

    I posted photos for a visual representation.

    My code is below & I want to avoid using .copy, .paste, but when I tried to use an array, the values didn't store because the range changed. I spent four hours on my pre-made solution, but I'm sure there is a much easier way to do precisely what I'm doing with fewer lines of code.

    Thanks in advance.

    Please Login or Register  to view this content.
    Attached Images Attached Images
    Last edited by carlmon; 03-31-2022 at 07:57 AM. Reason: adding excel file

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

    Re: How can avoid .copy and .paste in this loop?

    Please attach your actual Excel file so we can run your code. Please see "HOW TO ATTACH YOUR SAMPLE WORKBOOK" in the yellow banner at the top of the page and attach your actual file.

    By the way, your code would be much more readable if you used indentation to show control structure. Here is your code unchanged except for indentation:
    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: How can avoid .copy and .paste in this loop?

    Thank you for the tips. I've added the excel file - I'm new to VBA so all feedback is much appreciated.

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

    Re: How can avoid .copy and .paste in this loop?

    This is very complicated code so I'm going to have to just focus on the specifics of your question. I do not even know the procedure to run this code, since it appears that a bunch of data setup is required before this sub runs.

    Why do you want to avoid copy and paste? You said it might be done in fewer lines of code, but fewer is not always better. Also, it's only two lines of code, so the best we can do is one:
    Please Login or Register  to view this content.
    or this
    Please Login or Register  to view this content.
    But if your code produces the desired results I wouldn't touch it.




    I do feel obligated to point out a couple of things.

    There are a large number of global variables in ASCE_Equipment. They can be used judiciously but can also wreak havoc if you are trying to diagnose a bug. This is called common environment coupling and is generally considered undesirable. The best practice is to pass variables as arguments to subs, rather than making them all global.

    There are undeclared variables. I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

    There are frequent uses of deeply nested If/Else statements. In these cases, you can use the ElseIf structure to avoid the nesting. This is not a bug, but it is a tighter coding technique that is more readable and less prone to error. Here is a section of Sub variables rewritten:

    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: How can avoid .copy and .paste in this loop?

    Awesome, the tips like yours are the reason I like this forum so much.

    I want to avoid having the cell selected when .copy is used or .pastespecial is used -

    If I use the line that you wrote:

    Sheets("main").Range("AC" & r.Row - p + 3 & ":AH" & r.Row - p + 3).Value = Sheets("main").Range("AC" & r.Row - p + 2 & ":AH" & r.Row)

    Would that set the entire range equal to the new range, or just paste the first value?

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,522

    Re: How can avoid .copy and .paste in this loop?

    Re: "or just paste the first value?"
    What did it show when you tried it?

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

    Re: How can avoid .copy and .paste in this loop?

    If you use Copy and then PasteSpecial you will get the "marching ants" on the "from" range and you can you
    Please Login or Register  to view this content.
    to turn it off after the paste.

    The line you quoted should copy the whole range. Also, it just copies the values, not formats, but that might be OK for your situation. And as jolivanes suggests, you can learn a lot by trying stuff out.

  8. #8
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: How can avoid .copy and .paste in this loop?

    Thanks, 6String for all of your advice and help. I've added reputation to your account. I'm completely self-taught, so I'm no stranger to trying things out! :D

+ 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. Data Validation - Avoid overwriting due to copy/paste
    By amitgaja007 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-18-2015, 01:29 AM
  2. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  3. Macro to Copy and Paste Special Value to avoid Circular Reference
    By alpha608 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-06-2013, 01:41 PM
  4. 1 sheet same data different cells (anyway to copy paste to avoid manual labor)
    By fruitypebbs in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-08-2010, 07:06 PM
  5. Replies: 0
    Last Post: 09-10-2007, 10:22 AM
  6. [SOLVED] Copy/Paste how to avoid the copy of formula cells w/o calc values
    By Dennis in forum Excel General
    Replies: 10
    Last Post: 03-02-2006, 06:50 PM
  7. [SOLVED] How avoid errors when you Copy chartobjects paste in powerpoint
    By Gunnar Johansson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2005, 08:06 AM

Tags for this Thread

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