1

假设它在 A2、A4、A5、A6 中找到一些特定文本,如“sa”,我需要分别在 B2、B4、B5、B6 中复制“sohail”,就像这样......

ImageName                   PhotographerName
pirf_20120523_sa0001.jpg    sohail
pirf_20120523_ml0001.jpg    manav
pirf_20120523_sa0001.jpg    sohail
pirf_20120523_sa0001.jpg    sohail
pirf_20120523_sa0001.jpg    sohail
pirf_20120529_sh0005.jpg    shirazi
pirf_20120523_ml0001.jpg    manav 
pirf_20120529_sh0005.jpg    shirazi

我试图检索它找到文本的地址...

Sub FindinTextInEachCell()

Dim rngX As Range


Set rngX = Worksheets("Sheet1").Range("A1:A10").Find("sa", lookat:=xlPart)
If Not rngX Is Nothing Then
    MsgBox "Found at " & rngX.Address
End If

End Sub
4

1 回答 1

0

您没有Find正确使用方法,它只会返回Range找到的第一个,您必须findnext()对后续范围使用方法,但由于后续范围会重复,您必须将其与找到的第一个地址进行比较。

Sub foo()
On Error Resume Next

Dim rng As Range
Dim prng As Range
Dim firstAddress As String

With Worksheets(1).Range("A1:A10")
    Set rng = .Find("sa", LookIn:=xlValues, LookAt:=xlPart)
    If Not rng Is Nothing Then
        firstAddress = rng.Address
        Do
            Set prng = rng
            rng.Offset(0, 1) = "Sohail"
            Set rng = .FindNext(After:=prng)
        Loop While Not rng Is Nothing And rng.Address <> firstAddress
    End If
End With

End Sub
于 2012-07-16T09:40:55.423 回答