获取列的名称

时间:2016-04-29 06:33:51

标签: excel vba

我想创建一个列名列表,并将其与表中现有的列匹配。如果列名称与列表列表不匹配,我必须将其删除。到目前为止,我已经研究过了,他们只提供一个列名。我一直坚持这部分。      选项明确

Sub Sample()
Dim strSearch As String
Dim aCell As Range

strSearch = "Salary" 'I want to have a list of column names here

Set aCell = Sheet1.Rows(1).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

'if the column name does not match, then the entire column should be deleted

End Sub

任何帮助?

3 个答案:

答案 0 :(得分:3)

在这里,我假设您的列名在每列的第1行。

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

Sub Sample()
    Dim strSearch As Variant
    Dim aCell As Range
    Dim ColumnName As String
    Dim lastcolumn As Long

    strSearch = Array("Salary", "Column1", "Column2") '--->write column names here

    lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column

    For i = lastcolumn To 1 Step -1
        ColumnName = Cells(1, i).Value
        ' check whther column name exists in your array strSearch
        If Not IsInArray(ColumnName, strSearch) Then
            Columns(i).EntireColumn.Delete
        End If
    Next i
End Sub


编辑:对于worbook中的所有工作表 的 ______________________________________________________________________________

Sub Sample()
    Dim strSearch As Variant
    Dim aCell As Range
    Dim ColumnName As String
    Dim lastcolumn As Long
    Dim wsh As Worksheet

    strSearch = Array("Salary", "Column1", "Column2") '--->write column names here

    For Each wsh In ActiveWorkbook.Worksheets
        lastcolumn = wsh.Cells(1, Columns.Count).End(xlToLeft).Column
        For i = lastcolumn To 1 Step -1
            ColumnName = wsh.Cells(1, i).Value
            If Not IsInArray(ColumnName, strSearch) Then
                wsh.Columns(i).EntireColumn.Delete
            End If
        Next i
    Next
End Sub

函数IsInArray由JimmyPena

提供

答案 1 :(得分:0)

使用它来解析vba中的字符串数组:

Dim strSearch As Variant
strSearch = Array("Cat", "Dog", "Rabbit")

For Each element In strSearch
    'do something with element

Next element

答案 2 :(得分:0)

试试这个。

注意:请记住,从列表中删除时,始终从列表末尾开始并返回。这样,对下一列的引用始终是正确的。

 Sub DeleteUnwantedColumns()

      Dim CurrentWorkSheet As Worksheet
      Set CurrentWorkSheet = ThisWorkbook.Sheets("Sheet1")

      Dim LastColumn As Long
      LastColumn = CurrentWorkSheet.Cells(1, Columns.Count).End(xlToLeft).Column

      Dim KeepColumnArray As Variant
      'Just remember that an Array starts a 0
               'Array Position   0              1
      KeepColumnArray = Array("Salary", "Employee Number")

      Dim KeepColumnString As String
      KeepColumnString = "Salary Employee Number"

      Dim CurrentColumn As Long
      Dim ArrayPosition As Long
      Dim CurrentColumnText As String
      Dim DeleteColumnStatus As Boolean

      For CurrentColumn = LastColumn To 1 Step -1

           CurrentColumnText = CurrentWorkSheet.Cells(1, CurrentColumn).Text

           'Both of the following options work
           'Using InStr is less complicated and is NOT Case Sensitive
           'Using Array is a little complicated and IS Case Sensitive
           'Delete the one you dont want to use

           '=======================================================================================
           'Using an Array
           'Using 'UBound will count the size of the array which will help to prevent you having to
           '  change the loop should the KeepColumnArray change
           For ArrayPosition = 0 To UBound(KeepColumnArray, 1)

                'Setting a "Delete Status" while running through the KeepColumnArray
                If CurrentColumnText = KeepColumnArray(ArrayPosition) Then
                     DeleteColumnStatus = False
                     Exit For
                Else
                     DeleteColumnStatus = True
                End If

           Next ArrayPosition

           If DeleteColumnStatus = True Then CurrentWorkSheet.Columns(CurrentColumn).EntireColumn.Delete
           '=======================================================================================

           '=======================================================================================
           'Using a String
           If InStr(1, KeepColumnString, CurrentColumnText) = 0 Then
                CurrentWorkSheet.Columns(CurrentColumn).EntireColumn.Delete
           End If

      Next CurrentColumn

 End Sub