VBA在下一张纸上寻找价值

时间:2018-07-17 12:37:28

标签: excel vba excel-vba

我可以在不跳到下一页的情况下(ActiveSheet.Next.Activate)搜索值吗?

以下是整个代码: 问题是,即使没有搜索值,它也会跳到下一张纸。

    Dim ws As Worksheet
    Dim Loc As Range
    Dim StrVal As String
    Dim StrRep As String
    Dim i As Integer

    Private Sub CommandButton1_Click()
    i = 1
    Call Replacing
    End Sub

    Private Sub CommandButton2_Click()
    i = 2
    Call Replacing
    End Sub

    Public Sub Replacing()
        StrVal = Userform1.Textbox1.Text
        StrRep = Me.Textbox1.Text
        if Trim(StrVal) = "" Then Exit Sub
        Dim fstAddress As String
        Dim nxtAddress As String

        For Each ws In ThisWorkbook.Worksheets
            With ws
                Set Loc = .Cells.Find(what:=StrVal)
                fstAddress = Loc.Address
                If Not Loc Is Nothing Then
                    If Not StrRep = "" And i = 1 Then
                        Loc.Value = StrRep
                        Set Loc = .Cells.FindNext(Loc)
                    ElseIf i = 2 Then Set Loc = Range(ActiveCell.Address) 
                        Set Loc = .Cells.FindNext(Loc)
                        nxtAddress = Loc.Address
                        If Loc.Address = fstAddress Then
                            ActiveSheet.Next.Activate '****Here it should jump only if found something on the next sheet****
                            GoTo repeat
                            nxtAddress = Loc.Address
                        End If
                        If Not Loc Is Nothing Then Application.Goto ws.Range(nxtAddress), False
                    End If
                    i = 0
                End If
            End With
            Set Loc = Nothing
    repeat:
        Next ws
    End Sub

在值0、1和2之间切换的变量“ i”绑定到两个按钮。这些按钮是“替换”和“跳过(跳至下一个找到的值)”。

1 个答案:

答案 0 :(得分:0)

此代码在每次出现StrVal时询问您是否要替换该值或跳过它。

我在检查Found_Address = First_Found_Address是否存在问题:

  • 如果您替换了First_Found_Address中的值,它将不会再次找到该地址,并且会错过循环的起点。

代码的原始源也使用Loop While Not c Is Nothing And c.Address <> firstAddress停在最后一个值。这里的问题是,如果c中的值最终被更改,c将是Nothing,但仍会尝试检查c的地址-导致错误( Range Find Method)。

对此,我的解决方案是在工作表上建立一个已访问地址的字符串,并使用INSTR检查当前地址是否已被访问。

我已经包含了通过单击按钮或从另一个过程进行调用的代码。

Private Sub CommandButton1_Click()

    FindReplace Userform1.Textbox1.Text, 1

End Sub

Private Sub CommandButton2_Click()

    FindReplace Userform1.Textbox1.Text, 1, Me.Textbox1.Text

End Sub

Sub Test()

    FindReplace "cd", 1, "ab"

End Sub

Sub FindReplace(StrVal As String, i As Long, Optional StrRep As String = "")

    Dim ws As Worksheet
    Dim Loc As Range
    Dim fstAddress As String

    Dim bDecision As Variant

    For Each ws In ThisWorkbook.Worksheets
        'Reset the visited address list on each sheet.
        fstAddress = ""
        With ws
            Set Loc = .Cells.Find(what:=StrVal, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext)
            If Not Loc Is Nothing Then
                Do
                    fstAddress = fstAddress & "|" & Loc.Address

                    Loc.Parent.Activate 'Activate the correct sheet.
                    Loc.Activate        'and then the cell on the sheet.

                    bDecision = MsgBox("Replace value?", vbYesNo + vbQuestion, "Replace or Select value?")

                    If bDecision = vbYes Then
                        Loc = StrRep        'Raise the blade, make the change.
                                            'Re-arrange it 'til it's sane.
                    End If

                    Set Loc = .Cells.FindNext(Loc)

                    If Loc Is Nothing Then Exit Do
                Loop While InStr(fstAddress, Loc.Address) = 0
            End If
        End With
    Next ws

End Sub