简体   繁体   中英

Copy and Paste Rows to another worksheet based on a cells value

Summary: I have a tab called "Working List", in column AI is a Yes/No/Blank field. Once a day (may use a button, may create a power automate flow, not sure yet), we want to run a script that moves the "Yes" rows to another sheet called "Completed" (paste as values).

I am super new to script and not great at VBA (working on it), so I have read as much as I could but am not quite understanding how to make this work. I originally had a macro setup but quickly learned that online workbooks don't like that...

The struggle mainly has been that the "working list" tab ( Working List Tab ); columns B - AH is a pivot table (power query connection to a Power BI data set). Column A & AI - AN are tables just because I wanted to see if that made a difference (Columns A = table name "Claimed"; AI - AN = table name "Completed_WL"). The "Completed" tab is fully a table (table name - table2), in case that makes a difference.

I need the full row A - AN to be pasted as values. I don't need it to delete the rows - once the data set updates it will filter out.

I have tried using the "record" option, but it only copies and pastes the first row. I am not sure what to add/change to copy visible only:

function main(workbook: ExcelScript.Workbook) {
    let completed_WL = workbook.getTable("Completed_WL");   
    completed_WL.getColumnByName("Completed").getFilter().applyValuesFilter(["Yes"]);
    let table2 = workbook.getTable("Table2");
    let selectedSheet = workbook.getActiveWorksheet();
                   table2.getColumn("Claimed").getRangeBetweenHeaderAndTotal().getRow(0).copyFrom(selectedSheet.getRange("A5:AN5"), ExcelScript.RangeCopyType.values, false, false);
        completed_WL.getColumnByName("Completed").getFilter().clear();
}

Any help would be greatly appreciated!!

You can try updating your code to use a variable to get the last row. After you did that, you can integrate that into your copy statement to copy the data. You can see how that would look in the code below:

function main(workbook: ExcelScript.Workbook) {
  let completed_WL = workbook.getTable("Completed_WL");
  completed_WL.getColumnByName("Completed").getFilter().applyValuesFilter(["Yes"]);
  let table2 = workbook.getTable("Table2");
  let selectedSheet = workbook.getActiveWorksheet();
  let lastRow = selectedSheet.getCell(selectedSheet.getRange().getRowCount() - 1, 0).getRangeEdge(ExcelScript.KeyboardDirection.up)
  table2.getColumn("Claimed").getRangeBetweenHeaderAndTotal().getRow(0).copyFrom(selectedSheet.getRange(`A5:AN${lastRow}`), ExcelScript.RangeCopyType.values, false, false);
  completed_WL.getColumnByName("Completed").getFilter().clear();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM