简体   繁体   中英

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. 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"

Your issue is because CurrentDateLine is being set to the ForeColor. I believe what you want is the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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