2016-10-28 22 views
-1

VBA関数を作成して、指定された値に達した後に値の再帰を計算することに苦労しています。私のスプレッドシートでは、スプレッドシートの列Aの行を繰り返し処理することを検討しています。値に達すると、スイッチの識別番号に、自分の資格情報と一致する反復値を追加します。VBAで反復値を計算する

Public Function CalculatePS(Ref As String) 
    Dim i As Long 
    Dim j As Long 
    Dim c1 As Long 
    Dim c2 As Long 
    Worksheets("Sheet1").Activate 
    Set Range = ActiveWorksheet.Columns("A") 
    For i = 1 To Range.End 
     If Right(Cells(i, "A").Value, 4) < Ref + 1000 Then 
      j = i + 1 
      ActiveCell = Cells(j, "A") 
      Do While ActiveCell.Value <> Empty 
       If ActiveCell.Value = psType Then 
        c1 = c1 + 1 
       End If 
       If ActiveCell.Value = psType" Then 
        c2 = c2 + 1 
       End If 
      Wend 
      i = j + 1 
     End If 

    Next i 
    MsgBox (c1) 
    MsgBox (c2) 
    Return c1 
End Function 

私はRight(Cells(i,"A).Value,4)は私が私たちのネットワーク上のスイッチを識別するのに探していた参照番号と同じになるまで、この関数は、行を反復でしょう期待していました。私はVBAをかなり新しくしており、Cでこれを行うことができると確信していますが、この環境では機能しません。現在、CalculatePS( "2000")でコンパイルしようとすると、「引数はオプションではありません」というエラーが表示されます。

わかりやすくするために、私はスイッチ名のリストとそれらに割り当てられた電源を持っています。私は、どのように多くの715wと1100wの電源が、すべてのスイッチに希望の識別番号を持つかを計算しようとしています。

これは、スプレッドシートの例です。

biot-b348-uxxxx 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
BlankCell 
biot-b348-uxxxx 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
BlankCell 

ありがとうございました!

Edittedコード:

Public Function CalculatePS(uNumber As String, psType As String) As Integer 

    Dim i As Long 
    Dim j As Long 
    Dim c1 As Long 
    Worksheets("Sheet1").Activate 
    For i = 1 To rows.End 
    If Right(Cells(i, "A").Value, 4) < uNumber + 1000 And Right(Cells(i, "A").Value, 4) > uNumber Then 
     j = i + 1 
     Do While Cells(j,"A").Value <> Empty 
      If Cells(j,"A").Value = psType Then 
       c1 = c1 + 1 
      End If 
      Loop 
      i = j + 1 
     End If 

    Next i 
    CalculatePS = c1 

    End Function 
+3

'Return'はVBAの値を返すために使用されていません。代わりに関数名への代入が使用されます。 'Return'は呼び出し側に* control *(値ではない)を返すレガシーステートメントです。最新のVBAで使用する理由はありません(代わりにend subまたはexit sub(またはfunction)を使用してください)。 –

+1

@JohnColemanは 'Return c1'の代わりに' CalculatePS = c1'を持つべきであることを意味します。 –

+0

私は代わりに 'CalculatePS = c1'としたいと思います – BruceWayne

答えて

0

典型的には、私はいくつかの他の列にタスクを分離することを好むだろう。たとえば、ある列が一意の番号uXXXXを抽出するためにright(A1, 4)になり、すべての条件をカバーするのに十分な列が得られたら、簡単なcountifステートメントを使用して必要なものを達成することができます。私はあなたのワークシートのフォーマットを変更する自由がないと仮定しています。

また、VBAで一度も書いたことがないことはあなたの構文から明らかです。したがって、コードを少し変更しました。もう一度、あなたが望むものを達成するために複数の列を使用する自由を持たないと仮定すると、次の関数が役立ちます(=CalculatePS(A:A,"1234"))。

'@param vInputs variant type for your to pass range containing your input (in your case, select A:A 
'@param strUID string type representing the unique number, XXXX, in uXXXX (note to pass as "XXXX" instead of XXXX) 

Public Function CalculatePS(vInputs As Variant, strUID As String) As String 
    Dim vTemp As Variant: vTemp = vInputs 
    Dim intCounterTotal As Long 
    Dim intCounterRelevant As Long 
    Dim intNumOf715 As Integer: intNumOf715 = 0 
    Dim intNumOf1100 As Integer: intNumOf1100 = 0 

    For intCounterTotal = 1 To UBound(vTemp, 1) - 1 
     If CStr(Right(vTemp(intCounterTotal, 1), 4)) = CStr(strUID) Then 
      For intCounterRelevant = intCounterTotal + 1 To UBound(vTemp, 1) - 1 
       If Trim(vTemp(intCounterRelevant, 1)) = vbNullString Then GoTo FinishComputation 
       If InStr(vTemp(intCounterRelevant, 1), "715WAC") > 0 Then intNumOf715 = intNumOf715 + 1 
       If InStr(vTemp(intCounterRelevant, 1), "1100WAC") > 0 Then intNumOf1100 = intNumOf1100 + 1 
      Next intCounterRelevant 
     End If 
    Next intCounterTotal 
FinishComputation: 
    CalculatePS = "Number of 715s: " & intNumOf715 & "; Number of 1100s: " & intNumOf1100 
End Function 

試験データ:

biot-b348-uxxxx 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
BlankCell 
biot-b348-u1234 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
C3KX-PWR-1100WAC 
C3KX-PWR-715WAC 
C3KX-PWR-715WAC 
BlankCell 
関連する問題