2012-05-17 4 views
4
나는 다음과 같은 코드를 VBA로 액티브 명령 단추의 이름 속성을 변경하려고

Reference이름 변경

:

Set shp = ActiveSheet.Shapes(Selection.Name) 

With shp.OLEFormat.Object 
    .Object.Caption = "Node" & Str(NumNodes) 
    .Name = "Node" & Str(NumNodes) 
End With 

나는를 변경할 수 있어요 캡션 이름이지만 위의 코드로는 name 속성을 변경할 수 없습니다. name 속성에 대해 문자열을 int (NumNodes)와 연결하는 방법을 찾아야합니다.

UPDATE

이것은 복사하는 특정 셀 위치에 명령 버튼을 페이스트 전체 서브 루틴이다. 버튼 생성시 이름과 캡션과 같은 속성도 변경됩니다.

Public Sub Node_Button_Duplication() 
' 
'Comments: Copies and pastes Node 1's button to the appropriate column 

Dim shp As Shape 

' Copy Node 1 button and paste in appropriate location 

    ActiveSheet.Shapes("CommandButton1").Select 
    Selection.Copy 
    Cells(5, 10 + 7 * (NumNodes - 1) - 1).Select 
    ActiveSheet.Paste 
    Selection.ShapeRange.IncrementLeft 47.25 
    Selection.ShapeRange.IncrementTop -13.5 


    Set shp = ActiveSheet.Shapes(Selection.Name) 

    With shp.OLEFormat.Object 
     .Object.Caption = "Node" & Str(NumNodes) 
     .Name = "Node" & Str(NumNodes) 
    End With 

End Sub 

답변

7

이게 너가하려는거야?

Set shp = ActiveSheet.Shapes(Selection.Name) 
shp.Name = "Node" & Str(NumNodes) 

With shp.OLEFormat.Object 
    .Object.Caption = "Node" & Str(NumNodes) 
End With 

후속

그냥이 시도하고 작동 ...

Public Sub Node_Button_Duication() 
    Dim shp As Shape 
    Dim NumNodes As Long 

    ActiveSheet.Shapes("CommandButton1").Select 
    Selection.Copy 
    Cells(5, 10 + 7 * (NumNodes - 1) - 1).Select 
    ActiveSheet.Paste 
    Selection.ShapeRange.IncrementLeft 47.25 
    Selection.ShapeRange.IncrementTop -13.5 

    NumNodes = 5 

    Set shp = ActiveSheet.Shapes(Selection.Name) 
    shp.Name = "Node" & Str(NumNodes) 

    With shp.OLEFormat.Object 
     .Object.Caption = "Node" & Str(NumNodes) 
    End With 
End Sub 

더 많은 후속

Set shp = ActiveSheet.Shapes(Selection.Name) 

    With shp.OLEFormat.Object 
     .Object.Caption = "Node" & Str(NumNodes) 
     .Name = "Node" & NumNodes 
    End With 
시도

통지, Str(NumNodes)NumNodes으로 변경 했습니까?

ActiveX 컨트롤 이름에는 공백을 사용할 수 없습니다.

지금 사용해보십시오.

스냅

enter image description here

+0

예. 결과적으로 명령 단추 이름은 "CommandButton4"로 유지됩니다. – Ehudz

+0

방금 ​​해봤습니다. 그것은 새로 만든 된 명령 단추의 이름을 변경합니다. –

+0

** NAME ** 및 ** Caption **뿐만 아니라 ** Caption **? VB 편집기에서 이름 필드 여전히 단추를 만든 후 CommandButton4 말한다 – Ehudz