VBA 执行直到找到字符串,然后退出步骤
VBA Do Until string found, then exit step
我有一个文件,其日期值作为第 headers 列,最后一列将始终标记为 "Grand Total"。我想要一种方法来查看 headers 列(位于单元格 D4:I4 中)并输入公式以供稍后填写。
例如,如果我们检查D4,它不包含"Grand Totals",那么我需要在单元格L4和L5中输入以下公式:
Range("L4").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=""Weekending ""&TEXT(RC[-8],""mm/dd/yyy"")&"" Compliant?"""
Range("L5").Select
ActiveCell.FormulaR1C1 = _
"=IF((IF(RC17=""Duplicate"",SUMIF(C2,RC2,C[-8]),RC[-8]))<27,""Compliant"",
IF((IF(RC17=""Duplicate"",SUMIF(C1,RC1,C[-8]),RC[-8]))<30,""Approaching Limit"",
""Over""))"
然后转到E4单元格,如果不包含"Grand Totals",则需要在M4和M5单元格中输入公式...一旦找到"Grand Totals" [at循环的顶部],我需要退出循环(但不是子循环)。
我试图将我发现的代码拼凑起来:
Dim GrTot As String
Dim rng1 As Range
Set rng1 = Range("D4:I4")
GrTot = "Grand Total"
Range("D4").Select
Do While ActiveCell.Value <> GrTot
但我不知道该去哪里。感谢任何帮助。
编辑:
此后我尝试了另一种基于找到的示例的方法 here。这是我目前拥有的:
Dim x As Integer
Dim y As Integer
With Worksheets("Pivot")
x = 4
Do Until .Cells(4, x).Value = "Grand Total"
For y = 12 To 16
.Cells(4, y).Formula = "=""Weekending ""&TEXT(RC[-8],""mm/dd/yyy"")&""
Compliant?"""
.Cells(5, y).Formula = "=IF((IF(RC17=""Duplicate"",SUMIF(C2,RC2,C[-8]),
RC[-8]))<27,""Compliant"",IF((IF(RC17=""Duplicate"",
SUMIF(C1,RC1,C[-8]),RC[-8]))
<30,""Approaching Limit"",""Over""))"
Next y
x = x + 1
Loop
End With
End Sub
但是这个没有正确退出。它会很好地循环,按照我的要求编写公式,但是当它到达 "Grand Total" 列时它不会退出循环。我在这里做错了什么?
遍历范围内的每个单元格;如果单元格没有值,您可以将任何内容写入单元格,然后将某些内容写入偏移量。一旦在您的范围内找到总计,循环就会退出并移动到下一个范围。
For Each item In Range
If item.Value = GrTot Then
Exit For
'Perform action on cell
item.Value = formula
item.offset(0,1).Value = formula
item.offset(0,2).Value = formula
End If
我有一个文件,其日期值作为第 headers 列,最后一列将始终标记为 "Grand Total"。我想要一种方法来查看 headers 列(位于单元格 D4:I4 中)并输入公式以供稍后填写。
例如,如果我们检查D4,它不包含"Grand Totals",那么我需要在单元格L4和L5中输入以下公式:
Range("L4").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=""Weekending ""&TEXT(RC[-8],""mm/dd/yyy"")&"" Compliant?"""
Range("L5").Select
ActiveCell.FormulaR1C1 = _
"=IF((IF(RC17=""Duplicate"",SUMIF(C2,RC2,C[-8]),RC[-8]))<27,""Compliant"",
IF((IF(RC17=""Duplicate"",SUMIF(C1,RC1,C[-8]),RC[-8]))<30,""Approaching Limit"",
""Over""))"
然后转到E4单元格,如果不包含"Grand Totals",则需要在M4和M5单元格中输入公式...一旦找到"Grand Totals" [at循环的顶部],我需要退出循环(但不是子循环)。
我试图将我发现的代码拼凑起来:
Dim GrTot As String
Dim rng1 As Range
Set rng1 = Range("D4:I4")
GrTot = "Grand Total"
Range("D4").Select
Do While ActiveCell.Value <> GrTot
但我不知道该去哪里。感谢任何帮助。
编辑:
此后我尝试了另一种基于找到的示例的方法 here。这是我目前拥有的:
Dim x As Integer
Dim y As Integer
With Worksheets("Pivot")
x = 4
Do Until .Cells(4, x).Value = "Grand Total"
For y = 12 To 16
.Cells(4, y).Formula = "=""Weekending ""&TEXT(RC[-8],""mm/dd/yyy"")&""
Compliant?"""
.Cells(5, y).Formula = "=IF((IF(RC17=""Duplicate"",SUMIF(C2,RC2,C[-8]),
RC[-8]))<27,""Compliant"",IF((IF(RC17=""Duplicate"",
SUMIF(C1,RC1,C[-8]),RC[-8]))
<30,""Approaching Limit"",""Over""))"
Next y
x = x + 1
Loop
End With
End Sub
但是这个没有正确退出。它会很好地循环,按照我的要求编写公式,但是当它到达 "Grand Total" 列时它不会退出循环。我在这里做错了什么?
遍历范围内的每个单元格;如果单元格没有值,您可以将任何内容写入单元格,然后将某些内容写入偏移量。一旦在您的范围内找到总计,循环就会退出并移动到下一个范围。
For Each item In Range
If item.Value = GrTot Then
Exit For
'Perform action on cell
item.Value = formula
item.offset(0,1).Value = formula
item.offset(0,2).Value = formula
End If