Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

[powershell] Help Deleting Rows in an excel document

$
0
0

Good afternoon everyone,

I  have tried everything to get this to work.  I feel like I am getting the dumbest error and I know its going to be like change this one thing and it will work correctly.  So here is my issue, I have a script that will open a file that was dumped out from an Oracle DB.  It is a .OUT file, it imports correctly but I have to clean it up.  So I use the script to open excel and open the .OUT file.  The next part of my script deletes the top 8 line because I don't need them, and then copies a formula into row E and drags it down.

Here is the issue with the next part.  The next part (Function Delete Rows) will either give me the error "Exception from hresult: 0x800a03ec" or will skip over the $null rows and go directly to the other rows.  I also need to make it so that it subtracts a row from the row count so it doesn't go to the 900k+ rows because when deleting the rows it should decrease the # of rows as well.  I also have an issue saving the file.  If you guys can offer any help I'd GREATLY appreciate it.


#a function I found online for practicing good hygiene
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}

cls

Function DeleteRows {

		$wsrange = $ws1.UsedRange
		$lastcell = $wsrange.SpecialCells($xlCellTypeLastCell)
		$rowcount = $lastcell.row
		Write-Host "RowCount:" $Rowcount
		[int]$row = $rowcount

	for ($s = 2; $s -le $row; $s++) {
		Start-Sleep 5
	    If ($ws1.Cells.Item($s, 1).value() -eq $null) {
	        $Range = $ws1.Cells.Item($s, 1).EntireRow
	        $Range.Delete()
			$row = $row -= 1
			$row
			Write-Host 'found $null'
			}
	    elseif($ws1.Cells.Item($s, 1).Value() -eq "DELETE") {
	        $Range = $ws1.Cells.Item($s, 1).EntireRow
	        $Range.Delete()
			$row = $row -= 1
			$row
			write-host 'found delete'
			}
		elseif($ws1.Cells.Item($s, 1).Value() -eq "INSERT") {
	        $Range = $ws1.Cells.Item($s, 1).EntireRow
	        $Range.Delete()
			$row = $row -= 1
			$row
			write-host 'found insert'
	        }
		elseif($ws1.Cells.Item($s, 1).Value() -eq "UPDATE") {
	        $Range = $ws1.Cells.Item($s, 1).EntireRow
	        $Range.Delete()
			$row = $row -= 1
			$row
			write-host "found update"
	        }
}}

Function DeleteTop {
	for ($i = 8; $i -ge 1; $i--) {
		$x1.Cells.Item($i, 1).EntireRow.delete()
	}
	$x1.range("C1").EntireColumn.delete()

}

function formula{
		$rowcount
		$formula = '=if(A2="UPDATE","Update",if(A2="INSERT","INSERT",if(A2="DELETE","DELETE","")))'
		$ws1.Range("E1","E$rowcount").formula = $formula
		[Void]$ws1.Range("E1","E$rowcount").copy()
		[void]$ws1.Range("E1").PasteSpecial(-4163)
}

$files = dir('$mydirectory is here')


foreach ($f in $files){

################################################################
#####################Starting Excel#############################
################################################################
		$xlCellTypeLastCell = 11
		$x1 = New-Object -ComObject "Excel.Application"
		$x1.Visible=$true #For troubleshooting purposes only.
		$x1.DisplayAlerts = $false
		$x1.Workbooks.Opentext($f)
		$ws1 = $x1.Worksheets.Item(1)
		$ws1.Activate()

################################################################
##################Working with Workbooks##############################
################################################################

		$wsrange = $ws1.UsedRange
		$lastcell = $wsrange.SpecialCells($xlCellTypeLastCell)
		$rowcount = $lastcell.row
		Write-Host "RowCount:" $Rowcount

		Write-Host "Deleting Top"
		DeleteTop
		Start-Sleep 1

		Formula
		Start-Sleep 1
		$x1.Sheets.Item(1).range("A1").select()

		DeleteRows
		Start-Sleep 1

		$x1.ActiveWorkbook.SaveAs($f, $xlFixedFormat)
}


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>