Vba CountIf空白单元格和公式

Vba CountIf空白单元格和公式,vba,excel,excel-formula,countif,Vba,Excel,Excel Formula,Countif,我的问题是,我想使用VBA的countif函数排除空白单元格 大约有65行,但如果我设置一个范围K2:K1000,它会计算每个空白单元格,而我只需要计算excel中使用过的单元格。新的单元格将被添加,因此在不更改任何内容的情况下,我需要通过单击命令按钮,根据使用的单元格自动放置结果 如何通过VBA代码执行此操作?我尝试使用动态范围,但由于某些行的空白单元格,它不起作用。我们将感谢您的支持 Sub Button1_Click() Range("A3") = WorksheetFunctio

我的问题是,我想使用VBA的
countif
函数排除空白单元格

大约有65行,但如果我设置一个范围
K2:K1000
,它会计算每个空白单元格,而我只需要计算excel中使用过的单元格。新的单元格将被添加,因此在不更改任何内容的情况下,我需要通过单击命令按钮,根据使用的单元格自动放置结果

如何通过VBA代码执行此操作?我尝试使用动态范围,但由于某些行的空白单元格,它不起作用。我们将感谢您的支持

Sub Button1_Click()
    Range("A3") = WorksheetFunction.CountA(Range("K:K"))Range("K9:K1000").Rows.Count
    Range("A4") = WorksheetFunction.CountIf(Range("K9:K1000"), "")
End Sub

好的,尝试动态查找K列中的最后一行:

Sub Button1_Click()

    Dim lLastRow As Long

    ' This give you the last used row in column K of the Active Sheet.
    lLastRow = Cells(Rows.Count, 11).End(xlUp).Row

    ' This is the same function you were using.
    Range("A3") = WorksheetFunction.CountIf(Range("K9:K" & lLastRow), "<>")
    Range("A4") = WorksheetFunction.CountIf(Range("K9:K" & lLastRow), "")


End Sub
子按钮1\u单击()
昏暗的灯塔一样长
'这将为您提供活动工作表K列中最后使用的行。
lLastRow=单元格(Rows.Count,11).End(xlUp).Row
'这与您使用的功能相同。
Range(“A3”)=工作表函数.CountIf(Range(“K9:K”和lLastRow),“”)
范围(“A4”)=工作表函数.CountIf(范围(“K9:K”和lLastRow),“”)
端接头

好的,尝试动态查找K列中的最后一行:

Sub Button1_Click()

    Dim lLastRow As Long

    ' This give you the last used row in column K of the Active Sheet.
    lLastRow = Cells(Rows.Count, 11).End(xlUp).Row

    ' This is the same function you were using.
    Range("A3") = WorksheetFunction.CountIf(Range("K9:K" & lLastRow), "<>")
    Range("A4") = WorksheetFunction.CountIf(Range("K9:K" & lLastRow), "")


End Sub
子按钮1\u单击()
昏暗的灯塔一样长
'这将为您提供活动工作表K列中最后使用的行。
lLastRow=单元格(Rows.Count,11).End(xlUp).Row
'这与您使用的功能相同。
Range(“A3”)=工作表函数.CountIf(Range(“K9:K”和lLastRow),“”)
范围(“A4”)=工作表函数.CountIf(范围(“K9:K”和lLastRow),“”)
端接头

这是如何计算第二列(
B
)中的非空值:

计算空格时:

Range("A4") = WorksheetFunction.CountBlank(Columns(2))

这是如何计算第二列(
B
)中的非空值:

计算空格时:

Range("A4") = WorksheetFunction.CountBlank(Columns(2))

如果您想知道K列中有多少空单元格与usedrange相对,则可以这样做:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
Dim Lastrow As Long, useRange As Long
Lastrow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row
'get the last row with data on Column K
useRange = ws.UsedRange.Rows.Count
'get the last row with data on your UsedRange    
MsgBox "Your ratio of empty cells in Column K is: " & Lastrow & "/" & useRange
End Sub

如果您想知道K列中有多少空单元格与usedrange相对,则可以这样做:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
Dim Lastrow As Long, useRange As Long
Lastrow = ws.Cells(ws.Rows.Count, "K").End(xlUp).Row
'get the last row with data on Column K
useRange = ws.UsedRange.Rows.Count
'get the last row with data on your UsedRange    
MsgBox "Your ratio of empty cells in Column K is: " & Lastrow & "/" & useRange
End Sub
试着用这个

Sub Button1_Click()
Range("A3") = Worksheets("Sheet1").Columns(11).SpecialCells(xlCellTypeConstants).Count
Range("A4") = Worksheets("Sheet1").Columns(11).SpecialCells(xlCellTypeBlanks).Count
End Sub
试着用这个

Sub Button1_Click()
Range("A3") = Worksheets("Sheet1").Columns(11).SpecialCells(xlCellTypeConstants).Count
Range("A4") = Worksheets("Sheet1").Columns(11).SpecialCells(xlCellTypeBlanks).Count
End Sub

如果您有一个要检查的静态范围(或者像一些人已经建议的那样使用.end(xlUp)创建一个动态范围),这个函数应该可以正常工作

Public Sub CountPopulatedCells()

    Dim wb          As Workbook
    Dim ws          As Worksheet
    Dim lrow        As Long
    Dim checkRng    As Range
    Dim outputRng   As Range

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets(1)
    Set outputRng = ws.Range("C3")

    'Finds the last populated row in given column
    lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row

    'Creates a range using the previously created "lrow"
    Set checkRng = ws.Range("A2:A" & lrow)

    'Calls the function and feeds it "myRng" and puts the result in "outputRng"
    outputRng.Value = CountNonBlanks(checkRng)

End Sub

Public Function CountNonBlanks(rng As Range) As Integer

    Dim cell As Range

    'Zeros the ingteger before counting
    CountNonBlanks = 0

    'Checks each cell in range given (in this case "rng")
    For Each cell In rng

        'Checks if the cell is empty or not
        If Not IsEmpty(cell) Then

            'Adds 1 to the output integer for each non-empty cell
            CountNonBlanks = CountNonBlanks + 1

        End If

    Next

End Function
我发现使用xlCellTypeConstants和WorksheetFunction有时会出现奇怪的错误。我相信他们这样做的原因很简单(这可能是我的错),但我从来没有遇到过上述问题

此函数将循环指定范围内的每个单元格,并将值1添加到函数输出整数。每次运行宏时,整数本身将为零

Edit1:我之所以选择将“rng”注入函数,而不是在函数本身中创建它,是为了让您可以在整个主sub中的多个不同范围内使用它


Edit2:我添加了调用函数的sub本身,以及对每个部分的功能的一些注释。

如果您有一个要检查的静态范围(或使用.end(xlUp)创建一个动态范围,正如一些人已经建议的那样),此函数应该可以正常工作

Public Sub CountPopulatedCells()

    Dim wb          As Workbook
    Dim ws          As Worksheet
    Dim lrow        As Long
    Dim checkRng    As Range
    Dim outputRng   As Range

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets(1)
    Set outputRng = ws.Range("C3")

    'Finds the last populated row in given column
    lrow = ws.Cells(Rows.Count, 1).End(xlUp).Row

    'Creates a range using the previously created "lrow"
    Set checkRng = ws.Range("A2:A" & lrow)

    'Calls the function and feeds it "myRng" and puts the result in "outputRng"
    outputRng.Value = CountNonBlanks(checkRng)

End Sub

Public Function CountNonBlanks(rng As Range) As Integer

    Dim cell As Range

    'Zeros the ingteger before counting
    CountNonBlanks = 0

    'Checks each cell in range given (in this case "rng")
    For Each cell In rng

        'Checks if the cell is empty or not
        If Not IsEmpty(cell) Then

            'Adds 1 to the output integer for each non-empty cell
            CountNonBlanks = CountNonBlanks + 1

        End If

    Next

End Function
我发现使用xlCellTypeConstants和WorksheetFunction有时会出现奇怪的错误。我相信他们这样做的原因很简单(这可能是我的错),但我从来没有遇到过上述问题

此函数将循环指定范围内的每个单元格,并将值1添加到函数输出整数。每次运行宏时,整数本身将为零

Edit1:我之所以选择将“rng”注入函数,而不是在函数本身中创建它,是为了让您可以在整个主sub中的多个不同范围内使用它


Edit2:我添加了调用函数的sub本身,以及对每个部分的作用的一些注释。

如果只需要计算第65行,为什么要使用1000作为最后一行?因为它是一个共享文件夹,因此每个人都可以在该文件中添加新行。我不想每次都手动更改这两个公式的范围。此
range(“A3”)=WorksheetFunction.CountA(range(“K:K”))range(“K9:K1000”)。Rows.Count
是无效语法,甚至无法编译!这个公式给出了填充单元格和行数的比率。我还需要将该公式转换为动态范围,如果您只需要计算到第65行,为什么要使用1000作为最后一行?因为它是一个共享文件夹,因此每个人都可以在该文件中添加新行。我不想每次都手动更改这两个公式的范围。此
range(“A3”)=WorksheetFunction.CountA(range(“K:K”))range(“K9:K1000”)。Rows.Count
是无效语法,甚至无法编译!这个公式给出了填充单元格和行数的比率。我需要将这个公式转换为动态范围,以及计算空白
工作表函数。CountBlank
@Pᴇʜ-完全正确!:)这些人拥有它,你从非空白计数中减去空白计数,以获得单元格的数目不为空(>0)。我只是在我的代码中的某个地方这样做了,让我找到它,因为当创建一列唯一的名称时,您通常会得到一个空白。Templer是最后一行变量,用于每个月的工作表,因此它会更改。holderCount=Application.WorksheetFunction.CountA(ThisWB.Worksheets(“八月”).Range(“AA1:AA”和tempr))\uu-Application.WorksheetFunction.CountBlank(ThisWB.WorksheetFunction(“八月”).Range(“AA1:AA”和tempr)),并用于计算空格
WorksheetFunction.CountBlank
@Pᴇʜ-完全正确!:)这些人拥有它,你从非空白计数中减去空白计数,以获得单元格的数目不为空(>0)。我只是在我的代码中的某个地方这样做了,让我找到它,因为当创建一列唯一的名称时,您通常会得到一个空白。Templer是最后一行变量,用于每个月的工作表,因此它会更改。holderCount=Application.WorksheetFunction.CountA(ThisWB.Worksheets(“八月”).Range(“AA1:AA”和tempr))\uU2-Application.Worksheet