简体   繁体   中英

Need some help on basics of excel-VBA

I am interested in counting a variable (male) in column A and another variable (working) in column B. I want the output to be the total number of times both 'men' and 'working' are displayed together. Any help is highly appreciated. Thanks in advance

Consider the following data

在此处输入图片说明

VBA solution

Use the WorksheetFunction.CountIfs method . The following code should return 4 :

Option Explicit

Sub CountMaleWorking()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")

    Debug.Print WorksheetFunction.CountIfs(ws.Columns("A"), "male", ws.Columns("B"), "working")
End Sub

Or just use a forumla

=COUNTIFS(A:A,"male",B:B,"working")

As GSerg mentioned, no vba but a simple countifs.

Put in C1

=COUNTIFS(A1:A100,"male",B1:B100,"working")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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