循环非空白和空白单元格

时间:2013-04-05 11:47:21

标签: vba loops

早上好! 我正在学习VBA并看到了Catering Head的代码放在这里How to perform a sumif using blank cells as a reference?并且它运行正常,但是在我需要实现的逆过程中。在我的电子表格中,计数从第一个非空白单元格开始,然后转到空白单元格,直到到达下一个非空白单元格,然后在最后一个空白单元格中输入总和(从第一个空白单元格中取出A个单元格,直到最后一个空白单元格,然后从下一个非空白单元格重新开始)。 谢谢

00:21.6     10/1/2012 1:43      FALSE       00:21.6
01:47.7     10/1/2012 2:13      FALSE       01:47.7
00:56.7     10/1/2012 2:49      FALSE       
00:54.9     10/1/2012 3:43      
00:11.8     10/1/2012 3:43              2:34(00:56.7 +00:54.9+00:11.8)
02:10.9     10/1/2012 3:46      FALSE       02:10.9     
01:05.4     10/1/2012 3:58      FALSE       
00:55.8     10/1/2012 4:53      
04:41.8     10/1/2012 4:52      
00:26.3     10/1/2012 4:58      
00:04.2     10/1/2012 4:58      
00:15.3     10/1/2012 4:59      
00:06.4     10/1/2012 4:57      
00:10.7     10/1/2012 4:56      
00:04.4     10/1/2012 4:56      
00:04.2     10/1/2012 4:57      
00:29.2     10/1/2012 4:57      
00:34.5     10/1/2012 4:56      
01:22.4     10/1/2012 4:55                       (01:05.400:55.8+04:41.8+...+01:22.4)

00:08.1     10/1/2012 4:55      FALSE       00:08.1
03:20.9     10/1/2012 4:51      FALSE       03:20.9
00:56.3     10/1/2012 5:42      FALSE       00:56.3
02:23.1     10/1/2012 5:51      
01:20.6     10/1/2012 5:48      
00:09.8     10/1/2012 5:49      FALSE       03:53.5(i.e., 02:23.1+01:20.6+00:09.8)
01:40.0     10/1/2012 7:47      FALSE       01:40.0
01:13.4     10/1/2012 8:11      FALSE       01:13.4
00:41.6     10/1/2012 9:49      FALSE       00:41.6
01:08.1     10/1/2012 11:56     FALSE       01:08.1

1 个答案:

答案 0 :(得分:0)

此代码沿着列向下移动,随后添加,然后将答案放入它找到的第一个空白单元格中。

Sub CountMeBlank()

Dim varCounter As Variant

Do Until ActiveCell.Value = ""

    varCounter = varCounter + ActiveCell.Value
    ActiveCell.Offset(1, 0).Select

Loop

ActiveCell.Value = varCounter


'Grabs the formatting from the last cell as the data could be time or numeric.
ActiveCell.Offset(-1, 0).Select
Selection.Copy
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False

'Then colours the cell red to show contrast
ActiveCell.Font.Color = vbRed

End Sub

如果你需要一次遍历所有数据,你需要将它包装在某种形式的循环中。