【问题标题】:'Range' of object'_Global' fail in Do Until对象 _Global' 的“范围”在“直到”中失败
【发布时间】:2021-11-14 22:23:42
【问题描述】:

基本上我只是在网上找到了这个小代码,我认为它可能对我有帮助,因为我想改进它。但是在

Do Until Range("A" & amp, R) = ""

我在标题中得到了提到的错误。

代码如下:

Sub Use_Instr()
    R = 1
    'loop to the last row
    Do Until Range("A" & amp, R) = ""
        'check each cell if contains 'apple' then..
        '..place 'Contains Apple' on column B
        If Range("A" & amp, R) Like "*apple*" Then
            Range("B" & amp, R) = "Contains Apple"
        End If
        R = R + 1
    Loop
End Sub

它确实在 A 列的句子中搜索“apple”一词,并在 B 列写“contains apple” 列是否包含“苹果”

【问题讨论】:

  • Range("A" & amp, R) 不是解决范围的正确方法。我想我知道发生了什么.. 当您从网站复制时,它也复制了 html 代码。将& amp, 替换为&
  • 在Html中&被编码为&
  • 是的,看来谢谢你们,正如您所见,我不是 vba 专家...
  • 没有探测。顺便说一句,你可以在不使用循环的情况下实现你想要的;)

标签: excel vba range


【解决方案1】:

试试这个:

R = 1
'loop to the last row
Do Until Range("A" & R).Value = ""
'check each cell if contains 'apple' then..
'..place 'Contains Apple' on column B
If Range("A" & R).Value Like "*apple*" Then
    Range("B" & R).Value = "Contains Apple"
End If
R = R + 1
Loop

【讨论】:

    【解决方案2】:

    Range("A" & amp, R) 不是寻址范围的正确方法。当您从网站复制时,它也复制了 html 编码。在 Html 中,& 被编码为&。只需在您的代码中将& amp, 替换为&。所以你的代码变成了

    R = 1
    'loop to the last row
    Do Until Range("A" & R) = ""
        'check each cell if contains 'apple' then..
        '..place 'Contains Apple' on column B
        If Range("A" & R) Like "*apple*" Then
            Range("B" & R) = "Contains Apple"
        End If
        R = R + 1
    Loop
    

    为了使代码不区分大小写,正如下面 cmets 中 @VBasic2008 所建议的那样,您可能需要将 Range("A" & R) Like "*apple*" 更改为 If LCase(Range("A" & R).Value2) Like "*apple*" Then

    话虽如此,我会使用一种不同的方法,而不是使用循环稍慢的方法。

    它确实在A列的句子中搜索“apple”这个词,如果它包含“apple”,则在B列写“contains apple”

    如果您要在 Excel 中执行此操作,那么您将使用公式 =IF(ISNUMBER(SEARCH("apple",A1)),"Contains Apple","")

    所以我们要做的是在B 列中找到最后一行,然后一次性将这个公式添加到整个范围内!最后,我们将公式转换为值。

    Option Explicit
    
    Sub Sample()
        Dim ws As Worksheet
        Dim lRow As Long
        Dim sFormula As String
        
        '~~> Change this to the relevant sheet
        Set ws = Sheet1
        
        '~~> This is your formula
        '   =IF(ISNUMBER(SEARCH("apple",A1)),"Contains Apple","")
        sFormula = "=IF(ISNUMBER(SEARCH(""apple"",A1)),""Contains Apple"","""")"
        
        With ws
            '~~> Find the last row in column B
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
            
            '~~> Insert the formula in the entire range in 1 go
            With .Range("B1:B" & lRow)
                .Formula = sFormula
                '~~> Convert formula to value
                .Value = .Value
            End With
        End With
    End Sub
    

    【讨论】:

    • 为了使答案保持一致,并简要说明,您应该在 OP 修改后的代码中使用If LCase(Range("A" & R)) Like "*apple*" Then(因为您在改进的解决方案中使用SEARCH(而不是FIND)) .
    • 1. 我没有使用循环 2. 搜索不区分大小写,即不区分大小写:) @VBasic2008
    • 1. 您在 OP 的“已修复”代码 sn-p 中使用了一个循环,就在 ... So your code becomes... 之后和 Having said that,... 之前。 2.“已修复”代码 sn-p 区分大小写。
    • @VBasic2008:啊,你的意思是原始代码:D。我只建议将& amp 更改为&。没有尝试改进它。好建议。帖子更新了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多