I used the Automate feature in excel to automate some manual tasks that are performed every day at my job. I modified it a little to obtain the code below. My issue is that when copying data that is filtered (by value= “Virginia”), and pasting it, I am getting all the rows, instead of only the filtered ones. How can I accomplish this? Please see code below.
(I am not very experienced with TypeScript; I appreciate the help)
function main(workbook: ExcelScript.Workbook) {
let yesterdays_Open_PO_File = workbook.getWorksheet("Yesterdays Open PO File");
// Clear ExcelScript.ClearApplyTo.contents from all cells on yesterdays_Open_PO_File
yesterdays_Open_PO_File.getRange().clear(ExcelScript.ClearApplyTo.contents);
let open_PO_File = workbook.getWorksheet("Open PO File");
// Paste to range A1 on yesterdays_Open_PO_File from range A:AA on open_PO_File
yesterdays_Open_PO_File.getRange("A1").copyFrom(open_PO_File.getRange("A:AA"), ExcelScript.RangeCopyType.values, false, false);
// Refresh all data connections
workbook.refreshAllDataConnections();
// Get the autoFilter of the first table in the Open PO File PO worksheet.
const PODataNBR = workbook.getWorksheet("PO Data NBR Source");
const dataRange = PODataNBR.getUsedRange();
const autoFilter = PODataNBR.getAutoFilter();
// Filter to only include values that equal "Virginia". **--> This is the filter**
autoFilter.apply(dataRange, 26, {
criterion1: "Virginia",
filterOn: ExcelScript.FilterOn.custom
});
// Clear ExcelScript.ClearApplyTo.contents from range A:AA on open_PO_File
open_PO_File.getRange("A:AA").clear(ExcelScript.ClearApplyTo.contents);
let pO_Data_NBR_Source = workbook.getWorksheet("PO Data NBR Source");
// Paste to range A1 on open_PO_File from range A:AA on pO_Data_NBR_Source --> **This is where I want to paste the filtered data**
open_PO_File.getRange("A1").copyFrom(pO_Data_NBR_Source.getRange("A:AA"), ExcelScript.RangeCopyType.values, false, false);
let firm_DMD = workbook.getWorksheet("Firm DMD");
// Clear ExcelScript.ClearApplyTo.contents from range A:R on firm_DMD
firm_DMD.getRange("A:R").clear(ExcelScript.ClearApplyTo.contents);
let selectedSheet = workbook.getActiveWorksheet();
// Paste to range A1 on firm_DMD from range A:Q on selectedSheet
firm_DMD.getRange("A1").copyFrom(selectedSheet.getRange("A:Q"), ExcelScript.RangeCopyType.values, false, false);
// Refresh all data connections
workbook.refreshAllDataConnections();
}
Bascax is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.