删除具有特定单元格值的EntireRow
我试图使用这个VBS代码来删除具有特定单元格值的整行,但是当我尝试查找该值并select该行时,会出现错误:
Set xc = CreateObject("Excel.Application") xc.Visible = false xc.displayalerts=false Set xp = xc.WorkBooks.Open ("C:\Users\Shahim\Desktop\test.xlsx") Set xs = xp.Worksheets(1) Set xc = xs.find(what:="Vtest",lookat:=xlwhole).select Set xr = xc.ActiveCell.EntireRow.Select xr.selection.delete xp.save xp.close()
你有一些问题:
-
函数调用返回一个值时需要括号:
set xp=xc.WorkBooks.Open("C:\Users\Shahim\Desktop\test.xlsx")
-
Find()
在范围内运行,而不是工作表(xs.find(...)
无效)。 -
你不能在VBScript中使用命名参数(
what:="Vtest"
是无效的)。 -
您需要定义您使用的所有Excel常量:
Const xlWhole = 1
考虑到所有这些,请尝试以下操作:
' Define constants... Const xlWhole = 1 Set xc = CreateObject("Excel.Application") xc.Visible = false xc.displayalerts=false ' Use parens when calling functions... set xp = xc.WorkBooks.Open("C:\Users\Shahim\Desktop\test.xlsx") set xs = xp.Worksheets(1) ' Use Find() with a range (column A here, change as needed). ' Specify params by position, not name. set xc = xs.Range("A:A").find("Vtest", , , xlwhole) If Not xc Is Nothing Then xc.EntireRow.Delete End If xp.save xp.close()