【问题标题】:Deleting columns in excel using visual basic?使用visual basic删除excel中的列?
【发布时间】:2015-10-03 01:46:23
【问题描述】:

我有大约 750 个 excel 文件。我需要清理它们,使它们都包含相同的格式 - 即它们都包含相同数量的列。

某些文件 (80%) 包含额外的列,其中包含带有星号的标签,例如“*1 个主题”。

有没有办法使用 Visual Basic 浏览我文件夹中的所有文件以删除所有包含星号的列,以便所有文件都没有任何此类列?星号是计算机语言中的通配符这一事实会产生影响吗?

【问题讨论】:

  • 正如我在上一个问题中提到的......最好的方法是录制一个宏,在单个文件上执行您想要的操作,然后对其进行修改以处理多个文件。这是最好的学习 vba 的方法。我之前发布的链接也适用于这个问题...stackoverflow.com/a/31403366/5040941.
  • 您好,谢谢,但带有星号的列的位置会更改每个文件(如果它们甚至存在的话),因此可能对单个文件有效的内容不一定适用于另一个文件....感谢链接和您的不过之前的帖子,我已经看过了...

标签: vba excel


【解决方案1】:

编写一个使用文件系统对象循环遍历电子表格所在目录的宏。循环遍历每个工作表并分析列名。

以下是您将如何循环浏览每张纸。

Private Sub CommandButton7_Click()

Dim ws      As Excel.Worksheet
Dim iCol    As Integer
Dim strName As String
Dim iIndex  As Integer

    'Loop through the sheets.
    For iIndex = 1 To Application.Worksheets.Count
        Set ws = Application.Worksheets(iIndex)

        'Loop through the columns.
        For iCol = 1 To ws.UsedRange.Columns.Count
            'Check row 1 of this column for first char of *
            If Left(ws.Cells(1, iCol).Value, 1) = "*" Then
                'We have found a column with the first char of *
                ws.Columns(iCol).EntireColumn.Delete
            End If
        Next iCol

    Next iIndex
    ActiveWorkbook.SaveAs Filename:="C:\temp\newfiles\" & ActiveWorkbook.Name, FileFormat:=xlWorkbookNormal
End Sub

如果您想在单元格中的任何位置查找 *,您可以使用 instr()

Private Sub CommandButton7_Click()

Dim ws      As Excel.Worksheet
Dim iCol    As Integer
Dim strName As String
Dim iIndex  As Integer

    'Loop through the sheets.
    For iIndex = 1 To Application.Worksheets.Count
        Set ws = Application.Worksheets(iIndex)

        'Loop through the columns.
        For iCol = 1 To ws.UsedRange.Columns.Count
            'Check row 1 of this column for the char of *
            If instr(ws.Cells(1, iCol).Value, "*") > 0 Then
                'We have found a column with the char of *
                ws.Columns(iCol).EntireColumn.Delete
            End If
        Next iCol

    Next iIndex
    ActiveWorkbook.SaveAs Filename:="C:\temp\newfiles\" & ActiveWorkbook.Name, FileFormat:=xlWorkbookNormal
End Sub

这是给定目录中的基本循环文件。希望这能让你到达那里。

Private Sub CommandButton7_Click()
    Dim wb      As Workbook
    Dim ws      As Excel.Worksheet
    Dim iCol    As Integer
    Dim strName As String
    Dim iIndex  As Integer
    Dim strPath As String
    Dim strFile As String

    strPath = "c:\temp\oldfiles\"
    strFile = Dir(strPath & "*.xlsx")

    Do While strFile <> ""

        Set wb = Workbooks.Open(Filename:=strPath & strFile)

        'Loop through the sheets.
        For iIndex = 1 To Application.Worksheets.Count
            Set ws = Application.Worksheets(iIndex)

            'Loop through the columns.
            For iCol = 1 To ws.UsedRange.Columns.Count
                'Check row 1 of this column for the char of *
                If InStr(ws.Cells(1, iCol).Value, "*") > 0 Then
                    'We have found a column with the char of *
                    ws.Columns(iCol).EntireColumn.Delete
                End If
            Next iCol

        Next iIndex
        wb.SaveAs Filename:="C:\temp\newfiles\" & wb.Name, FileFormat:=xlOpenXMLWorkbook
        wb.Close SaveChanges:=False
        strFile = Dir
    Loop

End Sub

【讨论】:

  • 那太好了 - 谢谢。有没有办法让它适应子文件夹?这是否会再次保存文件并在列格式化后关闭?
  • 添加了保存文件以保留原件并在已删除列的已定义位置创建新文件。子文件夹是可能的,但有点工作。如果合理,一次做一个文件夹或将它们移动到一个文件夹中。
  • 这是一个循环遍历文件夹中文件的好例子。如果您需要帮助以适应上述代码,请告诉我。 stackoverflow.com/questions/5851531/…
  • 嗨 MatthewD,我尝试运行上面的代码,在删除 Set 语句并在 SaveAs 和 Close 之后添加括号后,我停止在脚本上收到错误,而是简单地得到一个输出消息:值不能空值。参数名称:solutionDirectory
  • 你在哪一行得到 Value cannot be null 错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多