Consulting

Results 1 to 10 of 10

Thread: Loop Thru Textboxes - Help....

  1. #1

    Loop Thru Textboxes - Help....

    Pulling my hair out....

    I have 8 sheets in a project and each has its own textbox named htmlbox1, htmlbox2, etc.

    Want code to check which sheet is active and then send output to the appropriate textbox.

    Tried this:

    [vba]If ActiveSheet.codename = "Sheet1" Then
    i = 1
    End If
    If ActiveSheet.codename = "Sheet2" Then
    i = 2
    End If[/vba]
    etc........then send the resultant output to the textbox associated with that active sheet.....

    [vba]ActiveSheet.("htmlbox" & i).Text = "whatever"[/vba]
    I'm new and I've tried everything I know.

    Thanks
    Pete

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,455
    Location
    [vba]


    Dim i As Long

    With ActiveSheet

    Select Case .Name

    Case "Sheet1": i = 1
    Case "Sheet2": i = 2
    Case "Sheet3": i = 3
    'etc
    End Select

    .OLEObjects("htmlbox" & i).Object.Text = "whatever"
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Tried OLEObjects before - used your idea as well and keep getting:

    Run Time Error '1004':
    Unable to get the OLEObjects property of the Worksheet class.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,455
    Location
    How did you create these textboxes?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Embarrassingly enough - I'm not sure. It was a code snippet I found that I have tried to massage for my own application.

    I believe

    =EMBED("Forms.TextBox.1","")

    might help?

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    There are Form controls and ActiveX controls. Looks like an ActiveX control. In the Developer tab, select the Design view and make sure that your textbox name is of that naming convention and exists.

    xld's code worked for me.

    [vba]Sub Test()
    ActiveSheet.OLEObjects("htmlbox" & NumberPart(ActiveSheet.Name)).Object.Text = "whatever"
    End Sub

    Function NumberPart(aString As String) As Long
    Dim s As String, i As Integer, mc As String
    For i = 1 To Len(aString)
    mc = Mid(aString, i, 1)
    If Asc(mc) >= 48 And Asc(mc) <= 57 Then s = s & mc
    Next i
    NumberPart = CLng(s)
    End Function[/vba]
    Last edited by Kenneth Hobs; 01-17-2012 at 09:27 AM.

  7. #7
    Textboxes are named htmlbox1, htmlbox2, etc.

  8. #8
    try :

    ActiveSheet.OLEObjects("htmlbox" & ActiveSheet.Index).Object.Value = "Whatever"

  9. #9
    redhunter:

    Well that worked but I needed to have 6 lines of text as output so I had to do this:

    [vba]ActiveSheet.OLEObjects("htmlbox" & ActiveSheet.Index).Object.Text = [/vba] [vba]ActiveSheet.OLEObjects("htmlbox" & ActiveSheet.Index).Object.Text + vbCrLf + "whatever"[/vba]
    And do it 6 times, each with different info. But it works fine now.

    Thanks!

  10. #10
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Use & rather than + as a concatenation operator.

    If you told us what you wanted, we could probably show you a loop or another method to build a concatenated string. Now that you have 5 posts, you can attach a workbook. Simple example workbooks help us help you more readily.

Posting Permissions

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