Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用visual basic删除excel中的列?_Vba_Excel - Fatal编程技术网

Vba 使用visual basic删除excel中的列?

Vba 使用visual basic删除excel中的列?,vba,excel,Vba,Excel,我有大约750个excel文件。我需要清理它们,以便它们都包含相同的格式,也就是说,它们都包含相同数量的列 有些文件(80%)包含额外的列,其中包含带有星号的标签,例如“*1主题” 是否有一种方法可以使用visual basic遍历“我的文件夹”中的所有文件来删除包含星号的所有列,从而使所有文件都没有任何此类列?星号是计算机语言中的通配符这一事实会有所不同吗?编写一个宏,使用filesystemobjects在电子表格所在的目录中循环。循环浏览每张表并分析列名 下面是如何循环浏览每张图纸 Pri

我有大约750个excel文件。我需要清理它们,以便它们都包含相同的格式,也就是说,它们都包含相同数量的列

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


是否有一种方法可以使用visual basic遍历“我的文件夹”中的所有文件来删除包含星号的所有列,从而使所有文件都没有任何此类列?星号是计算机语言中的通配符这一事实会有所不同吗?

编写一个宏,使用filesystemobjects在电子表格所在的目录中循环。循环浏览每张表并分析列名

下面是如何循环浏览每张图纸

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 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
Private子命令按钮7_单击()
将wb设置为工作簿
将ws设置为Excel.Worksheet
作为整数的Dim-iCol
将strName设置为字符串
作为整数的Dim iIndex
将strPath设置为字符串
作为字符串的Dim strFile
strPath=“c:\temp\oldfiles\”
strFile=Dir(strPath&“*.xlsx”)
当strFile“”时执行
设置wb=Workbooks.Open(文件名:=strPath&strFile)
“在床单上打圈。
对于iIndex=1的Application.Worksheets.Count
设置ws=应用程序工作表(iIndex)
'在列中循环。
对于iCol=1到ws.UsedRange.Columns.Count
'检查此列第1行中的字符*
如果InStr(ws.Cells(1,iCol).Value,“*”)大于0,则
'我们找到了一个字符为的列*
ws.Columns(iCol).entireclumn.Delete
如果结束
下一个iCol
下一个指数
wb.SaveAs文件名:=“C:\temp\newfiles\”&wb.Name,文件格式:=xlOpenXMLWorkbook
wb.Close SaveChanges:=False
strFile=Dir
环
端接头

正如我在上一个问题中提到的。。。最好的方法是在一个文件上录制一个宏,该宏可以执行您想要的操作,然后对其进行修改以处理多个文件。这是学习vba的最好方法。我之前发布的链接对这个问题也是有效的。您好,谢谢,但是每个文件中带星号的列的位置都会发生变化(如果它们存在的话),因此在一个文件上有效的内容不一定在另一个文件上有效。。。。谢谢你的链接和你以前的帖子,我已经看过了…太棒了-谢谢。是否有一种方法也可以调整它以查看子文件夹?这会再次保存文件并在列格式化后关闭吗?添加了saveas以保留原始文件并在定义的位置创建新文件,同时删除列。子文件夹是可能的,但需要做一点工作。如果合理,一次只处理一个文件夹或将它们移动到一个文件夹中。下面是一个循环处理文件夹中文件的好例子。让我知道,如果你需要帮助适应上述代码。嗨,MatthewD,我试着运行上面的代码,在SaveAs和Close之后删除Set语句和addings之后,我不再在脚本上获取错误,而是简单地得到一个输出msg:Value不能为null。参数名称:SolutionDirectory在哪一行获取的值不能为null错误?
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