Powershell excel merge cells when cell could vary
The :
character when used in a variable name specifies the scope. So PowerShell is seeing $x:C
as variable C
in scope x
.
By enclosing x
in curly braces, then PowerShell will not look ahead in the string.
$MergeCells = $objWorksheet.Range("B${x}:C${x}")
Since this is a formula problem, might be easier if you upload your sheet to Google sheets and use the add-ons feature and add Formula Helper. I'm using it myself. It looks for complicated formulas and helps you to solve it."