Count columns with data in them

jag108

Active Member
Joined
May 14, 2002
Messages
433
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
I am trying to count the amount of columns with actual data in them. For instance I have columns from A - J in one sheet but A to K in another, the code I will run on them will need to know when to stop.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Not sure what to make of this one.
When I use the code mentioned I get 41 when there are only 10 cloumns with data in them, so 31 are superfluous. Cell/column 41 is empty as well.
 
Upvote 0
The simple solution would be:

intColumns = ActiveSheet.Usedrange.Columns.Count

however if there are empty columns inbetween A and J, this will count them as well. Also this will cause problems if some cells outside the "real" usedrange of the worksheet have some special formatting, such as a date format, then Excel will see that cell as being part of the usedrange even tho it doesn't contain anything

there are other ways if necessary
 
Upvote 0
For some really strange reason this is coming up with 41 as well??????
I have made sure that there is no data from J on, there are also no blank columns/rows between A and J on my test sheet.
 
Upvote 0
could there be empty columns between A & J? If so will it cause a problem if the macro operates on these columns? If so we can do a check to see if the column is empty by using

If Application.CountA(Columns(i)) <> 0 Then
'put your code here
else
'move to next column
endif
 
Upvote 0
lastCol = Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
 
Upvote 0
Ponsonby gets first prize,
LastCol = Cells.Find(What:="*", _ SearchDirection:=xlPrevious, _ SearchOrder:=xlByColumns).Column
works perfectly.

Thanks everyone!!!
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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