문제 설명
셀에 입력한 msoShapeOval은 MsoShapeType으로 선언된 변수의 값이 될 수 없습니다. (msoShapeOval entered in cell not allowed to be value for variable declared as MsoShapeType)
첨부된 코드에서 shpOval3에 값을 할당하는 것을 제외하고 모든 것이 작동합니다.
셀에서 텍스트를 가져와 MsoShapeType으로 선언된 변수에 할당하려고 합니다.
1) 코드가 msoShapeOval의 셀 값을 shpOval3(MsoShapeType으로 선언됨)에 할당하려고 시도할 때 유형 불일치로 인해 오류가 발생합니다.
2) .Shapes에서 대신 strOval4(문자열)를 시도하면. AddShape(strOval4, Left, Top, Width, Height)
또한 잘못 실행되고 Type mismatch라고 표시됩니다.
셀 값이 로드될 때 셀에서 msoShapeType 상수로 변환하지 않으려고 합니다. 폼의 콤보 상자에 넣고 9의 값은 사용자에게 의미가 없습니다. 필요한 경우 변환할 수 있지만 변환하지 않고 솔루션을 찾고 있습니다.
Sub ShpType()
Dim shpOval1 As Long
Dim shpOval2 As MsoShapeType
Dim shpOval3 As MsoShapeType
Dim strOval4 As String
Sheets("Data").Range("Company1Shape") = "msoShapeOval"
shpOval1 = msoShapeOval
shpOval2 = msoShapeOval
shpOval3 = Sheets("Data").Range("Company1Shape")
strOval4 = Sheets("Data").Range("Company1Shape")
Debug.Print "shpOval1 = "; shpOval1
Debug.Print "shpOval2 = "; shpOval2
Debug.Print Sheets("Data").Range("Company1Shape")
Debug.Print "shpOval3 = "; shpOval3
Debug.Print "strOval4 = "; strOval4
End Sub
Debugger results with shpOval3 commented out
shpOval1 = 9
shpOval2 = 9
msoShapeOval
shpOval3 = 0
strOval4 = msoShapeOval
참조 솔루션
방법 1:
You can use a Select Case statement to test your string and assign the appropriate constant to a variable declared as msoAutoShapeType...
Dim strShapeType As String
strShapeType = Sheets("Data").Range("Company1Shape")
Dim shapeType As MsoAutoShapeType
Select Case strShapeType
Case "msoShapeOval"
shapeType = msoShapeOval
Case "msoShapeRectangle"
shapeType = msoShapeRectangle
'etc
'
'
'
End Select
Hope this helps!