我正在尝试使用vba隐藏工作表中的列A1。但是我收到错误“无法设置范围类的隐藏属性”
这是我的代码:
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
行中出现错误
Selection.EntireColumn.Hidden = True
我已经包含 MsgBox 来检查工作表是否受到保护,并且该列的单元格中是否有任何可用的注释。
第一个MsgBox返回No Comments,第二个返回false。
因此表格不受保护,评论也不存在。
对于为什么得到错误事件感到困惑。
请帮帮我
更新
我改变了我的代码:
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
它有效。但我已经添加了评论到其他列标题,我将鼠标悬停在单元格上。我现在没有收到评论..
删除形状是否与评论有关?
答案 0 :(得分:1)
实际上我已将注释添加到工作表中的其他列。评论来自activesheet.shapes,因此我无法隐藏列。一旦我为其设置了位置,就可以完美地运行
这段代码可以解决问题:
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
答案 1 :(得分:0)
您不能直接在列上使用entirecolumn
和隐藏属性,这些属性仅适用于Range()对象。拿Range("A1").EntireColumn.Hidden = True
感谢
纳格
答案 2 :(得分:0)
你也应该删除这一行
ActiveSheet.Columns(1)。选择
答案 3 :(得分:0)
你的代码对我来说效果很好吗?我在excel 2010,也许你不是。按原样插入,密码保护表。评论与否没有任何区别,它会隐藏它。
答案 4 :(得分:0)
两件事
请勿使用Application.WorksheetFunction.CountA(Columns(1))
查找最后一行。有关如何查找最后一行的信息,请参阅THIS链接。
这是你正在尝试的(UNTESTED)吗?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim cmt As Comment
Dim iRow As Long, lRow As Long
Dim Password As String
'~~> Change as applicable
Password = "Blah Blah"
Set ws = ThisWorkbook.Sheets("Project")
With ws
.Unprotect Password
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
For iRow = 1 To lRow
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"
Debug.Print "No Comments"
End If
Next iRow
.Columns(1).EntireColumn.Hidden = True
.Protect Password
End With
End Sub