0

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.

3
  • SumA=0 instead of Null Commented Feb 25, 2015 at 15:59
  • You're setting them to Null outside of the j loop. This code appears to correctly Null the variables within the Counter loop. Note that you can't add to a null value, i.e., SumA = Null + 1 will yield a Null result. You probably should be assigning these values as 0 rather than Null. Commented Feb 25, 2015 at 16:01
  • 1
    Wouldn't the loop set them to 0 at the start of each iteration anyways? Commented Feb 25, 2015 at 16:11

2 Answers 2

4

..............use 0 rather than Null

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and this not working for me. Varieables continue to stack. No matter how many times I try to 0 or Null them.
2

A couple things:

  1. You should not dimension your variables within the For loop. Move those out of the loop below the first one.
  2. If SumA, SumB, and SumC are always integers, you should dimension them as Long or Integer instead of variant.
  3. You're resetting each of these variables to 0 at the start of the loop, so there is no need for the SumA = Null, SumB = Null, and SumC = Null lines.

5 Comments

I already tried these solutions, but SumA, SubB and SumC just stack sum of each step of loop. At the beggining I just assign them to 0, like SumA = 0 without dimension, then I tried this after assign values of sum to cells. Then I tried to Null them. It just doesn't work.
What do you mean by "stack"? I have a feeling you are getting the error because they are dimensioned as Variant instead of Long.
I edited my question with two versions that I already tried. I'll try to move dimensions out of the loop and dimension them as Integer
I'm still not understanding what you mean by "stack." Does it continue to add the numbers or does it put one number at the end of the previous number?
It's ok, I just debug my code and there is my fault in algorithm. All variables are successfully null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.