Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

vba - Unable to Hide a column in Excel 97-2003 workbook

I am trying to hide a column A1 in my sheet using vba. But am getting a error "unable to set hidden property of range class"

Here is my code:

  ActiveWorkbook.Sheets("Project").Activate
   ActiveSheet.Unprotect password

   Dim cmt As comment
   Dim iRow As Integer

   For iRow = 1 To Application.WorksheetFunction.CountA(Columns(1))
      Set cmt = Cells(iRow, 1).comment
         If Not cmt Is Nothing Then

            Cells(iRow + 1, 1) = Cells(iRow, 1).comment.Text
            Cells(iRow, 1).comment.Delete
         Else
         MsgBox "No Comments"
         End If
   Next iRow

   MsgBox ActiveSheet.ProtectionMode

   ActiveSheet.Columns(1).Select

   Selection.EntireColumn.Hidden = True

Am getting error in the line

Selection.EntireColumn.Hidden = True

I have included MsgBox to check whether the sheet is protected and is there any comment available in the cells of that column.

1st MsgBox returns as No Comments and 2nd returns as false.

So the sheet is not protected and comment is also not present.

Confused on why getting the error eventhough.

Please help me out

UPDATE:

I have changed my code like this:

 ActiveWorkbook.Sheets("Project").Activate

    Dim sh As Shape
    Dim rangeToTest As Range
    Dim lRow As Long
    Dim c As Range

    lRow = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row

    Set rangeToTest = ActiveSheet.Range("A1:A" & lRow)
        For Each c In rangeToTest

            For Each sh In ActiveSheet.Shapes
                sh.Delete
            Next sh
        Next c

    ActiveSheet.Range("A1").EntireColumn.Hidden = True

And it worked. But I have added comments to other column headers which i get on hovering mouse over the cell. Am not getting the comments now..

Does deleting shapes have something to do with comments?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually i have added comments to other columns in my sheet. Comments come under activesheet.shapes so due to that i am unable to hide the column. Once I have set the placement for that it works perfectly

This code does the trick:

ActiveWorkbook.Sheets(sheetname).Activate

Dim sh As Shape
Dim rangeToTest As Range
Dim lRow As Long
Dim c As Range

lRow = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row

Set rangeToTest = ActiveSheet.Range("A1:A" & lRow)
    For Each c In rangeToTest

        For Each sh In ActiveSheet.Shapes
            sh.Placement = xlMoveAndSize
        Next sh
    Next c

ActiveSheet.Range("A1").EntireColumn.Hidden = True

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...