Cell RowHeight and ColumnWidth BOTH register zero - how ?

I am working on a VBA solution where it checks the value of a hidden range of cells to see if that column has been flagged for processing. As I say the column is shown (say a column width of 18) but the row is hidden.

My issue is that on one test the 1st cell in the hidden range the RowHeight returns 0 AND the ColumnWidth returns 0. The worksheet does however show cells. My question is : how is this possible ? Normally these two measurements cannot both be zero or else there would be no cells shown (i.e. none of the columns or rows).

Can any expert suggest how this can happen ?

NOTE: I have made sure the worksheet is not protected
Who is Participating?
[ fanpages ]Connect With a Mentor IT Services ConsultantCommented:
Is this just one of Excel's oddities and limitations ?


You could always store the original value in an in-cell Comment of one of the cells in that column prior to changing it, & then read the contents of the Comment if ever you need to query the value before reinstatement to visible (then remove the Comment).

Alternatively, if you only ever have one column hidden at once, you could simply have a Global (Public) variable holding the previous width value.

If you have multiple columns hidden simultaneously, then a Public Array variable could be used, or you could store the width in an unused row somewhere in the column(s).
Can you post the file so we can see the issue?
AL_XResearchAuthor Commented:
Etech0; unfortunately I cannot - I should have mentioned that initially.

I have worked many many  times in IT support so I know how limiting and irritating that it is but a) it is part of a multiple workbook solution that involves two books working together b) is extremely complicated and I would need to train you on that area of the system for you to understand what I am trying to do and to know which function to look in c) it is for a customer and so confidential d)

I have not, so far, been able to reproduce independently. With all that said it does happen consistently in the system.

I have tested, debuged and tracked down the issue to this root issue.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

[ fanpages ]IT Services ConsultantCommented:

Why not just copy the single worksheet with the issue to a new workbook & copy'n'paste the data shown "As Values" so there are no links to any other worksheets/workbooks?

If the data is sensitive, simply replace each cell with a value with an "X" or something similar.


AL_XResearchAuthor Commented:
Ok, I think I know what has been going on and it is in part my fault. I was unknowingly passing a range where both the row and the column that I was measureing were hidden (i.e. rowHeight = 0 and ColumnWidth = 0) - so that answers that.

The other part that is a little confusing is as follows and thus changes my question:

When you have a column with width of 9 and you hide the column in VBA the ColumnWidth reads as 0 but if you unhide the column using the excel interface the column width returns to 9. This behaviour causes a problem for my function that temporarily gives a hidden column (one with ColumnWidth = 0) a minimum width of 0.75 to allow searching on it's value and then restores it's previous column width (in this case 0) when finished.

How can I tell what columnwidth a cell was set to BEFORE it was hidden when it's columnWidth is set to 0 once hidden ? Excel appears to be able to do this but not via VBA ? Is this just one of Excel's oddities and limitations ?
AL_XResearchAuthor Commented:
fanpages: I am very sorry to read your post, I had hoped someone would come back and tell me I had missed something all these years. Your suggestion is just what I had been writing before I read your post (a version thereof anyhow). I am storing the column width for the key columns in a hidden row when the workbook is opened. I can the apply the correct width whenever I show those cells again.

Does anyone else have another suggestion / insight ?
AL_XResearchAuthor Commented:
Is there really no property that stores the 'unhidden colwidth' ? That is a huge Excel omission.
Saqib Husain, SyedEngineerCommented:
You might like to try this workaround

Set cr = Range("D:D").EntireColumn
If cr.ColumnWidth = 0 Then
    cr.Hidden = False
        Debug.Print cr.ColumnWidth
    cr.Hidden = True
    Debug.Print cr.ColumnWidth
End If
[ fanpages ]IT Services ConsultantCommented:
You may wish to enclose ssaqibh's "workaround" with the following two statements:

Application.ScreenUpdating = False

(ssaqibh's code)

Application.ScreenUpdating = True

This will ensure the person using the workbook does not see the column become visible, & then hidden again.
AL_XResearchAuthor Commented:
ssaqibh & fanpages: Thanks for your responses. I have already used both these concepts in the solution I discussed in my previous post. One of the main issues, apart from the incorrect ColumnWidth of zero as above, is that the range being used has a mixture of columns some hidden and some unhidden.

The final solution I came up with was to use 2 boolean arrays that store the hidden property of each cell's row and column, one array for the row index and one for the column. At the end of my function the code reads the array and re-applies the 'hidden' status.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.