简体   繁体   English

比较列的 SUM 以查看是否等于或大于阈值

[英]Compare SUM of columns to see if equal or greater than threshold value

I am working on an Excel VBA macro using a Mac.我正在使用 Mac 处理 Excel VBA 宏。 So any code examples would have to work with the Excel for Mac version of Office (Using Office 365)因此,任何代码示例都必须使用 Excel for Mac 版本的 Office(使用 Office 365)

So what I want to do is:所以我想做的是:

Compare the Column A "Name" with its corresponding "Grade" values in Column B to the "Grade" values in all the different Columns D, F, and H "Grade" ("Sheet1")将 A 列的“名称”与其在 B 列中对应的“等级”值与所有不同的 D、F 和 H 列“等级”(“Sheet1”)中的“等级”值进行比较

I want to get every combo of the Column A "Name" with the Column C, E, and G "Name" ("Sheet1")我想获得 A 列“名称”与 C、E 和 G 列“名称”(“Sheet1”)的每个组合

I want to SUM the "Grade" value that is associated to Column A to the "Grade" values in all the possible combo of "Grade" in Columns D, F, and H ("Sheet1")我想将与 A 列相关联的“Grade”值与 D、F 和 H 列(“Sheet1”)中“Grade”的所有可能组合中的“Grade”值相加

I want to see if this SUM is greater to or equal to 250我想看看这个SUM是否大于等于250

See if the value of the SUM of the "Grade" in Column B, Column D, Column F, and Column H in "Sheet1" is greater than or equal to 250.查看“Sheet1”中B栏、D栏、F栏、H栏的“成绩”的SUM值是否大于等于250。

If the "Sheet1" "Grade" SUM is greater to or equal to 250 Then:如果“Sheet1”“Grade”SUM 大于或等于 250,则:

Copy the Column A "Name" with its corresponding Column B "Grade" of "Sheet1" to the first empty row in Column A and B of "Sheet2"将 A 列“名称”及其对应的“Sheet1”的 B 列“等级”复制到“Sheet2”的 A 列和 B 列中的第一个空行

Copy the Column C "Name" with its corresponding Column D "Grade of "Sheet1" to the first empty row in Column C and D of "Sheet2"将 C 列的“名称”及其对应的 D 列“Sheet1”的等级复制到“Sheet2”的 C 列和 D 列中的第一个空行

Copy the Column E "Name" with its corresponding Column F "Grade" of "Sheet1" to the first empty row in Column E and F of "Sheet2"将 E 列“名称”及其对应的“Sheet1”列 F“等级”复制到“Sheet2”列 E 和 F 中的第一个空行

Copy the Column G "Name" with its corresponding Column H "Grade" of "Sheet1" to the first empty row in Column G and H of "Sheet2"将 G 列“名称”及其对应的“Sheet1”列 H“等级”复制到“Sheet2”列 G 和 H 中的第一个空行

So the Column Headers might be:所以列标题可能是:

Column A "Name" A列“名称”

Column B "Grade" B栏“等级”

Column C "Name" C列“名称”

Column D "Grade" D栏“等级”

Column E "Name" E栏“姓名”

Column F "Grade" F栏“等级”

Column G "Name" G栏“名称”

Column H "Grade" H栏“等级”

A sample data set might be:样本数据集可能是:

Fred 80 Jim 80 Bob 50 Bob 40弗雷德 80 吉姆 80 鲍勃 50 鲍勃 40

Sam 60 Jason 10 Fred 85 Anna 97山姆 60 杰森 10 弗雷德 85 安娜 97

Jason 90 Anna 78 Anna 65 Sam 99杰森 90 安娜 78 安娜 65 山姆 99

etc, etc, etc等等等等等等

Results copied to "Sheet2" might be (just some examples, not positive that the math below is correct):复制到“Sheet2”的结果可能是(只是一些例子,不肯定下面的数学是正确的):

Fred 80 Jim 80 Bob 65 Anna 97弗雷德 80 吉姆 80 鲍勃 65 安娜 97

Fred 80 Anna 78 Fred 85 Sam 99弗雷德 80 安娜 78 弗雷德 85 山姆 99

Sam 60 Jim 80 Fred 85 Anna 97山姆 60 吉姆 80 弗雷德 85 安娜 97

Sam 60 Anna 78 Bob 50 Sam 99山姆 60 安娜 78 鲍勃 50 山姆 99

Jason 90 Jim 80 Bob 65 Sam 99杰森 90 吉姆 80 鲍勃 65 山姆 99

Jason 90 Anna 78 Fred 85 Sam 99杰森 90 安娜 78 弗雷德 85 山姆 99

etc, etc, etc等等等等等等

Anything below 250 just wouldn't get copied over to "Sheet2"任何低于 250 的东西都不会被复制到“Sheet2”

Here is my code so far.到目前为止,这是我的代码。

'<---- **** START OF CODE **** ---->

Sub Test()
  '<---- Declare the variables needed
  Dim wb As Workbook, ws1, ws2 As Worksheet, ws1LastRow, ws2LastRow, i As Long

'<---- Set the value of the variables needed for the loop
Set wb = ThisWorkbook
Set ws1 = wb.Worksheets("Sheet1")
Set ws2 = wb.Worksheets("Sheet1")
ws1LastRow = ws1.Cells(Rows.Count, "A").EnColumn D(xlUp).row
ws2LastRow= ws2.Cells(Rows.Count, "A").EnColumn D(xlUp).row

'<---- Loop thru the values of Columns B, D, F, and H of Sheet1
For i = 1 To ws1LastRow
  If WorksheetFunction.SUM(ws1.Cells(i, "B").Value, ws1.Cells(i, "D").Value, ws1.Cells(i, "F").Value, ws1.Cells(i, "H").Value) > 250 Then
'<---- If value of the SUM above is > or = to 250, then copy the Column A:H values of Sheet1 to Sheet2
'<---- Ignore if less than 250
'<----- Make sure to compare every (i, 'A') value with every combo of (i, 'C') value, (i, 'E') value,  and (i, 'G') value
    ws1.Cells(i, "A").Copy Destination:=ws2.Cells(ws2LastRow, "A")
    ws1.Cells(i, "B").Copy Destination:=ws2.Cells(ws2LastRow, "B")
    ws1.Cells(i, "C").Copy Destination:=ws2.Cells(ws2LastRow, "C")
    ws1.Cells(i, "D").Copy Destination:=ws2.Cells(ws2LastRow, "D")
    ws1.Cells(i, "E").Copy Destination:=ws2.Cells(ws2LastRow, "E")
    ws1.Cells(i, "F").Copy Destination:=ws2.Cells(ws2LastRow, "F")
    ws1.Cells(i, "G").Copy Destination:=ws2.Cells(ws2LastRow, "G")
    ws1.Cells(i, "H").Copy Destination:=ws2.Cells(ws2LastRow, "H"): ws2LastRow = ws2LastRow + 1
  End If
Next i
End Sub

'<---- **** END OF CODE **** ---->

Export Data导出数据

Option Explicit

Sub ExportData()
' Needs 'RefColumns'.
    
    ' Source
    Const sName As String = "Sheet1"
    Const sCols As String = "A:H"
    Const sfRow As Long = 2
    Const sfsCol As Long = 2
    Const sStep As Long = 2
    ' Destination
    Const dName As String = "Sheet2"
    Const dFirst As String = "A2"
    ' Other
    Const Minimum As Double = 250
    ' Workbook
    Dim wb As Workbook: Set wb = ThisWorkbook
    
    ' Create a reference to the Source Range ('srg').
    Dim sws As Worksheet: Set sws = wb.Worksheets("Sheet1")
    Dim sfrrg As Range: Set sfrrg = sws.Rows(sfRow).Columns(sCols)
    Dim srg As Range: Set srg = RefColumns(sfrrg)
    If srg Is Nothing Then Exit Sub ' no data
    Dim srCount As Long: srCount = srg.Rows.Count
    Dim cCount As Long: cCount = srg.Columns.Count ' same for src and dest
    
    ' Write the values from the Source Range to the Data Array ('Data').
    Dim Data As Variant: Data = srg.Value
    
    ' Declare additional variables.
    Dim cValue As Variant ' Current Value
    Dim sr As Long ' Current Source Row
    Dim c As Long ' Current Column (same for src and dest)
    Dim dr As Long ' Current Destination Row
    Dim Total As Double ' Current Sum
    
    ' 'Filter' data i.e. write the desired values to the top
    ' of the Data Array.
    For sr = 1 To srCount
        Total = 0
        For c = sfsCol To cCount Step sStep
            cValue = Data(sr, c)
            If IsNumeric(cValue) Then
                Total = Total + cValue
            End If
        Next c
        If Total >= Minimum Then
            dr = dr + 1
            For c = 1 To cCount
                Data(dr, c) = Data(sr, c)
            Next c
        End If
    Next sr
    
    If dr = 0 Then Exit Sub ' no 'Total >= Mininum' found
    
    ' Create a reference to the Destination First Cell ('dfCell').
    Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
    Dim dfCell As Range: Set dfCell = dws.Range(dFirst)
    
    ' Clear the Destination Clear Range ('dclrg') e.g. 'A2:XFD1048576'.
    With dfCell
        Dim dclrg As Range: Set dclrg = .Resize(dws.Rows.Count - .Row, _
            dws.Columns.Count - .Column).Offset(.Row - 1, .Column - 1)
        dclrg.Clear
    End With
    
    ' Write from the Data Array to the Destination Range ('drg').
    Dim drg As Range: Set drg = dfCell.Resize(dr, cCount)
    drg.Value = Data

    ' Inform.
    MsgBox dr & " records found.", vbInformation, "Export Data"

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Creates a reference to the range from the first row of a range
'               ('FirstRowRange') through the row range containing
'               the bottom-most non-empty cell in the row's columns.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function RefColumns( _
    ByVal FirstRowRange As Range) _
As Range
    If FirstRowRange Is Nothing Then Exit Function
    
    With FirstRowRange.Rows(1)
        Dim lCell As Range
        Set lCell = .Resize(.Worksheet.Rows.Count - .Row + 1) _
            .Find("*", , xlFormulas, , xlByRows, xlPrevious)
        If lCell Is Nothing Then Exit Function ' empty range
        Set RefColumns = .Resize(lCell.Row - .Row + 1)
    End With

End Function

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

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