【问题标题】:Using VBA Excel I am trying to count the number of blank cells in a column between entries使用 VBA Excel 我试图计算条目之间的列中空白单元格的数量
【发布时间】: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

【问题讨论】:

标签: excel vba


【解决方案1】:

当你放:

 Option Explicit 

在您的 vba 模块顶部,您将看到有一个类型错误并且 CountBlanksCells 尚未声明:

 CountBlankCells = CountBlanksCells + 1

应该是:

 CountBlankCells = CountBlankCells + 1

编译器也不喜欢'Blank',而是使用:

  While ActiveCell.Value = Empty

【讨论】:

  • 感谢您的提醒。我更新了我的代码,它运行顺利。
【解决方案2】:

您最好尽可能避免使用SelectActiveCell(请参阅文档中的VBA 最佳实践)。此方法将遍历 Column AI 中的所有内容,并为每个实例吐出空白,而不仅仅是 AI56 中的空白。

Sub CountDays()
Dim ws As Worksheet
Dim x As Long, y As Long
Dim entExtCol As Long, daysHeldCol As Long, startRow As Long, endRow As Long

On Error GoTo ErrHandler

Set ws = ActiveSheet
entExtCol = 35 'Col AI
daysHeldCol = 36 'Col AJ
startRow = 2 'start of data
endRow = ws.Cells(ws.Rows.Count, entExtCol).End(xlUp).Row 'end of data

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

For x = startRow To endRow
    If ws.Cells(x, entExtCol) = "Bought" Then
        ct = 0
        For y = x To endRow
            If ws.Cells(y, entExtCol) = "Sold" Then
                ws.Cells(x, daysHeldCol) = ct
                Exit For
            Else
                ct = ct + 1
            End If
        Next y
    End If
Next x

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

ErrHandler:

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

【讨论】:

  • 如果您对某个答案感到满意,请考虑“接受”它以帮助遇到相同问题的其他用户也找到解决方案:请参阅How does accepting an answer work?
猜你喜欢
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多