2016-04-29 15 views
0

列名を検索していますが、コードが機能しません。これは私が試したことです:列名を一致させることができません

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

    lastRow = Cells(1, Columns.count).End(xlToLeft).Column 

    For k = 0 To lastRow 
     If aCell Is Nothing Then 
      aCell.Offset(0, 1).EntireColumn.Delete 
     End If 
    Next k 

私がしたいのは、列全体を削除することです。どんな助け?あなたはそれをこのように行うことができます

+0

aCell.EntireColumn.delete aCellに範囲が見つかった場合は、それが存在します。何も返さなくなるまでfindをループする必要があります。最終的には、lastColumnでなければなりません:) –

答えて

0

:あなたは、「サンプル」を列見出しと列全体を削除したい場合は

Sub deleteColumn() 

Dim headerColumnToDelete As String 
Dim endRow As Integer 
Dim counter As Integer 

endRow = Cells(1, Columns.Count).End(xlToLeft).Column 
headerColumnToDelete = "sample" 

For counter = endRow To 1 Step -1 

    If Cells(1, counter) = headerColumnToDelete Then 
     Cells(1, counter).EntireColumn.Delete 
    End If 

Next counter 

End Sub 
0

を、モジュールでこれを試してみてください。

Public Sub DeleteSample() 

    DeleteColumn ("sample") 

End Sub 

Public Sub DeleteColumn(Name As String) 

    'Get the header row 
    Dim row As Range 
    Set row = Rows(1) 

    'Find the cell containing Name in that row 
    Dim result As Range 
    Set result = row.Find(Name) 

    Dim wholeColumn As Range 

    'Select the whole column (or quit if it's not found) 

    On Error GoTo Catch 
    Set wholeColumn = result.EntireColumn() 
    On Error GoTo 0 

    'Delete the whole column and shift cells left 
    wholeColumn.Delete xlShiftToLeft 

Catch: 
    Exit Sub 

End Sub 

あなたドン」 tはforループでは、すべての必要がある、あなただけのセルを見つけるために、エクセルAPIを使用することができます:)

0
Sub columndelete() 
    Dim lastcolumn As Long 
    word = "sample" 
    lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column 
    For i = 1 To lastcolumn 
     If Cells(1, i).Value = word Then 
      Cells(1, i).EntireColumn.Delete 
     End If 
    Next i 
End Sub 
0

はちょうどあなたのコード内のいくつかの変更を加えました:

Sub test() 
    word = "sample" 
    Set acell = ActiveSheet.Rows(1).Find(What:=word, LookIn:=xlValues, _ 
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False) 

    If Not (acell Is Nothing) Then 
     acell.EntireColumn.Delete 
    End If 
End Sub 
関連する問題