VBA EXCEL – 为对象分配名称

我正在制定一个计划时间表的图表,并且在本周内有一条线。 我正在尝试给它分配一个名称,以便在重置电子表格时删除它。 我不能只是删除所有的形状,因为我有我需要保留在工作表上的button,所以我认为id分配一个名称的形状,然后只删除这个形状只在我的“重置”function,但是我得到一个错误,当我尝试分配名称。 这是绘制线,然后将其设置为一个对象的function,任何build议将不胜感激。

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 

我得到的错误是在CurrentDateLine.Name = "Current Date Line"行告诉我,“对象不支持此属性或方法”

你的问题是因为CurrentDateLine被设置为ForeColor。 我相信你想要的是以下几点:

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