简体   繁体   English

计数并转移到新的工作表VBA

[英]Count and transfer to new sheet VBA

I need to find the number of students for each studyboard, I have all of the numbers for each studyboard in column H so my code right now just counts how many time each number occur. 我需要找到每个学习板的学生人数,在H列中找到每个学习板的所有人数,因此我的代码现在只计算每个数字出现多少次。 I was wondering if there was an easier way to do this because as you can see my code is very long. 我想知道是否有更简单的方法来执行此操作,因为如您所见,我的代码很长。 Also i need to transfer the number of students to a new sheet called statistics. 我还需要将学生人数转移到一张称为统计的新表上。 Hope someone can help 希望有人能帮忙

Sub countstudents()
Range("V2") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "1")
Range("V7") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "22")
Range("V8") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "23")
Range("V9") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "24")
Range("V10") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "25")
Range("V11") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "26")
Range("V12") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "28")
Range("V13") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "29")
Range("V14") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "31")
Range("V15") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "32")
Range("V16") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "33")
Range("V17") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "34")
Range("V18") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "35")
Range("V19") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "36")
Range("V20") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "41")
Range("V21") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "81")
Range("V22") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "121")
Range("V23") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "122")
Range("V24") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "163")
Range("V25") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "164")
Range("V26") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "183")
Range("V27") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "184")
Range("V28") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "185")
Range("V29") = Application.WorksheetFunction.CountIf(Range("H2:H18288"), "")

This should do the trick: 这应该可以解决问题:

First we build a 2d array containing all your row numbers from the range formula, and the criteria for your Countif() 首先,我们建立一个二维数组,其中包含范围公式中的所有行号以及Countif()

The we itterate over that array to fill the worksheet cells. 我们遍历该数组以填充工作表单元格。 Important to note is that it is wise to explicitly reference the workbook and the worksheet in which this needs to be pasted. 需要注意的重要一点是,明智地明确引用需要将其粘贴到的工作簿和工作表。 So for example Workbooks("book1.xlsx").worksheets("sheet1").Range() = 因此,例如Workbooks("book1.xlsx").worksheets("sheet1").Range() =

Sub countstudents()
Dim i as long
Dim j as long
dim k as long
Dim arr(1 to 24, 1 to 2) as string
    arr(1, 1) = 2
    arr(1, 2) = 1
    arr(24, 1) = 29
    arr(24, 2) = ""
    For i is 2 to 23
      For j is 1 to 2
         If j = 1 then 
            arr1(i,j) = 5 + i 'starts at 7 and goes till 28
         Else 
            arr1(i,j) = 20 + i 'starts at 22 and goes till 43
         End if
      next j
    next i 

For k = 1 to ubound(arr)
    Range("v"&arr(k,1)) = application.worksheetfunction.countif(Range("H2:H18288"), arr(k,2))
next k
end sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM