Excel script Cope paste with filters

michalina

New Member
Joined
Jan 23, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,
Apologies if this is not the right place to ask this question.

I'm automating a task in my report, however my script stops right after filer is done in the first table, and i'm not sure what to do next.
What i wanted from this script:
1. filter table employees to get only False value in column Help
2. Copy last visible cell from column Employee Basic Data name
3. Paste it at the end of table PT in sheet Planning in column Employee Basic Data name
4. copy section B2-B12 from sheet Lists
5. paste in the fist empty cell in table PT in sheet Planning in column Hours classification
6. Copy last non empty cell in table PT in sheet Planning in column Employee Basic Data name and paste it 11 times below

Below is my current script:
JavaScript:
function main(workbook: ExcelScript.Workbook) {
  let employeesSheet = workbook.getWorksheet("Employees");
  let planningSheet = workbook.getWorksheet("Planning");
  let listsSheet = workbook.getWorksheet("Lists");
  let Employees = workbook.getTable("Employees")
  let PT = workbook.getTable("PT")
  // Repeat until there is no "False" value in column "Help" in sheet "Employees"
  while (Employees.getColumnByName("Help").getFilter().applyValuesFilter(["FALSE"]))  {
    // Get the last row in column "Help" in the "Employees" sheet
    let lastRowHelp = Employees.getRange("Help").getUsedRange().getLastCell().getRowIndex();
    {
      // Copy the last cell from column "Employee Basic Data name"
      let lastEmployeeName = Employees.getRange("Employee Basic Data name").getUsedRange().getLastCell();
      lastEmployeeName.load("values");
      // Get the last row in column "Employee Basic Data name" in the "Planning" sheet
      let lastRowPlanning = PT.getRange("Employee Basic Data name").getUsedRange().getLastCell().getRowIndex();
      // Paste the last employee name at the bottom of the "Employee Basic Data name" column in the "Planning" sheet
      PT.getRange(`Employee Basic Data name[${lastRowPlanning + 1}]`).setValues(lastEmployeeName.values);
      // Copy area B2:B12 from the "Lists" sheet
      let hoursClassificationRange = listsSheet.getRange("B2:B12");
      hoursClassificationRange.load("values");
      // Get the last row in column "Hours classification" in the "Planning" sheet
      let lastRowHoursClassification = planningSheet.getRange("Hours classification").getUsedRange().getLastCell().getRowIndex();
      // Paste it in the last row of column "Hours classification" in the "Planning" sheet
      planningSheet.getRange(`Hours classification[${lastRowHoursClassification + 1}]`).setValues(hoursClassificationRange.values);
      // Copy the last nonempty cell from column "Employee Basic Data name" in the "Planning" sheet
      let lastPlanningName = planningSheet.getRange("Employee Basic Data name").getUsedRange().getLastCell();
      lastPlanningName.load("values");
      // Paste it 11 times below
      for (let i = 1; i <= 11; i++) {
        planningSheet.getRange(`Employee Basic Data name[${lastRowPlanning + i + 1}]`).setValues(lastPlanningName.values);
      }
      }
    }
  }
 
Last edited by a moderator:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top