查找并添加一个值 [英] VBA.Find and adding a value

查看:90
本文介绍了查找并添加一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Excel表格中的列中找到一个特定的值,然后在右边添加一个值(= 1)三列。

I am trying to find a specific value in a column in an Excel sheet, and then add a value (=1) three columns to the right.

我的代码:

Sub AddNote()
    ActiveSheet.Range("F:F").Find(What:="Cat").Select
    Selection.Offset(0, 4).Value = 1
End Sub

只有我得到:


错误消息91:`对象变量或未设置块变量

Error message 91: `Object variable or With block variable not set.

我的命令有什么问题?

推荐答案

p>看看这个:

Sub test_IngaB()
Dim FirstAddress As String, cF As Range, LookForString As String
LookForString = "Cat"

With ThisWorkBook.Sheets("Sheet's name").Range("F:F")
    .Cells(1,1).Activate
    'First, define properly the Find method
    Set cF = .Find(What:=LookForString, _
                After:=ActiveCell, _
                LookIn:=xlFormulas, _
                LookAt:=xlPart, _
                SearchOrder:=xlByColumns, _
                SearchDirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False)

    'If there is a result, keep looking with FindNext method
    If Not cF Is Nothing Then
        FirstAddress = cF.Address
        Do
            cF.Offset(0, 3).Value = 1
            Set cF = .FindNext(cF)
        'Look until you find again the first result
        Loop While Not cF Is Nothing And cF.Address <> FirstAddress
    End If
End With

End Sub

这篇关于查找并添加一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆