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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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