我有一本包含数百种不同表格的工作簿。我想循环遍历包含指定字符串的每个工作表,一旦选择了工作表,允许我选择两个不同的单元格并将它们存储为变量。
我的代码通过查找57001
的第一个实例来查找字符串,将其替换为asdfghjklzxcvbnm
(因为否则它将永远使用相同的实例循环),直到没有更多匹配的57001
1}}(我在最后将修改后的值转回原始值)。每次找到指定的字符串时,我都会选择工作表,以便我可以决定是否将范围存储为a1
(之后另一个范围为a2
)或转到下一个匹配项。当a2
获得范围时,循环结束。
以下是我尝试的内容:
Sub Macro1()
On Error Resume Next
Dim Loc As Range
Dim a1 As Range
Dim a2 As Range
Dim sht As Worksheet
shtIndx = ActiveSheet.Index
rplc = "asdfghjklzxcvbnm"
rng1 = "57001"
For i = ActiveSheet.Index + 1 To Sheets.Count
With ThisWorkbook.Worksheets(i)
Set Loc = .Cells.Find(What:=rng1)
a1 = None
If Not Loc Is Nothing Or Not a2 Is Not None Then
Do Until Loc Is Nothing
.Select
.Cells(Loc.Row, Loc.Column).Select
Loc.Value = rplc
If a1 Is None Then
Set a1 = Application.InputBox("First Value?", "Obtain Range Object", Type:=8)
Set Loc = .UsedRange.FindNext(Loc)
Else
Set a2 = Application.InputBox("Second Value?", "Obtain Range Object", Type:=8)
End If
Loop
End If
End With
Next i
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=rplc, Replacement:=rng1, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next sht
Sheets(shtIndx).Select
End Sub
我的代码一直在循环,要求我填充第一个输入框,即使我已经选择了一个单元格。帮助
答案 0 :(得分:0)
通过此修改,将删除永无止境的循环。 我仍然不知道这是否能达到你的意思,逻辑有点模糊。
* Cycle: module.rds.aws_db_instance.this, module.rds.aws_security_group.this