在VBA中,我们可以使用Shape对象的`AutoShapeType`属性来创建一个批注框,并使用`TextFrame2.TextRange.Font`属性来设置批注框中的文本。然后,我们可以使用`Shape.Width`和`Shape.Height`属性来调整批注框的大小。最后,我们可以使用`Shape.Left`属性来限制批注框的右边界最大值。
以下是一个示例代码:
```vba
Sub CreateAndResizeComment()
Dim shp As Shape
Dim rng As Range
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
Set rng = ws.Range("A1") ' Change to your range
' Create a new comment
rng.AddComment "This is a comment"
' Get the comment shape
Set shp = rng.Comment.Shape
' Resize the comment box
With shp
.AutoShapeType = msoShapeRectangle
.TextFrame2.TextRange.Font.Size = 10 ' Change font size as needed
.Width = 200 ' Change width as needed
.Height = 100 ' Change height as needed
' Limit the right boundary of the comment box
If .Left + .Width > ws.Cells(ws.Rows.Count, 1).End(xlUp).Left Then
.Width = ws.Cells(ws.Rows.Count, 1).End(xlUp).Left - .Left
End If
End With
End Sub
```
在这个代码中,我们首先创建一个新的批注,然后获取这个批注的形状。然后,我们调整批注框的大小,并限制其右边界的最大值。如果批注框的右边界超过了工作表的最右边,我们就将批注框的宽度调整为使其左边界与最右边对齐。