Consulting

Results 1 to 7 of 7

Thread: Solved: convert String to Double

  1. #1
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location

    Solved: convert String to Double

    I'm trying to convert a Public String variable into a Double so I can use it to change my margins on a selection, but I keep getting "Type Mismatch". I'm not sure what I'm doing wrong.
    [vba]Sub MarginAll()
    Dim myMargin As Double
    myMargin = CDbl(myVal) 'change the String to a Double

    With ActiveWindow.Selection.ShapeRange
    If ActiveWindow.Selection.ShapeRange.HasTextFrame Then
    .TextFrame.MarginLeft = myMargin
    .TextFrame.MarginRight = myMargin
    .TextFrame.MarginTop = myMargin
    .TextFrame.MarginBottom = myMargin
    End If
    End With

    End Sub[/vba]
    Office 2010, Windows 7
    goal: to learn the most efficient way

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Hi Tom ***margin is a Single BTW but your code should work. What does myVal equal?
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    It's a text string from a control in a toolbar
    [vba]Public myVal As String

    If MyButton1 Is Nothing Then
    Set MyButton1 = Application.CommandBars(YOUR_TOOLBAR_NAME).Controls.Add(Type:=msoControlCom boBox)
    End If
    With MyButton1
    .Width = 50
    .Visible = True
    End With

    Sub SaveSettingIni(iToolBar As CommandBar)
    Dim DirLoc As String
    DirLoc = "C:\macros\"
    With iToolBar
    WriteIniValue DirLoc & "margins.ini", "Settings", "Margin", myVal
    WriteIniValue DirLoc & "margins.ini", "Settings", "Units", myUnits

    WriteIniValue DirLoc & "margins.ini", "Settings", "Position", CStr(.Position)
    WriteIniValue DirLoc & "margins.ini", "Settings", "RowIndex", CStr(.RowIndex)
    WriteIniValue DirLoc & "margins.ini", "Settings", "Left", CStr(.Left)
    WriteIniValue DirLoc & "margins.ini", "Settings", "Top", CStr(.Top)
    End With
    End Sub

    Sub ReadSettingIni(iToolBar As CommandBar)
    Dim DirLoc As String
    DirLoc = "C:\macros\"
    With iToolBar
    myVal = ReadIniValue(DirLoc & "margins.ini", "Settings", "Margin")
    myUnits = ReadIniValue(DirLoc & "margins.ini", "Settings", "Units")
    MyButton2.State = IIf(myUnits = "in", msoButtonDown, msoButtonUp)
    MyButton3.State = IIf(myUnits = "cm", msoButtonDown, msoButtonUp)
    MyButton1.Text = myVal '<---
    .Position = Val(ReadIniValue(DirLoc & "margins.ini", "Settings", "Position"))
    .RowIndex = Val(ReadIniValue(DirLoc & "margins.ini", "Settings", "RowIndex"))
    .Left = Val(ReadIniValue(DirLoc & "margins.ini", "Settings", "Left"))
    .Top = Val(ReadIniValue(DirLoc & "margins.ini", "Settings", "Top"))
    End With
    End Sub[/vba]
    Office 2010, Windows 7
    goal: to learn the most efficient way

  4. #4
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    I think the reason it's happening is because it thinks myVal is null. I have to figure out a way to read the setting I typed into the control box into myVal like I do when in the ReadSettingIni procedure.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  5. #5
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Ok, I change it to this (not even using the public variable).
    It works if I step through the code, but not if I run it normally. If I run it normally I still get the Type Mismatch error.

    [VBA]Sub MarginAll()
    Dim oldctrl As CommandBarComboBox
    Dim myMargin As Single

    Set oldctrl = CommandBars("Margins").Controls(2)
    myMargin = CSng(oldctrl.Text) 'change the String to a Single

    With ActiveWindow.Selection.ShapeRange
    If ActiveWindow.Selection.ShapeRange.HasTextFrame Then
    .TextFrame.MarginLeft = myMargin * 72
    .TextFrame.MarginRight = myMargin * 72
    .TextFrame.MarginTop = myMargin * 72
    .TextFrame.MarginBottom = myMargin * 72
    End If
    End With

    End Sub[/VBA]
    Office 2010, Windows 7
    goal: to learn the most efficient way

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    How about

    [VBA]myMargin = CommandBars("Margins").Controls(2)*1
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    Hmm... I'm not sure exactly what I did, but when I tried it at work it worked. Marking the thread solved.
    Office 2010, Windows 7
    goal: to learn the most efficient way

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •