View Single Post
  #20   Report Post  
Posted to microsoft.public.excel.programming
Ben McClave Ben McClave is offline
external usenet poster
 
Posts: 173
Default Macro to choose data and export in another workbook

Isabelle,

SelText is a method for an ActiveX object (not Delphi code to my knowledge).. I know that Jerry mentioned the combobox is not an ActiveX object, but as far as I can tell the object should be either a "DropDown" type (Form control) or an ActiveX type. Since "DropDown" did not work, I thought it would be worth checking if the ActiveX method would work.

As an alternative, I wrote a function to check a shape object's type and return the selected text. In the case that the object is not a DropDown or ActiveX control, it will check the value in the linked cell (in this case "A1") to find the correct value using Select Case.

Ben

Function GetText(ws As Worksheet, sShapeName As String) As String

' ws = Worksheet containing ComboBox
' sShapeName = Name of ComboBox
' Example:
' GetText(ThisWorkbook.Sheets("HOME"), "ComboBox1")

Dim sShape As Shape
Dim dd As DropDown
Dim sText As String
Err.Clear

'Set range on next line to the ComboBox's Linked Cell
sText = ws.Range("A1").Value

On Error Resume Next

Set sShape = ws.Shapes(sShapeName)
If sShape Is Nothing Then GoTo NoShape

Select Case sShape.Type
Case 8 'Drop Down
Set dd = sShape.OLEFormat.Object
If Err.Number 0 Then GoTo NoShape
GetText = dd.List(dd.ListIndex)
Exit Function
Case 12 'ActiveX
GetText = ws.OLEObjects(sShapeName).Object.SelText
If Err.Number = 0 Then Exit Function
End Select

NoShape:
If Not IsNumeric(sText) Then
GetText = sText
Else
Select Case sText
Case 1
GetText = "Customer 1"
Case 2
GetText = "Customer 2"
Case 3
GetText = "Supplier"
Case Else
GetText = vbNullString
End Select
End If

End Function