2012-09-27 310 views
-1

所以我需要一些幫助。我很新的VBA,所以即時通訊有一些麻煩。 嗯,我的工作書中有多張(excel)。我試圖做的是,計算多少個單元格在D列中具有單詞「IMCOMPLETE」的百分比,並將結果放在特定單元格的主表中。示例:VBA計算百分比

Sub Get_Percentage() 

If Range("Jackson,_Mr._Vince_R.TrainingSt'!D2:D100").Value = "IMCOMPLETE" Then 
    put outcome in "TotalSummery%"!E2 
If Range("Carter,_Mr._Oscar_R_(Oscar)Trai'!D2:D100").Value = "IMCOMPLETE" Then 
    put outcome in "TotalSummery%"!E4 
If Range("Taravella,_Mr._Jim_(Jim)Trainin'!D2:D100") Value = "IMCOMPLETE" Then 
    put outcome in "TotalSummery%"!E5 

End Sub 

僅供參考:我有10個工作表標籤。不知道這是不是一個宏。

+3

請避免在您的問題或主題中使用「Please help」。並且儘可能避免使用SHOUTING(除非在您的問題中使用'INCOMPLETE'非常重要)。無需簡單地發短信讓文本更難閱讀,尋求幫助不會早日爲您解決。每個在這裏提出問題的人都同樣重要,並且乞討不會將您列入優先列表。謝謝。 –

+0

幾個提示,你需要'結束如果',而不是'放置結果'你需要'範圍(「TotalSummery%」!E2).Value2 =「一個值」'。要查找有多少單元格不完整,可以使用With .ActiveSheet .Cells.Find(What:=「IMCOMPLETE」,After:= objExcel.ActiveCell,LookIn:= objExcel.XlFindLookIn.xlValues,LookAt:= objExcel.XlLookAt。 xlPart,SearchOrder:= objExcel.XlSearchOrder.xlByRows,SearchDirection:= objExcel.XlSearchDirection.xlNext,MatchCase:= False) –

+0

@Ken,對不起。完全理解。 – user1701878

回答

0
Sub FindAndCountWordInExcelWorkBook(Byval SearchString As String) 

SearchString = "IMCOMPLETE" 

Dim oRange As Range, aCell As Range, bCell As Range 
    Dim ws As Worksheet 
    Dim ExitLoop As Boolean 
    Dim FoundAt As String 
    On Error GoTo Err 
    Dim i As Integer 
    For i = 1 To Worksheets.Count 

     Set ws = Worksheets(i) 
     Set oRange = ws.UsedRange 

     Dim CountOfKeyWord As Integer 

     Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) 
     If Not aCell Is Nothing Then 
      Set bCell = aCell 
      FoundAt = aCell.Address 
      Do While ExitLoop = False 
       Set aCell = oRange.FindNext(After:=aCell) 

       If Not aCell Is Nothing Then 
        If aCell.Address = bCell.Address Then Exit Do 
        CountOfKeyWord = CountOfKeyWord + 1 
        FoundAt = FoundAt & ", " & aCell.Address 
       Else 
        ExitLoop = True 
       End If 
      Loop 
     Else 
      ' MsgBox SearchString & " not Found" 
     End If 

    Next i 

    MsgBox "The Search String: " & SearchString & ", appeared " & CountOfKeyWord & " times at these locations: " & FoundAt 
    Exit Sub 
Err: 
    MsgBox Err.Description 
End Sub 
0

這是一個簡單的方法來做到這一點。我正在做一張紙。您可以在一個循環

Sub Sample() 
    Dim ws As Worksheet 
    Dim SearchText As String 
    Dim WordCount As Long, ColDTotalWordCount As Long 
    Dim PercentageWord As Double 

    Set ws = ThisWorkbook.Sheets("Sheet1") 

    SearchText = "IMCOMPLETE" 

    With ws 
     '~~> Count the occurances of the word "IMCOMPLETE" 
     WordCount = Application.WorksheetFunction.CountIf(.Columns(4), SearchText) 

     '~~> Count the total words in Col D 
     ColDTotalWordCount = Application.WorksheetFunction.CountA(.Columns(4)) 

     '~~> Calculate Percentage 
     PercentageWord = WordCount/ColDTotalWordCount 
     Debug.Print Format(PercentageWord, "00.00%") 
    End With 
End Sub 

上面的代碼也可以轉換,當你隔着薄片循環這是非常有用的功能使用。

Option Explicit 

Sub Sample() 
    Dim wSheet As Worksheet 
    Dim TextToSearch As String 

    Set wSheet = ThisWorkbook.Sheets("Sheet1") 

    TextToSearch = "IMCOMPLETE" 

    Debug.Print GetPercentage(wSheet, TextToSearch) 
End Sub 

Function GetPercentage(ws As Worksheet, SearchText As String) As String 
    Dim WordCount As Long, ColDTotalWordCount As Long 
    Dim PercentageWord As Double 

    With ws 
     '~~> Count the occurances of the word "IMCOMPLETE" 
     WordCount = Application.WorksheetFunction.CountIf(.Columns(4), SearchText) 

     '~~> Count the total words in Col D 
     ColDTotalWordCount = Application.WorksheetFunction.CountA(.Columns(4)) 

     '~~> Calculate Percentage 
     PercentageWord = WordCount/ColDTotalWordCount 
     GetPercentage = Format(PercentageWord, "00.00%") 
    End With 
End Function 
+0

好吧,看起來這個代碼會獲取數據,但它會怎麼知道它放在哪裏?結果細胞並非全部相同。他們在同一列,但不是一個接一個。 – user1701878

+0

'但是它怎麼知道它放在哪裏?':呃,你會告訴它? –