VBA not getting count correct

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,362
Office Version
  1. 2010
VBA not getting count correct

Hello,

I am trying to write line in vba that count used cells in range B3:J6, I am getting wrong count (12-7-4 &11) I want them to get correct are 9-6-3 & 1


Book1
ABCDEFGHIJKL
1XYZ
2ABCA|AA|BA|CA|DA|EA|FA|GA|HA|I12
3XYZ21113507502122
4XYZ24678932
5XYZ12342
6XYZ152
762
872
982
1092
11102
Hoja1


Code:
Sub Count()

     Dim colLast1 As Long
     Dim colLast2 As Long
     Dim colLast3 As Long
     Dim colLast4 As Long
     
    
     colLast1 = Range("B3").End(xlToRight).Column
     colLast2 = Range("B4").End(xlToRight).Column
     colLast3 = Range("B5").End(xlToRight).Column
     colLast4 = Range("B6").End(xlToRight).Column
     MsgBox colLast1
     MsgBox colLast2
     MsgBox colLast3
     MsgBox colLast4
     End Sub


Thank you all

Excel 2000
Regards,
Moti
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Some questions...

1) What is in the range B3:J6... formulas or constants (and blanks)?

2) Do you want a count of each row separately or do you want the total for the entire range?

3) Where do you want the count(s) to go to?
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG29Nov58
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("B3"), Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
  MsgBox "Row " & Dn.Row & ", Count =" & Application.CountA(Dn.Resize(, 9))
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Some questions...

1) What is in the range B3:J6... formulas or constants (and blanks)?

2) Do you want a count of each row separately or do you want the total for the entire range?

3) Where do you want the count(s) to go to?
Hello Rick Rothstein, thank you


  1. Range B3:J6 are constants
  2. I want to count of each row separately
MsgBox colLast1 = 9
MsgBox colLast2 = 6
MsgBox colLast3 = 3
MsgBox colLast4 = 1

3. Just give me a correct msg 9, 6, 3 & 1

Regards,
Moti
 
Last edited:
Upvote 0
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG29Nov58
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] Rng = Range(Range("B3"), Range("B" & Rows.Count).End(xlUp))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
  MsgBox "Row " & Dn.Row & ", Count =" & Application.CountA(Dn.Resize(, 9))
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
Thank you MickG, is it possible to give 4 separate messages I can not explain but like 4 deferent variables like I shown

MsgBox colLast1 = 9
MsgBox colLast2 = 6
MsgBox colLast3 = 3
MsgBox colLast4 = 1

Or instead colLast1 any

Regards,
Moti


 
Upvote 0
Further I want to use "reference colLast1, 2 , 3 ,4" like this
Code:
 For I = 2 To colLast1
        For j = 2 To colLast2
           For k = 2 To colLast3
              For l = 2 To colLast4
 
Upvote 0
Hello,
Here is the code
Code:
 Sub Count()
     Dim colLast1 As Long
     Dim colLast2 As Long
     Dim colLast3 As Long
     Dim colLast4 As Long
          colLast1 = Range("B3").End(xlToRight).Column
          colLast2 = Range("B4").End(xlToRight).Column
          colLast3 = Range("B5").End(xlToRight).Column
          colLast4 = Range("B6").End(xlToRight).Column
   For I = 2 To colLast1
        For j = 2 To colLast2
           For k = 2 To colLast3
              For l = 2 To colLast4
 End Sub
The code above is giving me wrong count with post#1 layout
For I = 2 To colLast1 = is giving 2 to
12
For j = 2 To colLast2 = is giving 2 to 7
For k = 2 To colLast3 = is giving 2 to 4
For l = 2 To colLast4 = is giving 2 to 11

Correct would be as below
For I = 2 To colLast1 = I want code could give 2 to
9
For j = 2 To colLast2 = I want code could give2 to 6
For k = 2 To colLast3 = I want code could give2 to 3
For l = 2 To colLast4 = I want code could give2 to 1

Please can some one take a look?

Thank you

Regards,
Moti
 
Upvote 0
Thank you MickG, is it possible to give 4 separate messages I can not explain but like 4 deferent variables like I shown

MsgBox colLast1 = 9
MsgBox colLast2 = 6
MsgBox colLast3 = 3
MsgBox colLast4 = 1

Or instead colLast1 any

Regards,
Moti
Thank you MickG, Finally I manage to work your code “Application.CountA” as below.

Code:
[COLOR=#000000]Sub Count()[/COLOR]
[COLOR=#000000]     MyCount1 = Application.CountA(Range("B3:J3"))
     MyCount2 = Application.CountA(Range("B4:J4"))
     MyCount3 = Application.CountA(Range("B5:J5"))
     MyCount4 = Application.CountA(Range("B6:J6"))[/COLOR]
[COLOR=#000000]     MsgBox MyCount1
     MsgBox MyCount2
     MsgBox MyCount3
     MsgBox MyCount4[/COLOR]
[COLOR=#000000]End Sub[/COLOR]

Regards,
Moti
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,038
Latest member
apwr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top