VbScriptsearch一个特定的标题并select整个列

我正在写一个VbScript来删除通过参数放入的Excel文件的特定列。 excel文件的第1行是列的标题。 这是我当前的代码来查找标题中的特定string。

Set objExcel = CreateObject("Excel.Application") If WScript.Arguments.Count = 0 Then WScript.Echo "Drop excel files on this script" Else objExcel.Visible = True objExcel.Workbooks.Add(WScript.Arguments.Item(0)) Set FoundCell = objExcel.Range("A1:C1").Find("Sales Figures") '* Select the entire column of FoundCell and remove it End If 

我的第一个问题是,我必须手动指定范围标题(A1:C1)来search。 有没有办法自动执行(A1:X1) – 其中X是Excel表中的最后一列。

第二,我如何删除整个列的FoundCell?

我认为这可能有帮助。 您可以使用Sheet对象的UsedRange属性来仅select具有数据的单元格。 这意味着你不需要明确列引用。

Cells(1)Find() Cells(1)限制在UsedRange的第一行。

我在最后添加的位只是为了正确closuresExcel。

 Option Explicit dim objExcel, book, FoundCell Set objExcel = CreateObject("Excel.Application") If WScript.Arguments.Count = 0 Then WScript.Echo "Drop excel files on this script" Else objExcel.Visible = True set book = objExcel.Workbooks.Add(WScript.Arguments.Item(0)) ' assume we're looking in the activesheet Set FoundCell = book.ActiveSheet.UsedRange.Cells(1).Find("Sales Figures") if not IsNull(FoundCell) then '* Select the entire column of FoundCell and remove it book.ActiveSheet.UsedRange.Columns(FoundCell.Column).Delete ' book.SaveAs "removed.xlsx" end if End If ' close up avoiding any "save changes?" dialog for each book in objExcel.Workbooks book.saved = true next objExcel.quit