2

In my code $CoName won't always be perfect and will need adjusted slightly. $CoFixes fixes this issue. But when I run it as shown below, $CoName never makes it to $cell. I need to reuse the code in $CoFixes quite a few times so I'm trying to learn how to make this work.

$CoFixes = {
if ($CoName -eq "L.F. 10' Panel w/o lath"){$CoName = "L.F. of 10' Panel w/o lath"}
if ($CoName -eq "L.F. 9' Panel w/o lath"){$CoName = "L.F. of 9' Panel w/o lath"}
if ($CoName -eq "L.F. 8'2`" Panel w/o lath"){$CoName = "L.F. of 8'2`" Panel w/o lath"}
if ($CoName -eq "L.F. 4' Panel w/o lath"){$CoName = "L.F. of 4' Panel w/o lath"}
if ($CoName -eq "L.F. 4' Panel w/ 8`" top w/o lath"){$CoName = "L.F. of 4' Panel w/ 8`" top w/o lath"}
if ($CoName -eq 'Special Window Openings over 27"'){$CoName = 'Special Window Openings over 37"'}
if ($CoName -eq 'Door Opening up to 41.5" wide'){$CoName = 'Door Opening up to 41 1/2" wide'}
}

$CoName = $ChangeOrder1Worksheet.range('B20').text
&$CoFixes
$cell = $QuoteSheet.range('B1:B60').Find($CoName).offset(0, 3).address(0,0)
$value = $ChangeOrder1Worksheet.range('A20').text
&$vba
$objExcel.run("ChangeOrder", $cell, $value)
write-host $CoName " " $cell " " $value " " $QuoteSheet.range($cell).text

2 Answers 2

5

You have a scope issue. Variables in a parent scope are accessible from a child scope, but once you write to them, they are copied into the local scope and that's what you are modifying.

Treat this anonymous function you're creating as a function, and return the value instead:

$CoFixes = {
if ($CoName -eq "L.F. 10' Panel w/o lath"){"L.F. of 10' Panel w/o lath"}
elseif ($CoName -eq "L.F. 9' Panel w/o lath"){"L.F. of 9' Panel w/o lath"}
elseif ($CoName -eq "L.F. 8'2`" Panel w/o lath"){"L.F. of 8'2`" Panel w/o lath"}
elseif ($CoName -eq "L.F. 4' Panel w/o lath"){"L.F. of 4' Panel w/o lath"}
elseif ($CoName -eq "L.F. 4' Panel w/ 8`" top w/o lath"){"L.F. of 4' Panel w/ 8`" top w/o lath"}
elseif ($CoName -eq 'Special Window Openings over 27"'){'Special Window Openings over 37"'}
elseif ($CoName -eq 'Door Opening up to 41.5" wide'){'Door Opening up to 41 1/2" wide'}
else { $CoName }
}

$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = &$CoFixes

To make that more idiomatic, try a switch:

$CoFixes = {
    switch($CoName)
    {
        "L.F. 10' Panel w/o lath" {"L.F. of 10' Panel w/o lath"}
        "L.F. 9' Panel w/o lath" {"L.F. of 9' Panel w/o lath"}
        "L.F. 8'2`" Panel w/o lath" {"L.F. of 8'2`" Panel w/o lath"}
        "L.F. 4' Panel w/o lath" {"L.F. of 4' Panel w/o lath"}
        "L.F. 4' Panel w/ 8`" top w/o lath" {"L.F. of 4' Panel w/ 8`" top w/o lath"}
        'Special Window Openings over 27"' {'Special Window Openings over 37"'}
        'Door Opening up to 41.5" wide' {'Door Opening up to 41 1/2" wide'}
        default { $Name }
    }
}

$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = &$CoFixes

Then maybe put it in a real function:

function Repair-CoName {
    param(
        [String]
        $Name
    )

    switch($Name)
    {
        "L.F. 10' Panel w/o lath" {"L.F. of 10' Panel w/o lath"}
        "L.F. 9' Panel w/o lath" {"L.F. of 9' Panel w/o lath"}
        "L.F. 8'2`" Panel w/o lath" {"L.F. of 8'2`" Panel w/o lath"}
        "L.F. 4' Panel w/o lath" {"L.F. of 4' Panel w/o lath"}
        "L.F. 4' Panel w/ 8`" top w/o lath" {"L.F. of 4' Panel w/ 8`" top w/o lath"}
        'Special Window Openings over 27"' {'Special Window Openings over 37"'}
        'Door Opening up to 41.5" wide' {'Door Opening up to 41 1/2" wide'}
        default { $CoName }
    }
}

$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = Repair-CoName -Name $CoName

etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the information. That makes sense to me and helped me get over the hump. It doesn't simply put the code in place instead it acts like a function like you said.
2

Its because of the scoping.

$Variable1 = 1
& {
    $Variable1 = 2 #This will create a new local variable and visible only inside the scriptblock
    "Local variable is $Variable1"
    "Global Variable is $Global:Variable1"  
}

Execute above script, you can easily understand.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.