VBA-发现(动态)范围的透明单元直到最后一行

时间:2019-02-18 10:42:32

标签: excel vba

这与此处发布的问题几乎相同:

Excel VBA - How to clear a dynamic range (from below header, to last row)

但是,鉴于这是旧线程并且提供的解决方案不起作用,我以尝试解决问题的方式发布问题。

在此处找到两个用于找到正确列的解决方案:

VBA - Find a column with a specific header and find sum of all the rows in that column

https://www.extendoffice.com/documents/excel/4879-excel-select-column-by-header-name.html

此处清除目录的解决方案:

Excel VBA - How to clear a dynamic range (from below header, to last row)

这是我试图做的事情:

With ActiveSheet

    xStr = "Product Folder"

    Set aCell = .Range("B2:J2").Find(What:=xStr, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)

    'If Found
    If Not aCell Is Nothing Then

        col = aCell.Column
        colName = Split(.Cells(, col).Address, "$")(1)

        lRow = .Range(colName & .Rows.count).End(xlUp).Row

        'This is your range
        Set Rng = .Range(colName & "2" & lRow)

    'If not found
    Else

        MsgBox "Column Not Found"

    End If

    Range(Rng).Resize(Rows.count - 1, Columns.count).Offset(1, 0).ClearContents

End With

我可以获得所需的所有信息(列,标题,范围等),但似乎找不到清除内容的方法。

使用上面的代码,我得到了错误:

  

对象'_Global'的方法'Range'失败

我尝试更改

    lRow = .Range(colName & .Rows.count).End(xlUp).Row

    'This is your range
    Set Rng = .Range(colName & "2" & lRow)

对此:

    lRow = Range(colName & .Rows.count).End(xlUp).Row

    'This is your range
    Set Rng = Range(colName & "2" & lRow)

这行:

Range(Rng).Resize(Rows.count - 1, Columns.count).Offset(1, 0).ClearContents

对此:

Rng.Resize(Rows.count - 1, Columns.count).Offset(1, 0).ClearContents

但随后出现此错误:

  

应用程序定义或对象定义的错误

试图对此进行更改:

    'If Found
If Not aCell Is Nothing Then

    col = aCell.Column
    colName = Split(.Cells(, col).Address, "$")(1)

    lRow = .Range(colName & .Rows.count).End(xlUp).Row

    'This is your range
    Set Rng = .Range(colName & "2" & lRow)

'If not found
Else

    MsgBox "Column Not Found"

End If

Range(Rng).Resize(Rows.count - 1, Columns.count).Offset(1, 0).ClearContents

对此:

'If Found
    If Not aCell Is Nothing Then

        col = aCell.Column
        colName = Split(.Cells(, col).Address, "$")(1)

        lRow = Range(colName & .Rows.count).End(xlUp).Row + 1

        Set myCol = Range(colName & "2")

        'This is your range
        Set Rng = Range(myCol & ":" & colName & lRow)

    'If not found
    Else

        MsgBox "Column Not Found"

    End If

    Rng.Clear

然后在Set Rng行出现错误:

  

对象'_Global'的方法'Range'失败

我知道这行本身不是很正确(很明显),但是我只是不知道该如何获取代码以将其读为Range("A2:A" & Lastrow).Clear

1 个答案:

答案 0 :(得分:1)

这确实是导致错误的原因:

from pyspark.sql.functions as F from pyspark.sql.types as t # Custom udf decorator which accept return type def udf_typed(returntype=t.StringType()): def _typed_udf_wrapper(func): return F.udf(func, returntype) return _typed_udf_wrapper @udf_typed(t.IntegerType()) def my_udf(x) return int(x)

Set Rng = Range(myCol & ":" & colName & lRow)要求使用范围或字符串参数。

不是两者。尝试这个: Range()

Set Rng = Range(myCol.Address & ":" & colName & lRow)

或者通过传递2个范围对象来尝试这一点:

Sub TestMe()

    Dim rng As Range
    Dim myCol As Range
    Dim colName As String
    Dim lRow As Long

    Set myCol = Range("A5")
    colName = "A"
    lRow = 15
    Set rng = Range(myCol.Address & ":" & colName & lRow)    
End Sub
相关问题