Good Day,
a helpful voice in the general Excel help forum pointed me to ask my question here, I'll post the original topic here then explain what else I've tried so far.
I'm not a programmer, I have very few programming skills and while I managed one of the functions from VBA that my company needs in Excel Web / Office Script, the second function eludes me.
What I'm trying to do is sort a large table by clicking a button after I've selected a cell.
For example, I click in Column H the number "A53434", when I click the button, it should sort the table so only entries that have "A53434" in Column H. I feel like this shouldn't be so hard but I'm struggling quite a lot.
Here is the code a previous employee did in Excel VBA:
Sub SetFilter()
With ActiveSheet
.Range("A3:R3").AutoFilter
.Range("A3:R" & .Cells(Rows.Count, "A").End(xlUp).Row).AutoFilter _
Field:=ActiveCell.Column, Criteria1:=ActiveCell.Value
End With
End Sub
So far I've gotten this far in Office Script, but I have very little idea what I'm doing or what I'm doing wrong:
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
let activeCell = workbook.getActiveCell().getValue() as string;
console.log(activeCell);
//just to see if I actually have the active cell
let activeColumn = workbook.getActiveCell().getAddress().split('')[0];
let table = selectedSheet.getTables()[0];
//above line gives error
var sortCl = table.getColumnByName(activeColumn);
}
Please help a struggling non-programmer out, thanks.
After a bit back and forth I've gotten this far and I think I understand it
function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
let activeCell = workbook.getActiveCell();
let activeColumn = activeCell.getAddress().split('')[10];
//this gives me the Columns letter as the full Adress is a 9 letter word and ! then the column index
let activeColumnINdex = activeCell.getColumnIndex();
//this gives me the columns Index as a numerical value, which one I need? No idea.
console.log(activeColumn)
let table = selectedSheet.getTables()[0];
let filter = table.getAutoFilter();
filter.clearCriteria();
filter.apply(selectedSheet.getAutoFilter().getRange(),activeColumnINdex, filter);
// I think I understand the apply() function, first I give it a range (all of it) then the Column Index (A=0, B=1 etc.) but then I try to give it a criteria, which ought to be the currently active cell and I don't know how to do that.
}