VBA注释格式

huangapple go评论56阅读模式
英文:

VBA Comment Format

问题

我相对于在Excel中编写代码还是比较新手,我在一段代码中遇到了问题。

我想要能够为批注框设置固定的大小,对于单个单元格,我已经能够轻松实现,但是我不确定如何为整个工作表做到这一点。

这是我尝试的内容,但没有成功。任何帮助将不胜感激。

Sub com2()
  Dim lArea As Long, h As Long, n As Long
  With Range("ActiveSheet").Comment
    n = WorksheetFunction.RoundUp(Len(.Text) / 100, 0)
    .Shape.TextFrame.AutoSize = True
    h = .Shape.Height
    If .Shape.Width > 250 Then
      .Shape.Width = 250
      .Shape.Height = 250
    End If
  End With
End Sub
英文:

I am relatively new to coding with excel and I am stuck on a bit of code.

I want to be able to set a fixed size to the comment boxes which I have been able to do with a single cell no problem, but I am not sure how to do it for the whole sheet.

Here is the what I tried to do but didn’t work. Any help would be much appreciated.

Sub com2()
  Dim lArea As Long, h As Long, n As Long
  With Range("ActiveSheet").Comment
    n = WorksheetFunction.RoundUp(Len(.Text) / 100, 0)
    .Shape.TextFrame.AutoSize = True
    h = .Shape.Height
    If .Shape.Width > 250 Then
      .Shape.Width = 250
      .Shape.Height = 250
    End If
  End With
End Sub

答案1

得分: 0

假设您的问题是如何引用给定工作表中的所有注释:

Sub AutoSizeComments()
Application.ScreenUpdating = False
Dim ws As Worksheet
Set ws = Tabelle4 ' 这里可能需要替换成工作表的实际名称
' 循环遍历注释
Dim c As Range
For Each c In ws.Cells.SpecialCells(xlCellTypeComments)
adaptCommentSize c
Next c
Application.ScreenUpdating = True
End Sub


Sub adaptCommentSize(c As Range, _
Optional ByVal w As Double = 250, _
Optional ByVal h As Double = 250)
With c.Comment.Shape
With .TextFrame
.AutoSize = True
.AutoMargins = False
.MarginBottom = 0
.MarginTop = 0
.MarginLeft = 0
.MarginRight = 0
End With

    If .Width > w Then
        .Width = w
        .Height = h
    End If
End With

End Sub


附注:您提到的(未声明的)变量n在OP中未被使用。
英文:

Suppose your issue is how to reference all comments in a given worksheet:

Sub AutoSizeComments()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    Set ws = Tabelle4
    'Loop through comments
    Dim c As Range
    For Each c In ws.Cells.SpecialCells(xlCellTypeComments)
        adaptCommentSize c
    Next c
    Application.ScreenUpdating = True
End Sub
Sub adaptCommentSize(c As Range, _
    Optional ByVal w As Double = 250, _
    Optional ByVal h As Double = 250)
    With c.Comment.Shape
        With .TextFrame
            .AutoSize = True
            .AutoMargins = False
            .MarginBottom = 0
            .MarginTop = 0
            .MarginLeft = 0
            .MarginRight = 0
        End With

        If .Width > w Then
            .Width = w
            .Height = h
        End If

    End With
End Sub

Side note: Your (undeclared) variable n isn't used in OP.

huangapple
  • 本文由 发表于 2023年2月26日 21:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定