VBA - Counting the number of filled cells in the row

Asked

Viewed 19,521 times

5

Hey there, guys. I’m trying to create a code responsible for reading the number of filled cells in the lines.. I tried this way but I was unsuccessful.

  For b = 1 To rMaior
         contador = Worksheets("1").Range("b:b").Cells.SpecialCells(xlCellTypeConstants).Count
                MsgBox contador
  Next b

Thank you.

2 answers

3

Hello,

I believe your formula has a small error... try to use the sheet name as below:

MsgBox Plan1.Range("b:b").Cells.SpecialCells(xlCellTypeConstants).Count

I hope I’ve helped!

1

Change your code for this:

  For b = 1 To rMaior
         contador = Worksheets("1").Rows(b).Cells.SpecialCells(xlCellTypeConstants).Count
         MsgBox contador
  Next b

This code considers that you want to count n lines of the worksheet called 1. In addition, it must have a variable called rMaior declared and that specifies the last line that must be counted. Thus, the code will run from line 1 to rMaior counting the number of non-empty cells per line.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.