简体   繁体   English

VBA EXCEL-为对象分配名称

[英]VBA EXCEL - Assigning a name to an object

I am working on developing a chart for time planning and within I have a line that is placed on the current week. 我正在开发用于计划时间的图表,并且在本周内有一条线。 I am trying to assign a name to it so that I can delete it when i reset the spreadsheet. 我正在尝试为其分配名称,以便在重置电子表格时可以将其删除。 I cannot just delete all shapes because i have buttons that I need to keep on the sheet so I thought id assign a Name to the shape to then just delete this shape only in my "Reset" function however i get an error when i try to assign the name to it. 我不能只删除所有形状,因为我有一些需要保留在表单上的按钮,因此我认为id会为该形状分配一个名称,然后仅在“重置”功能中删除该形状,但是当我尝试执行以下操作时出现错误给它分配名称。 Here is the function that draws the line and then sets it as an object, any suggestions would be appreciated. 这是绘制线条,然后将其设置为对象的函数,任何建议将不胜感激。

Public Function DrawCurrentDateLine()

    Dim wsCRC As Worksheet
    Set wsCRC = Worksheets("CRC")

    Dim lrowcrc As Long
    lrowcrc = CRC.LastRowInCRC

    Dim CurrentDateColumn As Long
    CurrentDateColumn = GetTodaysDateColumn()

    Dim x1 As Long, x2 As Long, y1 As Long, y2 As Long

    x1 = Cells(8, CurrentDateColumn).Left
    x2 = Cells(lrowcrc, CurrentDateColumn).Left
    y1 = Cells(8, CurrentDateColumn).Top
    y2 = Cells(lrowcrc + 1, CurrentDateColumn).Top

    Debug.Print lrowcrc     'Returns 91
    Debug.Print CurrentDateColumn       'Returns 89
    Dim CurrentDateLine As Object

    Set CurrentDateLine = wsCRC.Shapes.AddLine(x1, y1, x2, y2).Line.ForeColor
    CurrentDateLine.Name = "Current Date Line"

    With CurrentDateLine
        .RGB = RGB(30, 30, 30)
        '.Weight = 3
    End With


End Function

The Error i get is at the CurrentDateLine.Name = "Current Date Line" line which tells me that the "object doesn't support this property or method" 我得到的错误是在CurrentDateLine.Name = "Current Date Line"行,它告诉我“对象不支持此属性或方法”

Your issue is because CurrentDateLine is being set to the ForeColor. 您的问题是因为CurrentDateLine被设置为ForeColor。 I believe what you want is the following: 我相信您想要的是以下内容:

Set CurrentDateLine = wsCRC.Shapes.AddLine(x1, y1, x2, y2)
CurrentDateLine.Name = "Current Date Line"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM