简体   繁体   English

删除行和列直到特定范围

[英]Delete Rows and Columns until specific range

I have an Excel workbook in which I search for a date in the heading using .Find . 我有一个Excel工作簿,其中使用.Find在标题中搜索日期。

For example if it is found in RANGE("C3") then it should remove rows above and columns before it. 例如,如果在RANGE("C3")找到它,则应删除上面的行前面的列

Please help me write the VBA code to perform this operation. 请帮助我编写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

Try, 尝试,

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

another way: 其他方式:

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

Or 要么

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

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

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