gpt4 book ai didi

excel - 无法获取范围类的 FindNext 属性

转载 作者:行者123 更新时间:2023-12-04 20:51:14 26 4
gpt4 key购买 nike

我有这个代码,它只需要用户的 3 个单词,在 b 中查找单词, cd并将值更改为 XXXXXXXXXXXXX .

问题是我得到

unable to get findnext property of range class



我需要这段代码用不同的词进行多次搜索并替换值而不关闭/重置excel。
如果可能,当它在 b 中找不到值时,应该去执行下一段代码。

该代码在段落中查找单词。

有人可以指出我正确的方向吗?
Private Sub CommandButton1_Click()

Dim x As String
Dim y As String
Dim z As String

x = InputBox("enter word")
y = InputBox("enter word")
z = InputBox("enter word")





With Worksheets(1).Range("b2:b1000")
Set b = Cells.Find(x)
If Not b Is Nothing Then
firstAddress = b.Address
Do

b.Value = "XXXXXXXXXXXXX"
Set b = .FindNext(b)
Loop While Not b Is Nothing
End If
End With


With Worksheets(1).Range("c2:c1000")
Set c = Cells.Find(y)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = "XXXXXXXXXXXXX"
Set c = .FindNext(c)
Loop While Not c Is Nothing
End If
End With


With Worksheets(1).Range("d2:d1000")
Set d = Cells.Find(z)
If Not d Is Nothing Then
firstAddress = d.Address
Do
d.Value = "XXXXXXXXXXXXX"
Set d = .FindNext(d)
Loop While Not d Is Nothing
End If
End With

End Sub

最佳答案

Tested this code and if I have a cell with : "Nombre del producto: wok antiadherente verde // Material: Metal // tamaño: vista general del dibujo // Capacidad: 2500 / 4200ml " and I am looking for the word "verde", it only replaces the word and the rest of the text is still there. – Andrew Cm 9 mins ago


Option Explicit

Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim firstWord As String
Dim secondWord As String
Dim thirdWord As String

On Error GoTo Whoa

'~~> Change this to the relevant worksheet
Set ws = Sheet1

firstWord = InputBox("enter word")
secondWord = InputBox("enter word")
thirdWord = InputBox("enter word")

With ws
If firstWord <> "" Then ReplaceText ws.Range("B2:B1000"), firstWord
If secondWord <> "" Then ReplaceText ws.Range("C2:C1000"), secondWord
If thirdWord <> "" Then ReplaceText ws.Range("D2:D1000"), thirdWord
End With

Exit Sub
Whoa:
MsgBox Err.Description
End Sub

'~~> Identify the cell where the value is found and
'~~> Store it in a range. at the end, directly
'~~> replace all values
Private Sub ReplaceText(rng As Range, txt As String)
Dim aCell As Range, bCell As Range
Dim rngFound As Range

Set aCell = rng.Find(What:=txt, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not aCell Is Nothing Then
Set bCell = aCell
Set rngFound = aCell

Do
Set aCell = rng.FindNext(After:=aCell)

If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
Set rngFound = Union(rngFound, aCell)
Else
Exit Do
End If
Loop
End If

If Not rngFound Is Nothing Then
rngFound.Value = "XXXXXXXXXXXXX"
End If
End Sub

关于excel - 无法获取范围类的 FindNext 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60484241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com