【发布时间】:2023-04-09 02:05:01
【问题描述】:
我想确定非空白单元格之间的列中空白单元格的数量。我有一个过去一年股票每日收盘价的 Excel 电子表格。 excel中的每一行都按时间顺序列出了每日收盘价。电子表格在相关行中列出了我买入股票的日期和卖出股票的日期。特定列用于记录日期。下面是电子表格的摘录。
Stock Date ClosingPrice Entered/Exited DaysHeld?
-------- ------------- --------------- ----------------- ------------
Apple 01/02/2017 116.25 Bought
Apple 01/03/2017 119.75
Apple 01/04/2017 117.50
Apple 01/05/2017 118.75 Sold
我正在尝试使用 VBA excel 中的循环确定“已输入”行到“已退出”行之间的空白单元格数量时间变得很费时间。
编译了以下代码,它计算空白单元格的数量,但它始终返回值 1。在下面的代码中,单元格 AI56 是我购买股票的日期,在“进入/退出”列标题下“购买”
Sub SandpitCountBlankCells()
' SandpitCoutBlankCells Macro
' Macro calculates the number of blank spaces
'
'Step 1: Declare variables
Dim CountBlankCells As Long
CountBlankCells = 0
'Select Cell AI56
Range("AI56").Select
ActiveCell.Offset(-1, -1).Range("A1").Select
'Check wheher the cell in the previous row is blank
If ActiveCell.Value = Blank Then
CountBlankCells = CountBlanksCells + 1
'While loop to check whether cells in the previous rows are blank
While ActiveCell.Value = Blank
ActiveCell.Offset(-1, 0).Range("A1").Select
ActiveCell.Select
CountBlankCells = CountBlanksCells + 1
'Place the value of the number of blank cells in cell AL56
Range("AJ56").Value = CountBlankCells
'Exit the loop
Wend
Range("AJ56").Value = CountBlankCells
End If
End Sub
【问题讨论】: