1

我有一个包含 3 个组的表(由 B 列中的值“1G、3G 或 5G”定义),如下所示。

对于值 1G 的每次(A 列),我需要测试是否在 1G 的 10 分钟内(如 A 列所示)出现值 3G。如果是这样,那么我需要测试值 5G 是否在 20 分钟内出现(值 1G 的时间)。

因此,读取时间“1G”然后读取时间“3G”,如果 =< 10 分钟,则读取时间“5G”=< 20 分钟。

'Time'    'Group Name'  'Data'
----------
12:08 am       1G        747
12:45 am       1G        745
1:00 am        1G        746
12:36 am       3G        743
12:45 am       3G        747
1:03 am        3G        74
12:50 am       5G        75
1:00 am        5G        741

格式“时间”:9/25/2012 8:37:00 pm

我希望将结果写在每行所有数据的新工作表上。

我已经查看了这个网站和其他网站上的答案,但没有找到答案。感谢您的帮助。

4

2 回答 2

2

这个宏会提取所有10分钟内有3G,20分钟内有5G的1G线路。

   Sub Macro2()
Dim lLastRow As Long, shtOrg As Worksheet, shtDest As Worksheet, rgLoop As Range

'turn off updates to speed up code execution
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With

Set shtOrg = ActiveSheet
Set shtDest = Sheets.Add

lLastRow = shtOrg.Cells(Rows.Count, 1).End(xlUp).Row

shtOrg.Range("D2:D" & lLastRow).FormulaR1C1 = _
        "=IF(RC[-2]=""1G"",IF(COUNTIFS(C[-3],"">="" &RC[-3],C[-3],""<=""&RC[-3]+10/1440,C[-2],""3G"")*COUNTIFS(C[-3],"">="" &RC[-3],C[-3],""<=""&RC[-3]+20/1440,C[-2],""5G"")>0,TRUE,""""),"""")"
shtOrg.Range("E2:E" & lLastRow).FormulaR1C1 = _
        "=N(IF(RC[-3]=""3G"",COUNTIFS(C[-1],TRUE,C[-4],""<=""&RC[-4],C[-4],"">=""&RC[-4]-10/1440),IF(RC[-3]=""5G"",COUNTIFS(C[-1],TRUE,C[-4],""<=""&RC[-4],C[-4],"">=""&RC[-4]-20/1440),"""")))>0"

shtOrg.Range("A1:E" & lLastRow).AutoFilter field:=4, Criteria1:="TRUE"

shtOrg.Range("A1:C" & lLastRow).SpecialCells(xlCellTypeVisible).Copy shtDest.Cells(1, 1)

shtOrg.Range("A1:E" & lLastRow).AutoFilter
shtOrg.Range("A1:E" & lLastRow).AutoFilter field:=5, Criteria1:="TRUE"
shtOrg.Range("A2:C" & lLastRow).SpecialCells(xlCellTypeVisible).Copy shtDest.Cells(Rows.Count, 1).End(xlUp).Offset(1)

shtOrg.Columns("D:E").ClearContents
shtOrg.Range("A1:E" & lLastRow).AutoFilter


With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With


End Sub
于 2012-10-11T23:14:20.643 回答
0

Enter this formula in your first row of data (I've assumed row 2):

=AND(B2="1G",SUM((B3:$B$8="3G")*(A3:$A$8<=A2+"00:10")*(A3:$A$8>=A2)),SUM((B3:$B$8="5G")*(A3:$A$8<=A2+"00:20")*(A3:$A$8>=A2)))

Change the $B$8 and $A$8 in the formula to your last row of data.

After entering the formula, instead of pressing Enter, press Ctrl+Shift+Enter. Curly brackets should appear around your formula.

Now drag the formula down. Anything that evaluates true has matched your conditions. It only searches below the current position, so best to order your data.

Now use an autofilter to get the relevant rows. If I understand correctly you can use a pivottable on another sheet, but I have no idea how that's done :)

于 2012-10-12T03:46:00.060 回答