【问题标题】:If and Do Until Loop EXCEL VBA如果和做直到循环EXCEL VBA
【发布时间】:2017-06-09 15:10:31
【问题描述】:

如果有人可以帮助我我在这里做错了什么,是 VBA 的新手。 尝试运行循环以查找特定文本,启动循环然后在特定点停止。 循环是这样的,我希望它在我的工作表中复制下面的一些值,因此 a 是 55。 我面临错误 Block IF without End If

代码如下:

Private Sub CommandButton3_Click()
For y = 1 To 15 Step 5
Dim x As Double
Dim a As Double
x = 1
a = 55
If Cells(x, y).Value = "Text1" Then
Do Until Cells(x, y).Value = "Text2"
Cells(a, y) = Cells(x, y).Value
Cells(a, y + 1) = Cells(x, y + 1)
x = x + 1
a = a + 1


Loop


End Sub

【问题讨论】:

  • 如果你缩进你的代码,你会发现 2 个错误:1. If Cells(x, y).Value = "Text1" Then without End If, 2. For y = 1 To 15 Step 5 without Next y
  • 如果我使用 if 然后我认为我不需要使用 End if?
  • AFAIK only 时间你不需要End If 是如果你的If 语句在一行中......即使那样,最好的做法是使用一个结构化的If 语句。例如,这不需要End If:If myVal = 1 then Cells(1,1).Value = "Okay"全部在一行。
  • 请参阅this answer 以获得关于正确缩进如何工作的漂亮图形描述,并知道Rubberduck 包含一个非常可定制的Smart Indenter,它可以为您自动完成(完整的免责声明:我积极参与 Rubberduck 开源项目)。

标签: vba excel if-statement


【解决方案1】:

缩进是前进的方向,你有一个没有nextfor 语句和一个没有End Ifif

Private Sub CommandButton3_Click()
    For y = 1 To 15 Step 5
        Dim x As Double
        Dim a As Double
        x = 1
        a = 55
        If Cells(x, y).Value = "Text1" Then
            Do Until Cells(x, y).Value = "Text2"
                Cells(a, y) = Cells(x, y).Value
                Cells(a, y + 1) = Cells(x, y + 1)
                x = x + 1
                a = a + 1
            Loop
        End If
    Next y
end sub

【讨论】:

  • 谢谢汤姆!出于某种原因,我没有得到任何输出。我想要的是我的代码从第一列开始,遍历它并找到 Text1,如果它确实将值复制到单元格 (a,1),直到它到达一个具有 Text2 的单元格,然后它停止。我错过了什么吗?
  • 还应该将声明移到 For 循环之前。
  • 另外,x 和 a 应该是 Long 或 Integer,而不是 Double
  • @IsraShaikh "从第一列开始,通过它并找到 Text1" 您的代码只会检查第一行的 Text1。在找到 Text1 之前,x(行)没有增量。因此,如果Cells(1, 1) <> "Text1" 您将立即进入外循环的下一次迭代(y = 6)。
  • @IsraShaikh 正是另一个答案的作用。在 if 语句周围添加一个循环,该循环递增 x(行),直到找到 Text1 或到达列的末尾。
【解决方案2】:

除了我在您的帖子的 cmets 中提到的问题之外,如果我理解正确,您希望在 A 列的单元格上循环,找到第一个“Text1”,然后将所有单元格复制到第 55 行及以下,直到你找到“Text2”。如果是这种情况,请尝试以下代码:

Private Sub CommandButton3_Click()

Dim x As Long, y As Long
Dim a As Long
Dim LastRow As Long

With Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
    For y = 1 To 15 Step 5

        x = 1  '<-- reset x and a (rows) inside the columns loop
        a = 55 '<-- start pasting from row 55

        LastRow = .Cells(.Rows.Count, y).End(xlUp).Row
        While x <= LastRow '<-- loop until last row with data in Column y
            If .Cells(x, y).Value Like "Text1" Then
                Do Until .Cells(x, y).Value = "Text2"
                    .Cells(a, y).Value = .Cells(x, y).Value
                    .Cells(a, y + 1).Value = .Cells(x, y + 1).Value
                    x = x + 1
                    a = a + 1
                Loop
            End If
            x = x + 1
        Wend
    Next y
End With

End Sub

【讨论】:

  • 是的,谢谢你!我如何编辑它以检查每五列?我添加了以下 for 循环,但它没有用。对于 y = 1 到 15 步骤 5
  • @IsraShaikh 你想为每 5 列做一次精确的复制粘贴吗?直到第 15 列?
  • 是的@ShaiRado 所以首先它在第 1 列运行,然后在第 6 列运行,直到第 15 列
  • @IsraShaikh 测试编辑后的代码,应该像你想要的那样工作
  • 它的工作原理谢谢!你能解释一下每一行代码吗! VBA 初学者真的可以伸出援助之手。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多