I want to count chars of data in columns, so I'm using for loop to do it.
Here is an example of my code:
Sub CharCount()
Dim Counter As Long
For Counter = 1 To 257 Step 1
Dim SumA As Variant
Dim SumB As Variant
Dim SumC As Variant
SumA = 0
SumB = 0
SumC = 0
Dim j As Long
For j = Counter To Counter * 257 Step 1
SumA = SumA + Len(Cells(j, 1))
SumB = SumB + Len(Cells(j, 2))
SumC = SumC + Len(Cells(j, 3))
Next j
Range("E" & Counter) = SumA
Range("F" & Counter) = SumB
Range("G" & Counter) = SumC
SumA = Null
SumB = Null
SumC = Null
Next Counter
End Sub
As you can see, I'm trying to Null variables for sum, but it doesn't seem to work and after each step of loop these variables just stack values of previous steps. How can I null variables?
I already tried these versions of code:
1:
Sub CharCount()
Dim Counter As Long
For Counter = 1 To 257 Step 1
SumA = 0
SumB = 0
SumC = 0
Dim j As Long
For j = Counter To Counter * 257 Step 1
SumA = SumA + Len(Cells(j, 1))
SumB = SumB + Len(Cells(j, 2))
SumC = SumC + Len(Cells(j, 3))
Next j
Range("E" & Counter) = SumA
Range("F" & Counter) = SumB
Range("G" & Counter) = SumC
Next Counter
End Sub
2:
Sub CharCount()
Dim Counter As Long
For Counter = 1 To 257 Step 1
SumA = 0
SumB = 0
SumC = 0
Dim j As Long
For j = Counter To Counter * 257 Step 1
SumA = SumA + Len(Cells(j, 1))
SumB = SumB + Len(Cells(j, 2))
SumC = SumC + Len(Cells(j, 3))
Next j
Range("E" & Counter) = SumA
Range("F" & Counter) = SumB
Range("G" & Counter) = SumC
SumA = 0
SumB = 0
SumC = 0
Next Counter
End Sub`
First and second versions doesn't work for me.
UPD. It was just my fault in algorithm. After debugging I noticed that all variables successfully nulled after step of loop.
Nulloutside of thejloop. This code appears to correctlyNullthe variables within theCounterloop. Note that you can't add to a null value, i.e.,SumA = Null + 1will yield aNullresult. You probably should be assigning these values as0rather thanNull.