删除行和列直到特定范围

时间:2018-08-10 12:00:19

标签: excel vba

我有一本Excel工作簿,其中使用.Find在标题中搜索日期。

例如,如果在RANGE("C3")中找到了它,则应删除上方的行和之前的

请帮助我编写VBA代码以执行此操作。

Dim WSD As Worksheet
Set WSD = Worksheets("report")
Dim MyRange As Range
Set MyRange = WSD.Range("A:AZ")
Set R = MyRange.Find("Date", LookIn:=xlValues)
Debug.Print R.Address

2 个答案:

答案 0 :(得分:1)

尝试

dim MyRange  as range

with Worksheets("report")
    Set MyRange = .Range("A:AZ").Find("Date", LookIn:=xlValues)
    if not MyRange is nothing then
        if MyRange.column > 1 then
            .cells(1, 1).resize(1, MyRange.column -1).entirecolumn.delete
        end if
        if MyRange.row> 1 then
            .cells(1, 1).resize(MyRange.row-1, 1 ).entirerow.delete
        end if
    end if
end with

答案 1 :(得分:1)

另一种方式:

Dim MyRange  As Range
With Worksheets("report")
    Set MyRange = .Range("A:AZ").Find("Date", LookIn:=xlValues)
    If Not MyRange Is Nothing Then MyRange.Resize(.UsedRange.Rows.Count + MyRange.Row, .UsedRange.Columns.Count + MyRange.Column).Cut Destination:=.Range("A1")
End With

Dim MyRange  As Range
With Worksheets("report")
    Set MyRange = .Range("A:AZ").Find("Date", LookIn:=xlValues)
    If Not MyRange Is Nothing Then
        .Rows(1).Insert
        .Columns(1).Insert
        With .Range("A1", MyRange)
            .Resize(, .Columns.Count - 1).EntireColumn.Delete
            .Resize(.Rows.Count - 1).EntireRow.Delete
        End With
    End If
End With