I would like to determine the number of blank cells in a column between non-blank cells. I have an excel spreadsheet of the daily closing prices of a stock over the last year. Each row in excel lists the daily closing prices in chronological order. The spreadsheet lists the day I bought the stock and the day I sold the stock on the relevant row. A specific column is used to record the date. An extract of the spreadsheet is contained below.
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
I am trying the determine the number of blank cells between the 'Entered' row to the 'Exited' row under column heading Entered/Exited using a loop in VBA excel as I trade many stocks and copying and pasting an excel formula every time has become time consuming.
The compiled the following code which counts the number of blank cells, however it always returns the value 1. In the code below, cell AI56 is the day I bought the stock, 'bought' under column heading 'Entered/Exited'
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