VBA:动态范围计数功能

时间:2016-12-17 18:13:36

标签: excel-vba dynamic count range vba

我想得到这张表:

 C|O     1       2       3      Count
 A       1       1       0       2
 B       0       0       1       1
 C       0       0       0       0

但是,根据我一直在开发的代码,我得到以下结果。列数和行数是动态的。

 C|O     1       2       3      Count
 A       1       1       0       2
 B       
 C       

这是代码。我认为第一个问题出在countRange上。第二个问题是当我输入计数值时,我想做初始值b = 0和k = 1,我想提前a = a + 1并重复While循环。

Dim a As Integer
a = 0
Dim b As Integer
b = 0
Dim k As Integer
k = 1
operations = 1

 Do While operations <= sh1.Range("D4").Value + 1
  If sh2.Cells(12,3+b) = "Count" Then
   Dim countRange As Range
   Set countRange = sh2.Range(Cells(13+a, 3),Cells(13+a,3+b-1))
   Dim count As Integer
   count = sh2.Application.WorksheetFunction.Sum(countRange)
   sh2.Cells(13+a,3+b) = count
   a = a + 1
   b = 0
   k = 1
  Else
   If sh2.Cells(12+a,3+b) = operations Then
    If sh2.Cells(13+a,2) = arrayC(k) Then
     sh2.Cells(13+a,3+b).Value = 1
    Else
     sh2.Cells(13+a,3+b).Value = 0
    End If
   End If
   operations = operations + 1
   b = b + 1
   k = k + 1
  End If
 Wend

1 个答案:

答案 0 :(得分:0)

operations变量声明为Dim operations as Integer = 1并使用while循环。 Do .... While循环肯定至少执行一次。