2017-11-29 1 views
0

セルを特定の範囲のcountifs式に設定するためにオートフィルを使用するサブを書きましたが、後で数式の値を値に変換することはできません。vbaの値を数値に変更する

コピー、貼り付けを使用しないでください。私は.value = .valueのいくつかの異なるバリエーションを使用しようとしましたが、これを正しく動作させることができないように見えます。かなり多くの場合、すべての値を1に設定します。私は以下の作業コードのコピーは、私は

おかげ

' wsWorksheet is the worksheet to be used 
' lngFirstRow is the first row of data 
' strSourceCol is the letter (e.g. A) of the column to be counted 
' strCountCol is the letter (e.g. I) of the column to add the count result to 
Sub Concurrency(wsWorksheet As Worksheet, lngFirstRow As Long, strSourceCol As String, strCountCol As String) 
Dim lngLastRow As Long 
Dim strFormula As String 
Const ConcFormula As String = "=(COUNTIFS(D$1:D1,"">""&C2,A$1:A1,A2))+1" 

'Find the last row of data in the source column 
With wsWorksheet 
     lngLastRow = .Cells(.Rows.Count, strSourceCol).End(xlUp).Row 
     If lngLastRow < lngFirstRow Then lngLastRow = lngFirstRow 
End With 


'With the first cell on the count column 
With wsWorksheet.Cells(lngFirstRow, strCountCol) 
    'Add the count formula 
    .Formula = ConcFormula 
    'If there are more rows autofill it down 
    If lngLastRow > lngFirstRow Then 
     .AutoFill Destination:=Range(_ 
      .Cells, _ 
      .Offset(rowoffset:=lngLastRow - lngFirstRow).Cells), Type:=xlFillDefault 
    End If 
End With 

End Sub 
を行った.VALUE = .VALUEミスとの混乱を避けるためにCOUNTIFS式にセルをセット取り付けた

を大幅に理解されます

答えて

2

これを行うことができるはずです:

With wsWorksheet 
    With .Range(.Cells(lngFirstRow, strCountCol), .Cells(lngLastRow, strCountCol)) 
    'Add the count formula 
     .Formula = ConcFormula 
     .Value2 = .Value2 
    End With 
End With 
+0

こんにちは。完璧に機能してくれたことに感謝しています:)あなたは私の頭痛 – MrJam

関連する問題