我想確定非空白單元格之間的列中空白單元格的數量。我有一個excel電子表格,顯示去年股票的每日收盤價格。 excel中的每一行都按時間順序列出每日收盤價格。電子表格列出了我購買股票的一天以及我在相關行中出售股票的那一天。一個特定的列用於記錄日期。電子表格的摘錄包含在下面。使用VBA 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中的一個循環,我換很多股票和複製和粘貼每次都有一個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
【如何避免在Excel中使用VBA選擇](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba),看看這些問題: [在非空單元格之間計數行](https://stackoverflow.com/questions/7924909/count-rows-between-non-empty-cells)和[計算最後一個單元格和下一個非空單元格之間的行中的空白單元格的數量]空白單元格](https://stackoverflow.com/questions/18988648/count-number-of-blank-cells-in-row-between-last-cell-and-next-non-blank-cell) – danieltakeshi