+ Reply to Thread
Results 1 to 6 of 6

Office Script to Copy Rows From One Worksheet To Another

  1. #1
    Registered User
    Join Date
    12-10-2018
    Location
    USA
    MS-Off Ver
    2016
    Posts
    31

    Office Script to Copy Rows From One Worksheet To Another

    I have a shared excel workbook on SharePoint that is being used by a few users. This sheet tracks reach outs to counties across the United States. When a reach out turns into a lead it then gets copied to a separate worksheet to expand on from there. I attempted to write a script that would accomplish this but failed miserably. What I am trying to accomplish with this script is in the "US Counties" worksheet, column O will have the option to move that row to the "RSR" worksheet. The script would then copy data from that row and place it in a new row in the "RSR" worksheet. There is a little shift in the columns between sheets which I have mapped out in the table below. Any help or direction is greatly appreciated.

    Sample Sheet - https://widewaters.sharepoint.com/:x...kDOnA?e=Tx0isn

    Cross Posted Links
    Stack Overflow - https://stackoverflow.com/questions/...37441_78798715
    Mr.Excel - https://www.mrexcel.com/board/thread...other.1262179/

    COUNTIES.png

    0bL5bmHC.png
    Last edited by SlightlyClueless; 07-30-2024 at 12:39 PM.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,442

    Re: Office Script to Copy Rows From One Worksheet To Another

    Please post a sample copy of your workbook with a few example entries. Images are nice but very difficult to work with. Thanks.

  3. #3
    Registered User
    Join Date
    12-10-2018
    Location
    USA
    MS-Off Ver
    2016
    Posts
    31

    Re: Office Script to Copy Rows From One Worksheet To Another

    Link to a sample sheet - https://widewaters.sharepoint.com/:x...kDOnA?e=Tx0isn

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    91,060

    Re: Office Script to Copy Rows From One Worksheet To Another

    Administrative Note:

    We would very much like to help you with your query, however it has been brought to our attention that the same query has been posted on one or more other forums and you have not told us about this. You are required to do so. Cross-posts are allowed but you must provide a link to your posts on other sites.

    Please see Forum Rule #7 about cross-posting and adjust accordingly. Read this to understand why we (and other sites like us) consider this to be important: https://excelguru.ca/a-message-to-forum-cross-posters/

    (Note: this requirement is not optional. No further help to be offered until you provide a link or, for members with fewer than 10 posts, a comment telling us where else you have posted this query.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  5. #5
    Registered User
    Join Date
    12-10-2018
    Location
    USA
    MS-Off Ver
    2016
    Posts
    31

    Re: Office Script to Copy Rows From One Worksheet To Another

    My apologies about that! I have modified the original post to include link to the 2 other sites the question has been posted to.

  6. #6
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,086

    Re: Office Script to Copy Rows From One Worksheet To Another

    Something along these lines, perhaps.

    PHP Code: 
    function main(workbookExcelScript.Workbook,Confirmstring "Y") {
    // Get the source worksheets
        
    let sourceSheet workbook.getWorksheet("US Counties");
        
    let targetSheet workbook.getWorksheet("RSR");

    //check that the sheets exist and that the active sheet is the source sheet
        
    if (targetSheet == undefined) {
            
    console.log("RSR sheet doesn't exist");
            return;
        }

        if (
    sourceSheet == undefined) {
            
    console.log("US Counties sheet doesn't exist");
            return;
        }
        
    console.log(workbook.getActiveWorksheet().getName());
        if (
    workbook.getActiveWorksheet().getName() != sourceSheet.getName()) {
            
    console.log("Please select the cell in Us Counties whose row you wish to copy");
            return;
        }
    //Get the copy and paste rows on the respective sheets
        
    var targetSheet.getRange("A1").getSurroundingRegion().getLastRow().getRowIndex()+2;
        var 
    ar workbook.getActiveCell().getRowIndex() + 1;

    //Assign the Copy/Paste ranges to variables
        
    var rngToCopy1 sourceSheet.getRange("A" ar  ":B" ar);
        var 
    rngToPaste1 targetSheet.getRange("B" ":C" r);

        var 
    rngToCopy2 sourceSheet.getRange("G" ar ":H" ar);
        var 
    rngToPaste2 targetSheet.getRange("J" ":K" r);

        var 
    rngToCopy3 sourceSheet.getRange("L" ar ":N" ar);
        var 
    rngToPaste3 targetSheet.getRange("L" ":N" r);
     
        var 
    rngToCopy4 sourceSheet.getRange("P" ar ":R" ar);
        var 
    rngToPaste4 targetSheet.getRange("W" ":Y" r);
        
    //Copy the ranges
        
    rngToPaste1.copyFrom(rngToCopy1);
        
    rngToPaste2.copyFrom(rngToCopy2);
        
    rngToPaste3.copyFrom(rngToCopy3);
        
    rngToPaste4.copyFrom(rngToCopy4);

        
    console.log("Done")

    Last edited by ByteMarks; 07-31-2024 at 09:08 AM.

+ 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. [SOLVED] Translate VB Code into Office Script
    By AliGW in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-17-2024, 03:15 AM
  2. Office Script (Not VBA) To Grab Username who Initiated Script?
    By Norcal1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-16-2023, 04:15 AM
  3. Office Script code to print to PDF
    By XescV in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-07-2023, 01:46 PM
  4. 365 Script to copy and add rows then clear contents on a range of cells
    By dejan720 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-07-2023, 09:07 AM
  5. [SOLVED] VBA script is using ALL rows in worksheet causing large file size and slow performance
    By eyanosa in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2023, 02:21 PM
  6. [SOLVED] Script to Search for Indicator and then copy certain fields, and paste on new worksheet
    By powda in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2013, 06:39 AM
  7. Replies: 3
    Last Post: 01-04-2006, 05:47 PM

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