【问题标题】:VbScript search for a specifc header and select the whole columnVbScript 搜索特定标题并选择整个列
【发布时间】:2015-09-02 19:26:56
【问题描述】:

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

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)。有什么方法可以自动执行 (A1:X1) - 其中 X 是 excel 表中的最后一列。

第二,我怎样才能删除 FoundCell 的整个列?

【问题讨论】:

    标签: excel vbscript


    【解决方案1】:

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

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

    我最后添加的只是为了正确关闭 Excel。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      相关资源
      最近更新 更多