Filter Worksheet Names: That name is already taken. Try a different one using PowerShell

Viewed 24

I am struggling filtering the same worksheet names. Can anyone help me T.T I've been doing these for months now, and It still gives me the That name is already taken. Try a different one. and it just keeps on looping like that.

What I'm trying to do here is to Add Another Worksheet if the Worksheet name is not existing, and if it does Exist, it will do something inside with the same Worksheet name. I'm stuck with this error, and I did a lot of research but it not compliments with the code I'm working in.

#Create a New workbook
$Dest = $Excel.Workbooks.Add()
$DestSheets = $Dest.Worksheets

#Open the First file
$Source = $Excel.Workbooks.Open($Files[0])
$FileName = $Source.Name
$SourceSheets = $Source.Worksheets

#Create an Array for Key
$Array = @()

ForEach ($Sheet in $SourceSheets){
    # Copy the desired Range of each current Sheets
    $allRows = $Sheet.Range("L6","M$(($Sheet.UsedRange.Rows|Select -Last 1).Row)")

    $OutputSheet = $DestSheets.Add()
    $OutputSheet.Name = $Sheet.Name

    ForEach ($OutSheet in $DestSheets) {

    # Write the File Name as a Header
    $OutSheet.Cells.Item(1,1) = "Key"
    $OutSheet.Cells.Item(1,2) = $FileName

        If([String]::IsNullOrEmpty($Dest.ActiveSheet.Range("A2").Value2)){ 
            $allRows.Copy() | Out-Null
            [void]$Dest.Activate()
            [void]$Dest.ActiveSheet.Range("A2").PasteSpecial(-4163)
        }

    $OutSheet.UsedRange.EntireColumn.AutoFit() | Out-Null
    }
    # Add the Key to the Array
    foreach ($i in $allRows.Count){
        $Array += New-Object PSObject -Property @{'Key' = $Sheet.Range("L6","L$(($Sheet.UsedRange.Rows|Select -Last 1).Row)").Value2;}
    }
}
$Dest.SaveAs("$ParentFolder\Output Folder\SampleOutput2.xlsx")
$Dest.Close()

# For All Files except File1
$Dest = $Excel.Workbooks.Open("$ParentFolder\Output Folder\SampleOutput2.xlsx")
$DestSheets = $Dest.Worksheets

ForEach($File in $Files){

    If ($File -eq $Files[1]) {
    Echo "Found File2"
        $Source1 = $Excel.Workbooks.Open($File)
        $Source1.Name
        $FileName1 = $Source1.Name
        $SourceSheets1 = $Source1.Worksheets
        
        ForEach ($Sheets1 in $SourceSheets1) {

            ForEach ($OutSheet in $DestSheets) {

                If ($Sheets1 -Match $OutSheet) {
                    Echo "WorkSheet Name Exists, Do Something Inside the Same WorkSheet"
                    $OutSheet.Cells.Item(1,3) = $FileName1
                }
                Else {
                    # ---> This is where I'm getting the error at:
                    Echo "No Matched WorkSheet Name, Add the New WorkSheet"
                    $OutputSheet = $DestSheets.Add()
                    $OutputSheet.Name = $Sheets1.Name
                }
            }
        }
    }
    Else {
    Echo "No File2"
    }
}

I really need help, and also asked this question with other forums but no luck. :(

0 Answers
Related