获取Excel中使用的最后一个单元格的值

时间:2017-11-30 11:19:58

标签: excel-vba powershell vba excel

From

i wanted to read the value 17/11/2017 from the image i.e the last row in column A

我已经使用了3行并得到了答案3这是真的,我想得到第3行中的值,并且当我们继续添加行时,如何追加?

3 个答案:

答案 0 :(得分:1)

如果你想坚持使用powershell。试试这个..

$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $False
$WorkBook = $objExcel.Workbooks.Open($filepath)
$WorkSheet = $objExcel.WorkSheets.item($sheetname)
$WorkSheet.activate()

[int]$lastRowvalue = ($WorkSheet.UsedRange.rows.count + 1) - 1
$lastrow = $WorkSheet.Cells.Item($lastRowvalue, 1).Value2
write-host $lastrow

$objExcel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel) | out-null

我假设您已指定$filepath$sheetname。每次运行后都不要忘记关闭excel。

答案 1 :(得分:0)

这样做,只需更改实际工作表的Sheet1:

LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row 
'This will find the last used row on column A of Sheet1

value = Sheet1.cells(LastRow,1).value 
'Place the value of that cell into a variable

Msgbox value 
'Display the variable to the user in a msgbox

答案 2 :(得分:0)

Shanayl提供的方法确实可以正常工作,但请记住,它使用的count方法不会计算空白。以下一项:

$filepath = "F:\Drives.xlsx"
$xl=New-Object -ComObject "Excel.Application"  
$wb=$xl.Workbooks.open($filepath)
$ws=$wb.ActiveSheet 
$cells=$ws.Cells

$Lastrowno =$Ws.UsedRange.SpecialCells(11).row