2015-02-07 197 views
2

然后总产值我有三个表工作簿(短跑,HT,RV)。Excel的计算靶细胞

我试图写一个宏/功能,从“短跑”统计有多少倍的值存在于表'RV'内的特定列中,然后在'Dash'内的特定单元格中输出该值

我可以去说'Dash'内的值是静态的并重复它(变量来自'Dash'永远不会改变,因为它是用户名列表)

在我的脑海中,它是这样的:在sheet.RV打印在Dash.B2的列J中的whatever.variable.Dash ...

我能够找到一个MsgBox选项,但我必须手动输入每个用户名(这是一个16字符名称(字符串)),然后MsgBox告诉我发生了什么。我期待只是在宏/功能的固定/静态的用户名自动完成这一选项,因为在“RV”行的量能700项之间,以23K条目

的MSGBOX选项有所不同:

Dim Count as Integer 
Dim Target As String 
Dim Cell as Object 
Dim N As Integer 

Sub Target_Count() 
    Count = 0 
    Target = InputBox("character(s) to find?") 
    If Target = "" Then GoTo Done 
     For Each Cell in Selection 
      N = InStr(1, cell.Value, target) 
      While N <> 0 
       Count = count + 1 
       N = InStr(n + 1, cell.Value, target) 
      Wend 
     Next Cell 
    MsgBox count & " Occurrences of " & target 
Done: 
End Sub 

我想要的输入框中的目标是“Dash.A1:8”和要打印的事件“Dash.B1:8”

回答

0

你可以只使用一个countif()公式而不是编程宏?说的柱你计数“破折号”的在在片RV列B,然后在片材短跑细胞中,公式将是:

=COUNTIF(RV!B:B,"dash")

enter image description hereenter image description here

或者,如果您想改变您的计数,只需将公式中的硬编码“破折号”替换为输入单元格地址即可。

enter image description here

0

如果你想VBA你可以使用这个。无论你想要调整它。

Sub Target_Count_2() 
Dim wb As Workbook 
Set wb = ThisWorkbook 
Dim Cell As Range 
Dim Count As Integer 
Dim LastRow As Long 
LastRow = wb.Worksheets("RV").Range("A1").SpecialCells(xlCellTypeLastCell).Row 
Dim strArr() As Variant 
strArr() = wb.Worksheets("RV").Range("J1:J" & LastRow).Value 
Dim i As Long 
Dim str As String 

    For Each Cell In wb.Worksheets("Dash").Range("B1:B8") 
     Count = 0 
     str = Cell.Offset(, -1).Value2 
     For i = LBound(strArr) To UBound(strArr) 
      If str = strArr(i, 1) Then Count = Count + 1 
      'If InStr(strArr(i, 1), str) > 0 Then Count = Count + 1 
     Next 
     Cell.Value2 = Count 
    Next 

Set Cell = Nothing 
Set wb = Nothing 
End Sub 

注意str = strArr(i, 1)将在小区对应唯一的全方位价值,同时InStr(strArr(i, 1), str) > 0也将在小区对应的部分。假设您正在寻找值为"AAAB"的单元格中的"AAA"。第一种方法不会将其他1添加到Count,而第二种方法将会。