通过工作表平整循环

时间:2015-08-27 12:30:19

标签: excel vba excel-vba

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

我需要检查的表格是'新表现(2)'

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

sub terfuge
    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)

    For Each WS In Worksheets
        WS.Cells.Replace What:=Search, Replacement:=Replacement, _
        LookAt:=xlPart, MatchCase:=False
    Next

End Sub

此外,是否有人能够告诉我如何进行某种形式的防错 - 我遇到的问题是,如果我在第一个输入框后取消,它会删除已定义的文本(即替换定义的文本空格,因为替换已经定义了)

如果用户点击取消,我怎么能让它无所事事?

5 个答案:

答案 0 :(得分:2)

要调用特定工作表,请使用表格(“工作表名称”)。您可以将工作表设置为代码中的变量。在这种情况下,它是变量WS。

Sheets("Sheet1").Range("A1") = "aaa"

将在Sheet1,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

答案 1 :(得分:1)

取出工作表循环。

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

答案 2 :(得分:0)

这个For循环遍历每张表

For Each WS In Worksheets
WS.Cells.Replace What:=Search, Replacement:=Replacement, _
LookAt:=xlPart, MatchCase:=False
Next

要使其仅替换一张纸上的值,请删除for循环并使用Worksheets("New Performance (2)").Cells...,如下所示:

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

答案 3 :(得分:0)

首先,不要运行你不理解的代码。话虽如此,在您尝试学习时,请在此处查看代码的这一部分:

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

答案 4 :(得分:0)

这应该可以解决问题:

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对象设置为等于工作表" New Performance(2)"

相关问题