views:

970

answers:

3

As the title explains, I have an Excel 2003 workbook and I'm copying a number of columns of one sheet to another in VBA. Unknown to me, someone has hidden a few columns on the source sheet and it has messed up how I process the cells in the destination sheet.

How can I programmically determine:

  1. IF there are hidden columns
  2. WHICH columns are hidden?

Thanks! JFV

+4  A: 

For a Range, check the Range.Hidden property.

The following snippet from MSDN is a good example of how to hide/unhide a row/column:

 Worksheets("Sheet1").Columns("C").Hidden = True

You can also test the value with an If statement:

 For ColCounter = 1 To 10
      If Columns("C").Hidden = True Then
           Columns("C").Hidden = False
      End If
 Next
A. Scagnelli
should also set some sort of a flag right in the conditional section where the col hidden is set to false... maybe an array where element 0 = col A, element 1 = col B and so forth
CheeseConQueso
@Cheese: I just wanted to provide an example of how to iterate through and check the .Hidden states -- there are a great many ways to improve on the snippet (Using For Each Column instead of a counter, for instance).
A. Scagnelli
@scag yeah i figured that... wasn't trying to bash you or anything, just was addressing the second part of his question - ps, your last name is very familiar... West Essex High School? I'm from fairfield...
CheeseConQueso
@Cheese my sister went there in the mid-90s
A. Scagnelli
@A. Scagnelli: Thanks for the answer. It bothered me when pasting it messed with the column order. @CheeseConQueso: Thanks for the additional point to the answer, it helps a lot.
JFV
A: 

you can write a function like:

Function IsColumnHidden(column As Variant)

if Columns(column).ColumnWidth = 0.0 then

IsColumnHidden= true

else IsColumnHidden= false

end if

End Function

column width or row height of 0.0 is an indicator if a row or column is hidden.

gjutras
+1  A: 

If you only want to copy the visible files then one option that is quite nice it to select only the visible columns as a range.

This can be done by

Set visrng =rng.SpecialCells(xlCellTypeVisible)

It wasn't clear to me if this would be useful in your case, but I decided to post it as it could be useful to others.

Modan
@Modan: Thanks for the info. Sounds like I might be able to incorporate that in the next version. Thanks!
JFV