Power Automate Desktop - Excel Find & Replace Automation
Power Automate Desktop - Excel Find & Replace Automation
This desktop flow allows the user to select an Excel file and perform a Find & Replace operation on the contents of the worksheet. The user can choose whether to replace all occurrences of a value or only the first occurrence.
1. Display Custom Form
The flow begins by opening a custom Adaptive Card form. The form contains fields where the user can:
- Enter the text to find
- Enter the replacement text
- Select an Excel file (
xls,xlsx,csv) - Choose the replace mode: Find & replace all or Find & replace first
2. Check for Cancel
If the user presses Cancel, the flow stops immediately. Otherwise, it continues with the replacement process.
3. Launch Excel
The selected Excel file is opened in editable mode using the Launch Excel Under Existing Process action. This ensures the file is opened inside an already running Excel instance.
4. Perform Find & Replace
The flow checks the user’s selection:
- If the user selected "Find & replace all" The action FindAndReplaceAll updates all matching text in the worksheet.
- If the user selected "Find & replace first" The action FindAndReplaceSingle updates only the first matched value.
5. Save & Close Excel
After completing the replacement process, the Excel file is saved and closed using CloseAndSave.
Complete PAD Code Used
# The following action displays a custom form and prompts you to select a file, the values you want to replace, and the values you want to replace them with.
@@statistics_TextBlock: '1'
@@statistics_Input_Text: '2'
@@statistics_Input_File: '1'
@@statistics_Input_ChoiceSet: '1'
@@statistics_Action_Submit: '2'
Display.ShowCustomDialog CardTemplateJson: '''{
\"type\": \"AdaptiveCard\",
\"version\": \"1.4\",
\"id\": \"AdaptiveCard\",
\"body\": [
{
\"type\": \"TextBlock\",
\"id\": \"Text block\",
\"text\": \"${Text_block_Text}\",
\"wrap\": true
},
{
\"type\": \"Input.Text\",
\"id\": \"TextToFind\",
\"isRequired\": true,
\"label\": \"${TextToFind_Label}\"
},
{
\"type\": \"Input.Text\",
\"id\": \"NewText\",
\"isRequired\": true,
\"label\": \"${NewText_Label}\"
},
{
\"type\": \"Input.File\",
\"id\": \"File input\",
\"value\": \"\",
\"isRequired\": true,
\"label\": \"${File_input_Label}\",
\"errorMessage\": \"${File_input_ErrorMessage}\"
},
{
\"type\": \"Input.ChoiceSet\",
\"id\": \"RenameFunction\",
\"value\": \"${RenameFunction_Value}\",
\"style\": \"compact\",
\"isMultiSelect\": false,
\"choices\": [
{
\"title\": \"Find & replace all\",
\"value\": \"all\"
},
{
\"title\": \"Find & replace first\",
\"value\": \"first\"
}
]
}
],
\"actions\": [
{
\"type\": \"Action.Submit\",
\"id\": \"Submit\",
\"title\": \"${Submit_Title}\"
},
{
\"type\": \"Action.Submit\",
\"id\": \"Cancel\",
\"associatedInputs\": \"none\",
\"title\": \"${Cancel_Title}\",
\"IsCancel\": true
}
]
}''' CustomFormData=> CustomFormData ButtonPressed=> ButtonPressed @Text_block_Text: $'''This desktop flow searches and replaces the provided values from the selected worksheet. Note that the edited file will be saved.''' @TextToFind_Label: $'''Text to find: ''' @NewText_Label: $'''Text to replace with:''' @File_input_Label: $'''Please select the Excel file (*.xls,*.xlsx,*.csv) you want to search text in: ''' @File_input_ErrorMessage: $'''Please provide a path to the Excel file. ''' @RenameFunction_Value: $'''all''' @Submit_Title: $'''Replace & save''' @Cancel_Title: $'''Cancel'''
# The following 'If' action checks whether the 'Cancel' button was pressed in the custom form. If not, the flow replaces the provided values.
IF ButtonPressed <> $'''Cancel''' THEN
Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: CustomFormData['File input'] Visible: True ReadOnly: False Instance=> ExcelInstance
# The following 'If' block checks the selected renaming function in the custom form and performs the appropriate renaming method in the Excel file.
IF CustomFormData['RenameFunction'] = $'''all''' THEN
Excel.FindAndReplace.FindAndReplaceAll Instance: ExcelInstance TextToFind: CustomFormData['TextToFind'] TextToReplaceWith: CustomFormData['NewText'] MatchCase: False MatchEntireCellContents: False SearchBy: Excel.SearchOrder.Rows Cells=> Cells
ELSE IF CustomFormData['RenameFunction'] = $'''first''' THEN
Excel.FindAndReplace.FindAndReplaceSingle Instance: ExcelInstance TextToFind: CustomFormData['TextToFind'] TextToReplaceWith: CustomFormData['NewText'] MatchCase: False MatchEntireCellContents: False SearchBy: Excel.SearchOrder.Rows FoundColumnIndex=> FoundColumnIndex FoundRowIndex=> FoundRowIndex
END
Excel.CloseExcel.CloseAndSave Instance: ExcelInstance
END
.gif)
Comments
Post a Comment