我尝试使用此VBS代码删除具有特定单元格值的整行,但在尝试查找该值并选择该行时出现错误:
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()
答案 0 :(得分:1)
您有很多问题:
你需要围绕函数调用的括号返回一个值:
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()