0

Using Excel Script how can you change the font name of cells on a sheet if they have a specific Value?

I need to change cells to font name "Wingdings" if the cell has a "ü" as the value. The regular font name is "Calibri". The sheet range is $1:$1048576.

Was originally doing VBA which I need to transfer to ExcelScript.

VBA code:

Sub FormatText(ByVal Target As Range)

If Target.Range("$1:$1048576") Then
    If Target.Value = "ü" Then
    
        Target.Font.Name = "Wingdings"
    
    Else
    
        Target.Font.Name = "Calibri"
    
    End If

End If

End Sub
1

1 Answer 1

0

This task can be accomplished using Excel script code. For optimal efficiency, the code should check only the relevant range of cells (UsedRange) instead of iterating through every cell on the worksheet.

function main(workbook: ExcelScript.Workbook) {
    let sheet = workbook.getActiveWorksheet();
    let targetRange = sheet.getUsedRange();
    targetRange.getFormat().getFont().setName("Calibri");
    let values = targetRange.getValues();
    values.forEach((rowArray, rowIndex) => {
        rowArray.forEach((cellValue, colIndex) => {
            if (cellValue === "ü") {
                let cell = targetRange.getCell(rowIndex, colIndex);
                cell.getFormat().getFont().setName("Wingdings");
            }
        });
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.