如何使用列和行设置范围

how to set a range using columns and rows

尝试获取范围设置以使用工作表函数 COUNTIF。这是我得到的:

Function count_if(work_sheet As String, criteria As String, column_num As Integer)

    Dim rows As Integer
    rows = Get_Rows_Generic(work_sheet, 1) ' get the number of rows in another sheet

    Dim full_range As Range
    With work_sheet
        Set full_range = .Range(.Cells(0, rows), .Cells(0, column_num))
    End With

    count_result = WorksheetFunction.CountIf(full_range, criteria)

    count_if = range_size
End Function

Sub test_stuff()
    Dim n As Integer
    n = count_if("usersFullOutput.csv", "TRUE", 9)
    MsgBox n
End Sub

当我 运行 代码时,excel 要求我选择另一个宏。我猜这是我设置范围的方式,但我不知道。

您的思路是正确的,但语法有些错误。您需要设置开始单元格和结束单元格,使用row_index和col_index,例如:

Set full_range = .Range(.Cells(1,1), .Cells(rows, column_num)

如果有帮助请告诉我

第 1 点

IF "usersFullOutput.csv" 实际上是您的工作表名称(不是文件名),使用此名称您不能这样做:

With work_sheet
    Set full_range = .Range(...)
End With

范围是工作表 对象 的 属性,而不是工作表名称 string。尝试这样做:

With Worksheets(work_sheet)
    Set full_range = .Range(...)
End With

第 2 点

Set full_range = .Range(.Cells(0, rows), .Cells(0, column_num))

Cells()的第一个参数是行号。行号永远不能是 0。 Excel 中的第一行始终是 1。 A1 将被 Cells(1, 1) 引用。也许你需要像

这样的东西
Set full_range = .Range(.Cells(1, 1), .Cells(rows, column_num))

第 3 点

range_size 未定义(count_if = range_size 行)。

我觉得你需要

count_if = count_result