7

我正在尝试格式化导出的数据并且需要删除几列。我想保留带有某些标题的列。为方便起见,如果我有 15 列并且想要保留具有以下标题的列;

州城市名称客户端产品

我担心我正在导出的数据可能会更改,并且将来可能会添加列。我希望能够过滤掉不需要的列并保留上述列。

编辑:我正在使用 Excel,我以前从未写过一行代码。曾经。

4

3 回答 3

13

试试这个。

以相反的顺序遍历列,检查 Select Case 中的标题,并根据需要删除。

Sub deleteIrrelevantColumns()
    Dim currentColumn As Integer
    Dim columnHeading As String

    ActiveSheet.Columns("L").Delete

    For currentColumn = ActiveSheet.UsedRange.Columns.Count To 1 Step -1

        columnHeading = ActiveSheet.UsedRange.Cells(1, currentColumn).Value

        'CHECK WHETHER TO KEEP THE COLUMN
        Select Case columnHeading
            Case "State", "City", "Name", "Client", "Product"
                'Do nothing
            Case Else
                'Delete if the cell doesn't contain "Homer"
                If Instr(1, _
                   ActiveSheet.UsedRange.Cells(1, currentColumn).Value, _
                   "Homer",vbBinaryCompare) = 0 Then

                    ActiveSheet.Columns(currentColumn).Delete

                End If
        End Select
    Next

End Sub
于 2013-05-17T13:23:31.643 回答
3

我猜我们在这里谈论Excel。如果是这样,使用 while 循环遍历您的列并决定保留或不保留应该可以解决问题。

Sub deleteIrrelevantColumns()
    Dim keepColumn As Boolean
    Dim currentColumn As Integer
    Dim columnHeading As String

    currentColumn = 1
    While currentColumn <= ActiveSheet.UsedRange.Columns.Count
        columnHeading = ActiveSheet.UsedRange.Cells(1, currentColumn).Value

        'CHECK WHETHER TO KEEP THE COLUMN
        keepColumn = False
        If columnHeading = "State" Then keepColumn = True
        If columnHeading = "City" Then keepColumn = True
        If columnHeading = "Name" Then keepColumn = True
        If columnHeading = "Client" Then keepColumn = True
        If columnHeading = "Product" Then keepColumn = True


        If keepColumn Then
        'IF YES THEN SKIP TO THE NEXT COLUMN,
            currentColumn = currentColumn + 1
        Else
        'IF NO DELETE THE COLUMN
            ActiveSheet.Columns(currentColumn).Delete
        End If

        'LASTLY AN ESCAPE IN CASE THE SHEET HAS NO COLUMNS LEFT
        If (ActiveSheet.UsedRange.Address = "$A$1") And (ActiveSheet.Range("$A$1").Text = "") Then Exit Sub
    Wend

End Sub
于 2013-05-16T22:24:48.363 回答
1

我有一个类似的问题,这是对我有用的代码。我认为这要简单得多。

Range("A1").Select

Do Until ActiveCell.Value = ""

    If ActiveCell.Value = "Forecast Status" _
        Or ActiveCell.Value = "Amount " _
        Or ActiveCell.Value = "Service Booking Value " _
        Or ActiveCell.Value = "Transaction Number" _
        Or ActiveCell.Value = "Last Modified by" _
        Or ActiveCell.Value = "Last Modified Date" _
        Or ActiveCell.Value = "T# Comparison" _
        Or ActiveCell.Value = "Amount Comparison" _
        Or ActiveCell.Value = "Forecast Status Comparison" _
        Or ActiveCell.Value = "First Ship Date Comparison" Then

        ActiveCell.Offset(0, 1).Select

    Else

        ActiveCell.EntireColumn.Select
        Selection.Delete Shift:=xlToLeft
        Selection.End(xlUp).Select

    End If
Loop
于 2015-06-09T04:42:41.567 回答