【问题标题】:Add CountIf Formula into Cell in VBA Code Syntax在 VBA 代码语法中将 CountIf 公式添加到单元格中
【发布时间】:2017-01-03 19:33:36
【问题描述】:

我想知道是否有人可以帮助我使用这种语法?我正在尝试将CountIf 公式注入我的摘要表(这是这部分代码中的活动表)中的单元格,并且我正在尝试引用工作簿其他工作表中的单元格。我这样做的原因是因为我希望在汇总表之外的工作表中发生任何更改,以便立即在汇总表中更新。

到目前为止,我对这部分的了解是:

If Z=13 Then 'Z denotes the Sheet, in this case Summary Sheet 
  looped = ActiveSheet.Range("A1048576").End(xlUp).Row
  For i=1 to looped 'going through the rows 
    Cells(i, 1) = "=Countifs(Sheets1!A"& i & "Sheets1!A",Sheets2!A," & """ & ""UNTESTED"" & "")""
  Next 
End If 

我遇到的问题是语法

Cells(i, 1) = "=Countifs(Sheets1!A" & i & "Sheets1!A",Sheets2!A," & """ & ""UNTESTED"" & "")""

我希望这一行做的是进入第 13 个工作表之前的每个工作表,并且对于该工作表的每一行 i ,查看 A 列并查看该单元格中是否有“UNTESTED”。如果是,则计数,依此类推。

这可能吗?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    必须是countifs吗?否则,为什么不直接使用excel作为指导创建countifs,然后单独填写呢?如在 excel 中获取一个工作计数,然后将其放入 VBA 代码中。您在上面使用的 countifs 的语法不正确。看起来你会用 countif 语句覆盖每个单元格,我认为这也不是你想要的。

    另外,我在下面的不同子项中有两个示例,以总结每个工作表和 VBA 中的工作簿。我认为其中之一就是您正在寻找的。您可以将值打印到所需的单元格中,而不是直接打印到窗口中。

    如果您在代码中注意到我在 xlUp 之前使用 .Rows.Count 来获取最后一行。这应该保持该代码与其他版本的 Excel 的兼容性。并非所有版本都有 1048576,尽管大多数版本都有。

    Option Explicit
    Sub count_of_untested_total()
    'Summarizes data for the workbook before summary sheet and prints to immediate window
    Dim wks As Worksheet
    Dim lastRow As Long
    Dim cnt As Long
    Dim i As Long
    
    Const SUMMARY_SHEET = 13 'Index of the summary sheet
    
    For Each wks In ThisWorkbook.Worksheets
        With wks
            'For all sheets before the summary sheet
            '(if they were created in order the index should be in
            'order, otherwise you should use names)
            If .Index < SUMMARY_SHEET Then
                'Get last row of current sheet for column A
                lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    
                'Loop through all rows for column A in current sheet
                For i = 1 To lastRow
                    'increment cnt when column has untested
                    'good to enforce common capitalization for comparisons
                    If UCase(.Cells(i, 1)) = "UNTESTED" Then cnt = cnt + 1
                Next i
            End If
        End With
    Next wks
    
    Debug.Print "Number of Untested: " & cnt
    
    End Sub
    
    Sub count_of_untested_per_sheet()
    'Summarizes data per sheet before summary sheet and prints to immediate window
    Dim wks As Worksheet
    Dim lastRow As Long
    Dim cnt As Long
    Dim i As Long
    
    Const SUMMARY_SHEET = 13 'Index of the summary sheet
    
    For Each wks In ThisWorkbook.Worksheets
        With wks
            'reset to 0 for each sheet
            cnt = 0
            'For all sheets before the summary sheet
            '(if they were created in order the index should be in
            'order, otherwise you should use names)
            If .Index < SUMMARY_SHEET Then
                'Get last row of current sheet for column A
                lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    
                'Loop through all rows for column A in current sheet
                For i = 1 To lastRow
                    'increment cnt when column has untested
                    'good to enforce common capitalization for comparisons
                    If UCase(.Cells(i, 1)) = "UNTESTED" Then cnt = cnt + 1
                Next i
            End If
            'Summary on each sheet
            Debug.Print "Number of Untested for " & .Name & ": " & cnt
        End With
    Next wks
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      相关资源
      最近更新 更多