Delete Row with Specific Value (PowerShell)

Benzula

Board Regular
Joined
Feb 28, 2014
Messages
248
So my Code so far is, What I need to be able to do is if Column A has "VIOS" anywhere within that cell, like "VIOS123" or "ABCVIOS" to delete that entire row. I have already created the ComObject, and set my paths. I am using Excel as Visible just so that I can test and follow the changes as I do them and will eventually be copying and pasting using powershell the result of formatting FilePath1.

I have it so that I can delete a specific row, but the location may change of the VIOS row so I need to make it more dynamic.

Code:
## Defines Month Key
= (get-date).Year * 100 + (get-date).Month

## Defines Excel File from Server as "ExcelPath"
$ExcelPath = 'FilePath1.XLS'

## ## Defines Master Excel File from Server as "ExcelPath2"
$ExcelPath2 = 'FilePath2.XLS'
## Creates ComObject to interact with
$Excel = New-Object -ComObject Excel.Application

## Allows Vision of Workbook, Turn to False when not testing
$Excel.Visible = $true

## Uses Defined "Excel Path" (Line 6) and Opens workbook
$ExcelWorkBook = $Excel.Workbooks.Open($ExcelPath)

## Activates the Workbook's First Worksheet
$ExcelWorkSheet = $ExcelWorkBook.Worksheets.Item(1)



##THIS IS WHERE I NEED HELP
$ExcelWorkSheet.Cells.Item(1, 1).EntireRow.Delete() # Delete the first row
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
If it were VBA I would just write it like this but I'm not sure all the Syntax would be for PowerShell.

Code:
Sub Delete_row()


Dim MyRange As Range
Dim MyTotal As Long


Set MyRange = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
MyTotal = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Cells.Count


For MyCount = MyTotal To 1 Step -1


'Then Action of Macro Here (Usually IF than Statement)


If MyRange.Cells(MyCount).Formula = "VIOS" Then
MyRange.Cells(MyCount).EntireRow.Delete




End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,392
Messages
6,119,255
Members
448,879
Latest member
oksanana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top