简体   繁体   English

Excel VBA 删除一行中的特定列

[英]Excel VBA delete specific columns in one row

I am searching for a name in one row in excel.我正在 excel 中的一行中搜索一个名称。 I would like to delete all columns before that name till a specific column Eg my Name is in column 10 and I want to delete all from column 2 to column 9 so that column 10 with my name will be then at position of column 2.我想删除该名称之前的所有列,直到特定列例如我的名字在第 10 列中,我想删除从第 2 列到第 9 列的所有列,以便我的名字的第 10 列将位于第 2 列的 position。

Sub delete_cells()
Dim rg As Range
 Set rg = ActiveSheet.Rows(2).Find(What:="Name", LookIn:=xlValues, LookAt:=xlWhole, _
         SearchOrder:=xlByRows, MatchCase:=False)
 If Not rg Is Nothing Then
    If rg.Column > 1 Then ActiveSheet.Columns("4:" & rg.Column - 3).Delete
 Else
     MsgBox "Something
 End If
End Sub

I get an run time error.我收到运行时错误。 Why?为什么?

EDIT explanation:编辑说明:

我有什么和我需要什么的形象

I need to search for "Name" in row 2 and take all values from Column I, J, K etc. to Column E. So I need columns E to H to be deleted and move columns I, J etc. only for the given row.我需要在第 2 行中搜索“名称”并将 I、J、K 等列中的所有值提取到 E 列。所以我需要删除 E 到 H 列并仅针对给定移动列 I、J 等排。 There should no columns of other rows being deleted.不应删除其他行的列。

The parameter of columns can be columns的参数可以是

  • column letter(s), representing a Column: Columns("B")列字母,代表一个列: Columns("B")
  • a number, representing the column number: Columns(2)一个数字,代表列号: Columns(2)
  • two column letter(s), separated with a colon, representing from and to columns Columns("B:AA")两列字母,用冒号分隔,表示从列和到列Columns("B:AA")

However, it is not possible to pass two numbers separated with colon: Columns("2:4") is invalid.但是,不能传递用冒号分隔的两个数字: Columns("2:4")无效。

I think the easiest way for you is to use the resize -method:我认为对您来说最简单的方法是使用resize方法:

Sub delete_cells(findName As String)
    const firstColToDelete = 2    ' (you have 4 in your code, change as you need)

    Dim rg As Range
    Set rg = ActiveSheet.Rows(2).Find(what:=findName, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False)
 
    If Not rg Is Nothing Then
        Dim colsToDelete As Range
        Set colsToDelete = ActiveSheet.Columns(firstColToDelete).Resize(, rg.Column - firstColToDelete)
        colsToDelete.Delete
    Else
         MsgBox "Something"
    End If
End Sub

Update: To just delete the columns from your header row (row 2), change the code to更新:要从 header 行(第 2 行)中删除列,请将代码更改为

    Dim colsToDelete As Range
    Set colsToDelete = ActiveSheet.Cells(2, firstColToDelete).Resize(, rg.Column - firstColToDelete)

    colsToDelete.Delete xlShiftToLeft

Note that this will leave your rightmost data columns without header.请注意,这将使您最右边的数据列没有 header。 Is this what you intent?这是你的意图吗?

Delete Columns Before删除之前的列

  • While developing code for deleting rows or columns, it is a good idea to use Select instead of Delete , and only when the code is finished, switch to Delete .在开发删除行或列的代码时,最好使用Select而不是Delete ,并且只有在代码完成后,才切换到Delete
  • Resize and Offset will accurately define the search range. ResizeOffset将准确定义搜索范围。
  • The LookIn parameter xlFormulas will allow finding an occurrence in a hidden column. LookIn参数xlFormulas将允许在隐藏列中查找事件。
  • The parameter of the After argument, .Cells(.Cells.Count) (defining the last cell) will make the search start from the first cell of the range. After参数的参数.Cells(.Cells.Count) (定义最后一个单元格)将使搜索从范围的第一个单元格开始。

The Code编码

Option Explicit

Sub deleteColumns()
    
    Const cRow As Long = 2
    Const cCol As Long = 2
    Const Criteria As String = "Name"
    
    Dim ws As Worksheet: Set ws = ActiveSheet
    
    Dim fCell As Range
    With ws.Rows(cRow)
        Set fCell = .Resize(, .Columns.Count - cCol).Offset(, cCol) _
            .Find(Criteria, .Cells(.Cells.Count), xlFormulas, xlWhole)
    End With
    If Not fCell Is Nothing Then
        ws.Columns(cCol).Resize(, fCell.Column - cCol).Select ' .Delete
    Else
        MsgBox "Could not find '" & Criteria & "'.", vbExclamation, "Not Found"
    End If

End Sub

EDIT:编辑:

Sub deleteColumns()
    
    Const cRow As Long = 2
    Const cCol As Long = 4
    Const Criteria As String = "Name"
    
    Dim ws As Worksheet: Set ws = ActiveSheet
    
    Dim fCell As Range
    With ws.Rows(cRow)
        Set fCell = .Resize(, .Columns.Count - cCol).Offset(, cCol) _
            .Find(Criteria, .Cells(.Cells.Count), xlFormulas, xlWhole)
        If Not fCell Is Nothing Then
            .Columns(cCol).Resize(, fCell.Column - cCol).Delete xlToLeft
        Else
            MsgBox "Could not find '" & Criteria & "'.", _
            vbExclamation, "Not Found"
        End If
    End With

End Sub

You could put cRow in the arguments section:您可以将 cRow 放入 arguments 部分:

Sub deleteColumns(ByVal cRow As Long)

and delete the line Const cRow As Long = 2 .并删除Const cRow As Long = 2行。
Then you could use deleteColumns(i) in the loop of another procedure.然后你可以在另一个过程的循环中使用deleteColumns(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM