2017-10-20 67 views
0

需要您的VBA專業知識來完成以下任務。 我需要在一列上做一個「MACORS」城市標識,並打印數字旁邊的每個城市。 我有這樣的代碼:COUNTIF並打印計數的字符串

Public Sub CountLocation() 

Range("V2").Select 

Selection.End(xlDown).Select 
lastcell = ActiveCell.Address 

ActiveCell.Offset(2, 0).Value = "=countif(V2:" + lastcell + ", ""Zurich"")" 
ActiveCell.Offset(2, 1).Value = "Zurich" 
ActiveCell.Offset(3, 0).Value = "=countif(V2:" + lastcell + ", ""Armonk"")" 
ActiveCell.Offset(3, 1).Value = "Armonk" 
ActiveCell.Offset(4, 0).Value = "=countif(V2:" + lastcell + ", ""Hong Kong"")" 
ActiveCell.Offset(4, 1).Value = "Hong Kong" 
ActiveCell.Offset(5, 0).Value = "=countif(V2:" + lastcell + ", ""London"")" 
ActiveCell.Offset(5, 1).Value = "London" 
ActiveCell.Offset(6, 0).Value = "=countif(V2:" + lastcell + ", ""Madrid"")" 
ActiveCell.Offset(6, 1).Value = "Madrid" 

End Sub 

我的問題是我在一個片大約90「可能」城市,從其它片材而不同。 我上面的代碼打印了一個不在特定工作表中的城市。

謝謝你的幫助

+0

您的代碼正在與不管是當時的activesheet你運行你的代碼。你是否試圖指定一個或多個工作表來運行這個? – QHarr

+0

另外,在代碼的頂部使用Option Explicit,以便檢查變量的拼寫和聲明。 – QHarr

+0

謝謝QHarr,是的,它正在工作,但它返回了未輸入到列中的城市的名稱。 例如: 2蘇黎世 0香港。 我不希望香港印刷,因爲它沒有價值。 – Jonathan

回答

1

把城市放在一個數組中並循環該數組。此外,將計數範圍作爲一個範圍。有一個如果測試大於0,所以不會寫出城市,如果不存在。

一起:

Option Explicit 

Public Sub CountLocation() 

    Dim wb As Workbook 
    Dim ws As Worksheet 
    Dim countRange As Range 

    Set wb = ThisWorkbook 
    Set ws = wb.ActiveSheet 'Change as appropriate 

    Dim lastRow As Long 
    lastRow = ws.Range("V2").End(xlDown).Row 'Update: Extracted this into a variable 

    Set countRange = ws.Range(ws.Cells(2, "V"), ws.Cells(lastRow, "V")) 

    Dim Cities() 
    Cities = Array("Zurich", "Armonk", "Hong Kong", "London", "Madrid") 

    Dim city As Long 
    Dim counter As Long 
    Dim startRange As Range 

    Set startRange = ws.Cells(lastRow, "V").Offset(2, 0) 

    counter = 2 

    For city = LBound(Cities) To UBound(Cities) 
     If Application.WorksheetFunction.CountIf(countRange, Cities(city)) > 0 Then 
     startRange.Offset(counter, 0) = Application.WorksheetFunction.CountIf(countRange, Cities(city)) 
     startRange.Offset(counter, 1) = Cities(city) 
     counter = counter + 1 
     End If 

    Next city 

End Sub 

注意事項的各個部分:

下在V列從V2開始空白單元格之前得到最後使用一行。如果在上次使用的單元格之前有空單元格,則會得到錯誤的最後一行。

lastRow = ws.Range("V2").End(xlDown).Row, "V") 

然後設置的範圍爲V2算到最後使用的排五:

Set countRange = ws.Range(ws.Cells(2, "V"),ws.Cells(lastRow, "V")) 

定義在那裏你會開始寫出來的結果:

Set startRange = ws.Cells(lastRow, "V").Offset(2, 0) 

在計數範圍內循環計算特定城市發生的城市陣列:

Application.WorksheetFunction.CountIf(countRange, Cities(city)) 

將它包裝在IF語句中以查看count是否大於0。

If Application.WorksheetFunction.CountIf(countRange, Cities(city)) > 0 

寫出來的片從第一粘貼位置由一個行每次使用計數器變量抵消:

startRange.Offset(counter, 0) = Application.WorksheetFunction.CountIf(countRange, Cities(city)) 
startRange.Offset(counter, 1) = Cities(city) 
+0

你是天才! 我需要研究你的代碼謝​​謝你! – Jonathan

+1

我已經添加了一些更多的解釋來幫助你。可能有更有效的寫作方式,但它是實現你想要的方法。 – QHarr

+0

哇!很好QHarr, 順便說一句,我重新運行的代碼,它現在不工作,它說:方法'範圍'object'_Worksheet'失敗。它突出顯示Set countRange = ws.Range(Cells(2,「V」),Cells(ws.Range(「V2」)。End(xlDown).Row,「V」)) – Jonathan