如何使用列和行设置范围
试图让范围设置使用工作表函数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让我select另一个macros。 我猜这就是我设定的范围,但我不知道。
点#1
如果“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
你在正确的轨道上,但语法有些不对。 您需要使用row_index和col_index设置开始单元格和结束单元格,例如:
Set full_range = .Range(.Cells(1,1), .Cells(rows, column_num)
让我知道如果有帮助