2015-02-07 149 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,而第二種方法將會。