简体   繁体   English

VBA遍历空列

[英]VBA loop through empty columns

I am stuck on how to complete a loop with a certain result. 我被困在如何以一定的结果完成循环。

I have attached a screen shot of what i basically want to happen. 我已附上我基本上想发生的事情的屏幕截图。

The VBA would start off in B1 i want the VBA to loop through empty columns to the right until it gets to any character/string(the result can appear in any cell to the right of B1,and is a variable word.) Once it finds a cell with any characters in, i want to copy it and move it to B5. VBA将从B1开始,我希望VBA在右边的空列中循环,直到到达任何字符/字符串为止(结果可以出现在B1右边的任何单元格中,并且是可变单词。)找到一个包含任何字符的单元格,我想将其复制并将其移至B5。 I have the following code, and it nearly does what i want apart from the fact it just keeps looping. 我有以下代码,除了它不断循环外,它几乎可以实现我想要的功能。

Code: 码:

Sub looptillnotempty()
    Dim notempty As Boolean
    notempty = False
    Do Until notempty
        If IsEmpty(ActiveCell) = True Then
            ActiveCell.Offset(0, 1).Select
        Else
            ActiveCell.Copy
            Range("C5").PasteSpecial Paste:=xlPasteFormulas
        End If
    Loop
    Range("C8").Select
End Sub

What I am trying to accomplish: : 我要完成的工作

在此处输入图片说明

You can exit the loop with Exit Do as follows: 您可以使用Exit Do退出循环,如下所示:

Sub looptillnotempty()
    Dim notempty As Boolean
    notempty = False
    Do Until notempty
        If IsEmpty(ActiveCell) = True Then
            ActiveCell.Offset(0, 1).Select
        Else
            ActiveCell.Copy
            Range("B5").PasteSpecial Paste:=xlPasteFormulas
            Exit Do
        End If
    Loop
    Range("C8").Select
End Sub

You can go like follows 你可以像下面这样去

Sub looptillnotempty()
  With ActiveSheet.Range("B1").End(xlToRight)
      If .Value <> "" Then ActiveSheet.Range("B5").Formula = .Formula
  End With
  Range("C8").Select
End Sub

Just choose which cell to copy found cell formula into, since in your question text you wrote "B5", in your code example you wrote "C5" and in the screenshot it seems to be "A5"... 只需选择要将找到的单元格公式复制到哪个单元格中,因为在您的问题文本中您编写了“ B5”,在代码示例中您编写了“ C5”,并且在屏幕截图中似乎是“ A5” ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM