简体   繁体   English

检查vba中是否有空Range

[英]Check if there is empty Range in vba

I am trying to filter by a list of Condition from the Condition wb to use for the Order wb. 我试图按条件wb中的条件列表进行筛选,以用于订单wb。 I use a checkEmpty range in order to check if there are no matching value then I will clear the filter and start with the next condition. 我使用checkEmpty范围来检查是否没有匹配的值,然后清除过滤器并从下一个条件开始。 But my code doesn't work and the error is "Range of object_worksheet" failed. 但是我的代码不起作用,并且错误是“ Range of object_worksheet”失败。 I get the error because even there is no matching value (empty range), the code still jump to Else condition. 我收到错误消息是因为即使没有匹配值(空范围),代码仍然跳到其他条件。 Here is my code: 这是我的代码:

Sub Order()

Dim start As Double
Dim strKeyWord As String
Dim myCount As Integer
Dim checkEmpty As Range
Dim lRow1 As Long

Dim wsOrder As Worksheet
Dim wsCondition As Worksheet
Dim wbOrder As Workbook
Dim wbCondition As Workbook

Dim OrderFile As String
Dim ConditionFile As String

'Open Order wb
OrderFile = Application.GetOpenFilename()
Set wbOrder = Workbooks.Open(OrderFile)
Set wsOrder = wbOrder.Worksheets(1)

'Open Condition wb
ConditionFile = Application.GetOpenFilename()
Set wbCondition = Workbooks.Open(ConditionFile)
Set wsCondition = wbCondition.Worksheets(1)

'using the CountA ws function (all non-blanks)
myCount = Application.CountA(wsCondition.Range("A:A")) - 1

start = 2

For I = 1 To myCount Step 1

    strKeyWord = wsCondition.Range("A" & start)
    wsOrder.Range("R:R").AutoFilter Field:=1, Criteria1:="=*" & strKeyWord & "*"

    'lRow1 = WorksheetFunction.Max(wsOrder.Range("I65536").End(xlUp).Row)
    Set checkEmpty = wsOrder.Range("I2:I100").SpecialCells(xlCellTypeVisible)

    If checkEmpty Is Nothing Then
        On Error Resume Next
        wsOrder.ShowAllData
        On Error GoTo 0
    Else
        wsOrder.Range("I2", Range("I" & Rows.Count).End(xlUp)).Copy
        With wsCondition
            .Cells(.Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial
        End With
    End If
    start = start + 1

Next I
End Sub

Thank you very much! 非常感谢你!

So the main issue is that you didn't specify a worksheet for Range("I" & Rows.Count).End(xlUp) . 因此,主要问题是您没有为Range("I" & Rows.Count).End(xlUp)指定工作表。

Using 运用

wsOrder.Range("I2", Range("I" & wsOrder.Rows.Count).End(xlUp)).Copy

should fix that. 应该解决这个问题。

But also I would correct the For I loop because you never use I . 但是我也将纠正For I循环,因为您从不使用I But you don't need the start variable and can use I instead which is also auto incremented. 但是您不需要start变量,而是可以使用I ,它也会自动递增。

'using the CountA ws function (all non-blanks)
myCount = Application.CountA(wsCondition.Range("A:A")) 'removed the -1

'remove start=2 and replace start with I

For I = 2 To myCount Step 1
    strKeyWord = wsCondition.Range("A" & I)
    wsOrder.Range("R:R").AutoFilter Field:=1, Criteria1:="=*" & strKeyWord & "*"

    'lRow1 = WorksheetFunction.Max(wsOrder.Range("I65536").End(xlUp).Row)
    Set checkEmpty = wsOrder.Range("I2:I100").SpecialCells(xlCellTypeVisible)

    If checkEmpty Is Nothing Then
        On Error Resume Next
        wsOrder.ShowAllData
        On Error GoTo 0
    Else
        wsOrder.Range("I2", Range("I" & Rows.Count).End(xlUp)).Copy
        With wsCondition
            .Cells(.Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial
        End With
    End If   
Next I

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

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