Vba 在工作表中展平循环

Vba 在工作表中展平循环,vba,excel,Vba,Excel,我在excel电子表格中有以下代码,需要对其进行修改,以便它只替换一张表格上的输入数据,而不是检查所有表格 我需要检查的表格是“新性能(2)” 我很想了解这个过程,也很想知道答案,因为我正在努力学习如何用宏做更多的事情:) 另外,有谁能告诉我如何进行某种形式的防错-我的问题是,如果在第一个输入框后取消,它会删除定义的文本(即,将定义的文本替换为空白,因为替换尚未定义) 你知道如果用户点击cancel,我怎么能让它什么都不做吗?这个For循环正在每个工作表中迭代 For Each WS In Wo

我在excel电子表格中有以下代码,需要对其进行修改,以便它只替换一张表格上的输入数据,而不是检查所有表格

我需要检查的表格是“新性能(2)”

我很想了解这个过程,也很想知道答案,因为我正在努力学习如何用宏做更多的事情:)

另外,有谁能告诉我如何进行某种形式的防错-我的问题是,如果在第一个输入框后取消,它会删除定义的文本(即,将定义的文本替换为空白,因为替换尚未定义)


你知道如果用户点击cancel,我怎么能让它什么都不做吗?

这个
For
循环正在每个工作表中迭代

For Each WS In Worksheets
WS.Cells.Replace What:=Search, Replacement:=Replacement, _
LookAt:=xlPart, MatchCase:=False
Next
要使其仅替换一张工作表上的值,请删除for循环并使用
工作表(“新性能(2)”)。单元格…
如下所示:

Worksheets("New Performance (2)").Cells.Replace What:=Search, Replacement:=Replacement, _
    LookAt:=xlPart, MatchCase:=False

首先,不要运行您不理解的代码。也就是说,当您试图学习时,请在此处查看代码的这一部分:

For Each WS In Worksheets
    WS.Cells.Replace What:=Search, Replacement:=Replacement, _
    LookAt:=xlPart, MatchCase:=False
Next
上面写着“把所有打开的工作表都拿走。”。对于每个工作表,执行以下任务,一次一个

要使其仅适用于您的特定工作表,只需更改正在查看的工作表,如下所示:

Sheets("New Performance(2)").Cells.Replace What:=Search, Replacement:=Replacement, _
    LookAt:=xlPart, MatchCase:=False

这应该可以做到:

Dim WS As Worksheet
Dim Search As String
Dim Replacement As String
Dim Prompt As String
Dim Title As String
Dim MatchCase As Boolean

Prompt = "Replace Merchant ID?"
Title = "Search Value Input"
Search = InputBox(Prompt, Title)

Prompt = "What is the replacement value?"
Title = "Search Value Input"
Replacement = InputBox(Prompt, Title)

Set WS = ThisWorkbook.Worksheets("New Performance (2)")
WS.Cells.Replace What:=Search, _
                 Replacement:=Replacement, _
                 LookAt:=xlPart, _
                 MatchCase:=False
原因是:

在您发布的代码中,您正在遍历所有工作表,并将WS对象设置为等于每个工作表

因此,在我发布的代码中,我删除了以for和next开头的行,从而摆脱了for循环。
然后我将WS对象设置为工作表“新性能(2)”

取出工作表循环

Dim WS As Worksheet
Dim Search As String
Dim Replacement As String
Dim Prompt As String
Dim Title As String
Dim MatchCase As Boolean

Prompt = "Replace Merchant ID?"
Title = "Search Value Input"
Search = InputBox(Prompt, Title)

Prompt = "What is the replacement value?"
Title = "Search Value Input"
Replacement = InputBox(Prompt, Title)

'Here you will set the worksheet to the sheet you want to search.
Set ws = ActiveWorkbook.Sheets("New Performance (2)")
ws.Activate
WS.Cells.Replace What:=Search, Replacement:=Replacement, _
LookAt:=xlPart, MatchCase:=False

End Sub
对于“防错”,您可以分析退货

Search = InputBox(Prompt, Title)
If Search = "" then
    Exit sub
End if
或者您可以使用消息框

  ' Displays a message box with the yes and no options.
  Response = MsgBox("Are you sure you want to exit?", vbYesNo)

  ' If statement to check if the yes button was selected.
  If Response = vbYes Then
      'Do something
  Else
      'Do something
  End If

  These are the return values

    Constant    Value   Description
    --------    -----   ----------
    vbOK        1       OK
    vbCancel    2       Cancel
    vbAbort     3       Abort
    vbRetry     4       Retry
    vbIgnore    5       Ignore
    vbYes       6       Yes
    vbNo        7       No

要调用特定工作表,请使用工作表(“工作表名称”)。可以将工作表设置为代码中的变量。在本例中,它是变量WS

Sheets("Sheet1").Range("A1") = "aaa"
将在表1、A1中写入aaa

我们也将:

dim WS as worksheet
set WS = Sheets("Sheet1")
WS.Range("A1")= "aaa"
在循环中,每个工作表都被分配给变量WS,并且会发生一些事情。只需使用一个指定的工作表更改For Each循环:

Dim WS As Worksheet
Dim Search As String
Dim Replacement As String
Dim Prompt As String
Dim Title As String
Dim MatchCase As Boolean

Prompt = "Replace Merchant ID?"
Title = "Search Value Input"
Search = InputBox(Prompt, Title)

Prompt = "What is the replacement value?"
Title = "Search Value Input"
Replacement = InputBox(Prompt, Title)

Set WS =  Sheets("New Performance (2)")
WS.Cells.Replace What:=Search, Replacement:=Replacement, _
LookAt:=xlPart, MatchCase:=False


End Sub

我不明白。你想在工作表上查些什么?在我的回答中添加了一些防错的信息。他们都来得很快!哇,我在发帖的时候被你和其他三个人打败了!
Dim WS As Worksheet
Dim Search As String
Dim Replacement As String
Dim Prompt As String
Dim Title As String
Dim MatchCase As Boolean

Prompt = "Replace Merchant ID?"
Title = "Search Value Input"
Search = InputBox(Prompt, Title)

Prompt = "What is the replacement value?"
Title = "Search Value Input"
Replacement = InputBox(Prompt, Title)

Set WS =  Sheets("New Performance (2)")
WS.Cells.Replace What:=Search, Replacement:=Replacement, _
LookAt:=xlPart, MatchCase:=False


End Sub