lundi 21 décembre 2020

Using Powershell in Excel to check all cell values in column to trigger copy and paste per row

I have a spreadsheet with 2 date columns with only one date value in either of them per row. Having two columns for this is redundant for my purposes so I would like to run a powershell script to check every cell in the Date2 column and if blank, copy the Date1 value on the same row to the Date2 column. That's the part I can't work out; To check all cells in the Date2 column and if blank, copy the adjacent Date1 value over. Once that's done, I'll delete the Date1 column. NOTE: The Date2 column has more dates so using it as the primary column to keep (less copy/pasting). TIA

Example Test.xlsx:
         A            B
1 |    Date1   |    Date2   |
2 | 01/01/1900 |            |
3 | 01/01/1900 |            |
4 |            | 01/01/1900 |
5 |            | 01/01/1900 |
6 | 01/01/1900 |            |
7 |            | 01/01/1900 |
$Excel = New-Object -Com Excel.Application -Property @{Visible = $false} # Start Excel and hide the window
$Excel.DisplayAlerts = $False # Disable comfirmation prompts
$Workbook = $Excel.Workbooks.Open(C:\Temp\Test.xlsx) # Open spreadsheet file

#This is the part I'm stuck on:
#If cell in column Date2 is empty, then copy the adjacent Date1 value to the Date2 cell - This is done until at the end of the columns

[void]$Workbook.Sheets.Item("Test").Cells.Item(1, 1).EntireColumn.Delete() # Deleting "Date1" column
$Workbook.Save() # Save changes
$Workbook.Close($true) # Close workbook
$Excel.Quit() # Quit Excel
[Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) # Release COM

Aucun commentaire:

Enregistrer un commentaire