vba - 在图表中定位标签

标签 vba excel excel-2016

我有一个包含两个图表的电子表格,我想根据表中的值在系列点之一旁边添加一些文本框。

我为此创建了两个程序,每个程序都有自己的优点和缺点:

Sub add_comments(apply_to As Series, source_range As Range) 
  Dim i As Long 
  Dim c As Range 

  If source_range.Count > apply_to.Points.Count Then 
    Set source_range = source_range.Resize(apply_to.Points.Count, 1) 
  End If 

  i = 1 
  For Each c In source_range 
    If Not IsError(c) And i <= apply_to.Points.Count Then 
      If Len(c.Text) <> 0 Then 
        apply_to.Points(i).HasDataLabel = True 
        apply_to.Points(i).DataLabel.Text = c.Value2 
        apply_to.Points(i).DataLabel.Format.AutoShapeType = msoShapeRectangularCallout 
        With apply_to.Points(i).DataLabel.Format.Line 
          .Visible = msoTrue 
          .ForeColor.RGB = RGB(0, 0, 0) 
        End With 
        apply_to.Points(i).DataLabel.Position = xlLabelPositionAbove 
      Else 
        If apply_to.Points(i).HasDataLabel Then 
          apply_to.Points(i).DataLabel.Delete 
        End If 
      End If 
    End If 
    i = i + 1 
  Next c 
End Sub 

上面的代码使用标签,这是非常理想的,除了我无法重新定位标签,并​​且当它们重叠时它会变得有点难看。

Sub alternative_comments(apply_to As Series, source_range As Range) 
  Dim c As Range 
  Dim i As Long 

  If source_range.Count > apply_to.Points.Count Then 
    Set source_range = source_range.Resize(apply_to.Points.Count, 1) 
  End If 

  i = 1 
  For Each c In source_range 
    If Not IsError(c) And i <= apply_to.Points.Count Then 
      If Len(c.Text) <> 0 Then 
        With SPC_01.Shapes.AddLabel(msoTextOrientationHorizontal, 100, 100, 10, 10) 
          .TextFrame2.TextRange.Characters.Text = c.Text 
          With .Line 
            .Visible = msoTrue 
            .ForeColor.RGB = RGB(0, 0, 0) 
          End With 
          .Top = apply_to.Points(i).Top - .Height 
          .Left = apply_to.Points(i).Left - .Width 

          Debug.Print apply_to.Points(i).Top & " - " & .Top 
          Debug.Print apply_to.Points(i).Left & " - " & .Left 
        End With 
      End If 
    End If 
    i = i + 1 
  Next c 
End Sub 

另一个解决方案使用文本框,这非常适合移动和调整大小,但不会自动缩放以适应文本,而且我也找不到任何明智的方法来做到这一点。

enter image description here

正如您所看到的,尽管我觉得使用标签的缺点比使用文本框的缺点要轻一些,但我仍然坚持这两种方法。但是,我想知道你们中的任何人是否可以告诉我自动向一系列数据点添加注释的最佳方法是什么?我走在正确的道路上吗?

我还有posted this question to the VBAExpress forums ,如果你们中有人想查看整个工作簿。

最佳答案

对于文本框方法,您可以使用以下方法将其设置为自动调整大小:

.TextFrame2.AutoSize = msoAutoSizeShapeToFitText

然后有两个文本换行选项可以改变外观。您可以将文本设置为换行:

.TextFrame2.WordWrap = True

这不会改变文本框的宽度,它会像上面那样垂直地拉出文本框。

或者您可以将其设置为不换行:

.TextFrame2.WordWrap = False

这会将其水平串起来,直到遇到换行符。

因此,您可以根据需要设置文本框宽度并打开换行,或者在源文本中添加显式换行符 (Alt + Enter) 并关闭换行。

关于vba - 在图表中定位标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42779253/

相关文章:

excel - VLOOKUP 并填写日期范围内的所有单元格

vba - 保留格式化长(> 15 个字符)数字

excel - 双击复制单元格并自动粘贴到另一张纸上的不同单元格

python-3.x - 如何使用excel之类的功能修改数据框中的列?

office-js - Office.js 性能 : How much should I put into one Excel. 运行函数?

vba - 如何编辑 Access ADP 文件?

vba - 使文本在单行的一系列单元格中从右到左爬行

excel - 如何在 Excel 2016 VBA 编辑器中启用 Unicode

vba - 使用 VBA 在 Excel 中选择复选框

powerquery - Power Query - 带有值的多个 OR 语句