通过工作表展平一个循环

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

我需要检查的工作表是'New Performance(2)'

我很想了解的过程,以及知道答案,因为我正在努力学习如何做更多的macros:)

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 

另外,有人能告诉我如何把一些forms的错误校对 – 我与它的问题是,如果我在第一个input框后取消,它将删除定义的文本(即用空白replace定义的文本为replace已经被定义)

任何想法,如果我可以让它不做任何事,如果用户点击取消?

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

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

将在Sheet1,A1中写入aaa

所以将:

 dim WS as worksheet set WS = Sheets("Sheet1") WS.Range("A1")= "aaa" 

在你的循环中,每个工作表都被分配给variablesWS,并发生一些事情。 只需使用一个指定的工作表来更改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) '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 

这个For循环遍历每个表单

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

为了使它只replace一张纸上的值,删除for循环并使用Worksheets("New Performance (2)").Cells...就像这样:

 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对象设置为“New Performance(2)”