Forum Discussion
VBA to Office Script Help
To convert your VBA code to an Office Script that can be used in Excel Online or in the Teams version of Excel, we'll need to rewrite the code using the syntax and features supported by Office Scripts. Unfortunately, not all VBA functionality is available in Office Scripts, so we may need to make some adjustments to your code.
Below is a version of your VBA code converted to an Office Script. This script will be triggered whenever a change is made in the worksheet, similar to the Worksheet_Change event in VBA:
Javascript code is untested, please backup your file.
function main(workbook: ExcelScript.Workbook, sheet: ExcelScript.Worksheet, changedRange: ExcelScript.Range) {
const KEYWORD: string = "Discontinued";
const UID: string = "Unique ID";
const DEST_SHT: string = "Archived_CVL";
let targetCell = changedRange.getResizedRange(0, -8);
let targetValue = targetCell.getValue();
if (targetValue && typeof targetValue === 'string' && targetCell.getColumn() === 9 && targetCell.getRow() > 3 && targetValue.toLowerCase() === KEYWORD.toLowerCase()) {
let listObject = sheet.getListObjects()[0];
let sID = sheet.getRange(targetCell.getRow(), 1).getValue();
if (listObject) {
listObject.getAutoFilter().clear();
listObject.getAutoFilter().applyFilter(1, sID);
let visibleRange = listObject.getRange().getSpecialCells(ExcelScript.SpecialCellType.visible);
if (visibleRange) {
let destSheet = workbook.getWorksheet(DEST_SHT);
let destRange = destSheet.getRange(destSheet.getUsedRange().getRowCount() + 1, 1);
visibleRange.copyTo(destRange, ExcelScript.CopyToType.all, false);
visibleRange.delete(ExcelScript.DeleteShiftDirection.up);
}
}
}
}
Here are some key differences and considerations:
- We use TypeScript syntax for Office Scripts, which is similar to JavaScript.
- We use the main function as the entry point for the script.
- We use the getResizedRange method to adjust the target range by moving 8 columns to the left.
- We use methods like getValue, getRange, getListObjects, getAutoFilter, and getSpecialCells to manipulate the worksheet data and objects.
- We use toLowerCase() to perform case-insensitive string comparison.
Please note that Office Scripts are still evolving, and not all VBA functionality is available. You may need to adapt your code further or explore alternative solutions depending on your requirements and the features available in Office Scripts. The text, steps and the script was created with the help of AI.
Formularbeginn
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and like it!
This will help all forum participants.
- jeffrey_mccoyMay 25, 2024Copper Contributor
I need some help rewriting this Excel VB script to an Office Script please. The VB scripts checks for duplicate cells in the columns specified, and then merges them and centers them into 1 large merged cell that takes up multiple rows, while preserving the individual cell information in columns B - P that are not specified in the routine. I appreciate any help you can provide. thank you!!
Sub MergeCells()
'set your data rows here
Dim Rows As Integer: Rows = 500Dim First As Integer: First = 1
Dim Last As Integer: Last = 0
Dim Rng As RangeApplication.DisplayAlerts = False
With ActiveSheet
For i = 1 To Rows + 1
If .Range("A" & i).Value <> .Range("A" & First).Value Then
If i - 1 > First Then
Last = i - 1Set Rng = .Range("A" & First, "A" & Last)
Rng.MergeCells = True
Set Rng = .Range("E" & First, "E" & Last)
Rng.MergeCells = True
Set Rng = .Range("F" & First, "F" & Last)
Rng.MergeCells = True
Set Rng = .Range("G" & First, "G" & Last)
Rng.MergeCells = True
Set Rng = .Range("H" & First, "H" & Last)
Rng.MergeCells = True
Set Rng = .Range("M" & First, "M" & Last)
Rng.MergeCells = True
Set Rng = .Range("N" & First, "N" & Last)
Rng.MergeCells = True
Set Rng = .Range("O" & First, "O" & Last)
Rng.MergeCells = TrueEnd If
First = i
Last = 0
End If
Next i
End With
Application.DisplayAlerts = True
End Sub- NikolinoDEMay 26, 2024Platinum Contributor
Office Scripts for Excel on the web are similar to VBA but have some key differences in syntax and functionality. Here is how you can rewrite your VBA script to an Office Script to merge and center cells based on the specified columns:
Office Script
Code is untested please backup your file.
function main(workbook: ExcelScript.Workbook) { let sheet = workbook.getActiveWorksheet(); let rowCount = 500; // Set your data rows here let first = 0; let last = 0; for (let i = 0; i <= rowCount; i++) { let currentCellValue = sheet.getRange(`A${i+1}`).getValue(); let firstCellValue = sheet.getRange(`A${first+1}`).getValue(); if (currentCellValue !== firstCellValue) { if (i - 1 > first) { last = i - 1; mergeRange(sheet, first, last, "A"); mergeRange(sheet, first, last, "E"); mergeRange(sheet, first, last, "F"); mergeRange(sheet, first, last, "G"); mergeRange(sheet, first, last, "H"); mergeRange(sheet, first, last, "M"); mergeRange(sheet, first, last, "N"); mergeRange(sheet, first, last, "O"); } first = i; last = 0; } } } function mergeRange(sheet: ExcelScript.Worksheet, first: number, last: number, column: string) { let rangeAddress = `${column}${first + 1}:${column}${last + 1}`; let range = sheet.getRange(rangeAddress); range.merge(true); range.getFormat().setHorizontalAlignment(ExcelScript.HorizontalAlignment.center); range.getFormat().setVerticalAlignment(ExcelScript.VerticalAlignment.center); }
Explanation
- Setting Up the Script: The script starts by defining the worksheet and the number of rows to process.
- Loop Through Rows: It loops through the rows to find duplicates in column A.
- Merge and Center Cells: When a duplicate is found, the script merges and centers the cells in the specified columns (A, E, F, G, H, M, N, O).
- Helper Function: The mergeRange function handles the merging and centering of a specified range within a column.
Running the Script
- Open Excel Online: Open your workbook in Excel Online.
- Access Office Scripts: Go to the "Automate" tab and select "Code Editor."
- Create New Script: Paste the script above into a new script and save it.
- Run the Script: Execute the script to perform the merge and center operation.
This Office Script achieves the same functionality as your original VBA script but is adapted to work in the Excel Online environment using TypeScript.
NOTE: My knowledge about Office Script is limited so I entered your question in the AI. The text and the steps are the result of the AI. Maybe it will help you further in your project, if not please just ignore it.
- NikolinoDEDec 15, 2024Platinum Contributor